Hello, I’m trying to create a front-end for interacting with a Convai character, and I keep running into the following erros when trying to send a voice clip recording from the user:
Request failed: HTTP/1.1 500 Internal Server Error
and
Get Response Failed 400 Invalid recognition ‘config’: bad sample rate hertz.
I’ve tried tinkering with the sample rate of my audio recording, specifying the sample rate in my payload form, and specifying the mime type of the file, but none of them work.
In my code I’m converting the audio clip to a byte array using File.ReadAllBytes(), then adding it to my request with form.AddBinary Data.
public void SendUserMessage(Action<object> onSuccessfulAPI, string userMessage, byte[] userAudio = null)
{
currentActionTarget = onSuccessfulAPI;
WWWForm form = new WWWForm();
if (userMessage != null) form.AddField("userText", userMessage);
else if (userAudio != null)
{
form.AddBinaryData("file", userAudio, "VoiceRecording.wav");
//form.AddField("sample_rate", "16000");
}
form.AddField("charID", GlobalData.Instance.CharacterKey);
form.AddField("sessionID", GlobalData.Instance.SessionID);
form.AddField("voiceResponse", "true");
StartCoroutine(SendAPIRequest("character/getResponse", form, ProcessReceivedResponse));
}
The voice file is being generated properly because I can open it and listen to it in VLC media player. I can also get the audio file to play back in Unity so I know it’s pulling from the correct file path.
I’ve shared a working sample code with you that has been tested successfully. If you feel unsure about your current implementation, I recommend referring to the example Python code provided in our documentation. You can use that as a base and adapt it to your project with the help of AI tools if needed.
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class ConvaiInteractionAPI : MonoBehaviour
{
// The API endpoint URL for Convai Character's response
private string url = "https://api.convai.com/character/getResponse";
// Your Convai API Key (set this in the Inspector or hard-code your key)
public string apiKey = "";
// Path to the audio file (Update this path accordingly;
public string audioFilePath = "Path/To/Your/audio.wav";
// Your character id (set this in the Inspector or hard-code your character id)
public string characterId = "<your character id>";
// Start is called on the frame when a script is enabled
void Start()
{
// Combine path to locate TestAudio.wav inside Assets folder
audioFilePath = Path.Combine(Application.dataPath, "TestAudio.wav");
ConvaiAPIKeySetup.GetAPIKey(out string convaiAPIKey);
if (string.IsNullOrEmpty(convaiAPIKey))
{
Debug.LogError("Convai API Key not found. Please set it in the ConvaiAPIKeySetup ScriptableObject.");
return;
}
else
{
apiKey = convaiAPIKey;
}
// Start the coroutine to send the API request
StartCoroutine(SendConvaiRequest());
}
// Coroutine to send a POST request to the Convai API
IEnumerator SendConvaiRequest()
{
// Create a form and add the fields required by the API
WWWForm form = new WWWForm();
form.AddField("charID", characterId);
form.AddField("sessionID", "-1");
form.AddField("responseLevel", "5");
form.AddField("voiceResponse", "True");
// Read the audio file into a byte array
byte[] audioData = null;
try
{
audioData = File.ReadAllBytes(audioFilePath);
}
catch (Exception e)
{
Debug.LogError("Error reading audio file: " + e.Message);
yield break;
}
// Add the audio file to the form as binary data
form.AddBinaryData("file", audioData, "audio.wav", "audio/wav");
Debug.Log("Sending request to Convai API...");
// Create a UnityWebRequest for a POST request with the form data
UnityWebRequest request = UnityWebRequest.Post(url, form);
// Set the header with the API Key
request.SetRequestHeader("CONVAI-API-KEY", apiKey);
// Send the request and wait until it completes
yield return request.SendWebRequest();
// Check for network errors
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("Request Error: " + request.error);
}
else
{
// Log the full JSON response for debugging purposes
string jsonResponse = request.downloadHandler.text;
Debug.Log("Response JSON: " + jsonResponse);
// Parse the JSON response into the ConvaiResponse object
ConvaiResponse responseData = JsonUtility.FromJson<ConvaiResponse>(jsonResponse);
if (responseData != null)
{
Debug.Log("Session ID: " + responseData.sessionID);
Debug.Log("Character Response: " + responseData.text);
// Decode the base64 audio string if available and write to a file
if (!string.IsNullOrEmpty(responseData.audio))
{
try
{
byte[] decodedAudio = Convert.FromBase64String(responseData.audio);
// Save the audio file in the persistent data path
string outputPath = Path.Combine(Application.persistentDataPath, "audioResponse.wav");
File.WriteAllBytes(outputPath, decodedAudio);
Debug.Log("Audio file saved at: " + outputPath);
}
catch (Exception e)
{
Debug.LogError("Error decoding audio: " + e.Message);
}
}
}
else
{
Debug.LogError("Failed to parse response JSON.");
}
}
}
// Class to represent the structure of the JSON response from the API
[Serializable]
public class ConvaiResponse
{
public string text;
public string sessionID;
public string audio;
}
}
What does this solution change? That’s what I’m trying to figure out. What does your script do differently to mine?
My basic outline is:
Record audio → Save audio to local file path → Read from path → Convert audio into byte array → Put audio file into WWWForm → Sent form in UnityWebRequest
The error I’m getting is related to sample rate but your form doesn’t specify one, despite there being a field on the documentation page. Is there a specific sample rate I should use? Do I need to specify it at all?
The error says “Get Response Failed 400 Invalid recognition ‘config’: bad sample rate hertz.”. I get this error regardless of whether I specify a sample rate in my payload form. Have tried 16000, 44100 and 48000 khz.
I used your code, only changing the file path to find my audio file, and it gives the exact same error. Either the issue is with my audio file, or on Convai’s end.