88from httpx_ws import AsyncWebSocketSession , WebSocketSession , aconnect_ws , connect_ws
99
1010from .realtime import aiter_websocket_audio , iter_websocket_audio
11- from ..core import AsyncClientWrapper , ClientWrapper , RequestOptions
11+ from ..core import AsyncClientWrapper , ClientWrapper , RequestOptions , WebSocketOptions
1212from ..core .iterators import AsyncAudioStream , AudioStream
1313from ..types import (
1414 AudioFormat ,
@@ -215,6 +215,7 @@ def stream_websocket(
215215 config : TTSConfig = TTSConfig (),
216216 model : Model = "s1" ,
217217 max_workers : int = 10 ,
218+ ws_options : Optional [WebSocketOptions ] = None ,
218219 ) -> Iterator [bytes ]:
219220 """
220221 Stream text and receive audio in real-time via WebSocket.
@@ -305,6 +306,9 @@ def text_generator():
305306 speed , base = config .prosody
306307 )
307308
309+ # Prepare WebSocket connection kwargs
310+ ws_kwargs = ws_options .to_httpx_ws_kwargs () if ws_options else {}
311+
308312 executor = ThreadPoolExecutor (max_workers = max_workers )
309313
310314 try :
@@ -316,6 +320,7 @@ def text_generator():
316320 "model" : model ,
317321 "Authorization" : f"Bearer { self ._client .api_key } " ,
318322 },
323+ ** ws_kwargs ,
319324 ) as ws :
320325
321326 def sender ():
@@ -502,6 +507,7 @@ async def stream_websocket(
502507 speed : Optional [float ] = None ,
503508 config : TTSConfig = TTSConfig (),
504509 model : Model = "s1" ,
510+ ws_options : Optional [WebSocketOptions ] = None ,
505511 ):
506512 """
507513 Stream text and receive audio in real-time via WebSocket (async).
@@ -591,11 +597,15 @@ async def text_generator():
591597 speed , base = config .prosody
592598 )
593599
600+ # Prepare WebSocket connection kwargs
601+ ws_kwargs = ws_options .to_httpx_ws_kwargs () if ws_options else {}
602+
594603 ws : AsyncWebSocketSession
595604 async with aconnect_ws (
596605 "/v1/tts/live" ,
597606 client = self ._client .client ,
598607 headers = {"model" : model , "Authorization" : f"Bearer { self ._client .api_key } " },
608+ ** ws_kwargs ,
599609 ) as ws :
600610
601611 async def sender ():
0 commit comments