elevenlabs / elevenlabs-python

The official Python API for ElevenLabs Text to Speech.
https://elevenlabs.io/docs/api-reference/getting-started
MIT License
1.97k stars 222 forks source link

optimize_streaming_latency in websocket caused errors #300

Open netw0rkf10w opened 1 month ago

netw0rkf10w commented 1 month ago

Hello,

In your websockets documentation, it is stated that optimize_streaming_latency can be used a query parameter. However, when adding ?optimize_streaming_latency=3 to my uri as follows:

uri = f"wss://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream-input?model_id=eleven_multilingual_v2?optimize_streaming_latency=3"
    async with websockets.connect(uri) as websocket:
        await websocket.send(json.dumps({
            "text": " ",
            "voice_settings": {"stability": 1, "similarity_boost": 0.8},
            "xi_api_key": os.getenv("ELEVENLABS_API_KEY"),
        }))

        async def listen():
...

I obtained the following errrors:

Traceback (most recent call last):
  File "text_to_speech.py", line 155, in <module>
    asyncio.run(benchmarking())
  File "/home/miniconda3/envs/py310/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/home/miniconda3/envs/py310/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "text_to_speech.py", line 148, in benchmarking
    await text_to_speech_input_streaming(VOICE_ID, text_iterator(text))
  File "text_to_speech.py", line 84, in text_to_speech_input_streaming
    async with websockets.connect(uri) as websocket:
  File "/home/miniconda3/envs/py310/lib/python3.10/site-packages/websockets/legacy/client.py", line 629, in __aenter__
    return await self
  File "/home/miniconda3/envs/py310/lib/python3.10/site-packages/websockets/legacy/client.py", line 647, in __await_impl_timeout__
    return await self.__await_impl__()
  File "/home/miniconda3/envs/py310/lib/python3.10/site-packages/websockets/legacy/client.py", line 654, in __await_impl__
    await protocol.handshake(
  File "/home/miniconda3/envs/py310/lib/python3.10/site-packages/websockets/legacy/client.py", line 325, in handshake
    raise InvalidStatusCode(status_code, response_headers)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 500

Is this a bug or did I do something wrong please?

Thank you in advance for your reply!

aamoronatti commented 6 days ago

You are using ? instead of & in the wss url

uri = f"wss://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream-input?model_id=eleven_multilingual_v2&optimize_streaming_latency=3"