Thank you, @J7er, for sharing your findings. I'm adding this response here to summarize the root cause and solution for others who may encounter the same issue.
Issue:
When sending audio through a Twilio WebSocket using FastAPI, the following error was observed:
Error sending message: string indices must be integers
This occurred because the message was being sent using await ws.send(json.dumps(message))
, which sends the data as a raw string.
Explanation:
Twilio expects the message to be sent as a proper JSON object. Sending it as a string can cause parsing issues on the receiving end, resulting in the error.
Recommended Fix:
Instead of manually converting the message to a JSON string, use FastAPI's built-in method:
await ws.send_json(message)
This checks the message is sent in the expected format.
Thanks again for confirming the solution. I hope this helps others facing a similar issue.