Watson Speech to Text: Response Message too Long - cut off

218 Views Asked by At

I have a fully-functional client for IBM Watson Speech To Text. I wanted to start recording more metadata (word confidence, start/end times, etc.) so I added the appropriate fields to the my initial request.

Everything worked smoothly until I encountered an exception deserializing the Watson text message response into a JSON object. When I printed the string, this was the result. Notice that it is cut off, which explains the error deserializing:

{
   "results": [
      {
         "word_alternatives": [
            {
               "start_time": 3.71,
               "alternatives": [
                  {
                     "confidence": 1.0,
                     "word": "Hey"
                  }
               ],
               "end_time": 3.97
            },
            {
               "start_time": 3.97,
               "alternatives": [
                  {
                     "confidence": 1.0,
                     "word": "what's"
                  }
               ],
               "end_time": 4.54
            },
            {
               "start_time": 4.54,
               "alternatives": [
                  {
                     "confidence": 1.0,

It appears that I am asking for too much information. The System.Net.WebSockets.WebSocketReceiveResult object returned from ClientWebSocket.ReceiveAsync() has a property result.Count describing the number of bytes of information sent. In debugging I found that result.Count = 1024.

My questions are these:

1 - Is the 1kB limit imposed by Watson or is that a limitation of the .NET WebSocket library?

2 - How can I lift that limitation to receive the full message?

Edit: Minimal example

There is a lot of code that touches this issue, but hopefully this give enough context to help:

    // Set up connection
    ClientWebSocket socket = new ClientWebSocket();
    // Works: 
    //string headerInfo = "{ \"content-type\":\"audio/l16;rate=8000\",\"interim_results\":true,\"smart_formatting\":true,\"timestamps\":false,\"inactivity_timeout\":-1,\"word_confidence\":false,\"profanity_filter\":false,\"action\":\"start\"}";
    // Doesn't:
    string headerInfo = "{ \"content-type\":\"audio/l16;rate=8000\",\"interim_results\":true,\"smart_formatting\":true,\"timestamps\":true,\"inactivity_timeout\":-1,\"word_confidence\":true,\"profanity_filter\":false,\"action\":\"start\"}";
    var startMsg = new ArraySegment<byte>(Encoding.UTF8.GetBytes(headerInfo));
    var endOfMsg = true;
    await socket.SendAsync(startMsg, WebSocketMessageType.Text, endOfMsg, default(CancellationToken));

    // Send Audio bytes

    // Receive response
    var msgBuffer = new byte[8 * 1024];
    var receiver = new ArraySegment<byte>(msgBuffer);
    var result = await socket.ReceiveAsync(receiver, CancellationToken.None);
    var message = Encoding.UTF8.GetString(receiver.Array.Take(result.Count).ToArray());
    var result = JsonConvert.DeserializeObject<ResultsObject>(watsonMsg);

ResultsObject is a local type to deserialize into.

1

There are 1 best solutions below

0
On

It turned out that a message was being sent in several chunks. The solution was this fix:

var ResultMsg = new List<byte>();
if (receiver.Array.Length > 0)
{
    ResultMsg.AddRange(receiver.Array.Take(result.Count));
}
if (result.EndOfMessage)
{
    var msgBytes = ResultMsg.ToArray();
    var message = Encoding.UTF8.GetString(msgBytes);
    TextMessageHandler(message);
    ResultMsg.Clear();
}