Skip to content

Commit 14fc516

Browse files
committed
feat: add WebSocket options for configurable timeouts and message limits in TTS streaming
1 parent c316875 commit 14fc516

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

src/fishaudio/core/websocket_options.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@
55

66
class WebSocketOptions:
77
"""
8-
Options that can be provided to configure WebSocket connections.
8+
Options for configuring WebSocket connections.
9+
10+
These options are passed directly to httpx_ws's connect_ws/aconnect_ws functions.
11+
For complete documentation, see https://frankie567.github.io/httpx-ws/reference/httpx_ws/
912
1013
Attributes:
11-
keepalive_ping_timeout_seconds: Maximum time to wait for a pong response
12-
to a keepalive ping before considering the connection dead (default: 20s)
13-
keepalive_ping_interval_seconds: Interval between keepalive pings (default: 20s)
14-
max_message_size_bytes: Maximum size for incoming messages (default: 65,536 bytes)
15-
queue_size: Size of the message receive queue (default: 512)
14+
keepalive_ping_timeout_seconds: Maximum delay the client will wait for an answer
15+
to its Ping event. If the delay is exceeded, WebSocketNetworkError will be
16+
raised and the connection closed. Default: 20 seconds.
17+
keepalive_ping_interval_seconds: Interval at which the client will automatically
18+
send a Ping event to keep the connection alive. Set to None to disable this
19+
mechanism. Default: 20 seconds.
20+
max_message_size_bytes: Message size in bytes to receive from the server.
21+
Default: 65536 bytes (64 KiB).
22+
queue_size: Size of the queue where received messages will be held until they
23+
are consumed. If the queue is full, the client will stop receiving messages
24+
from the server until the queue has room available. Default: 512.
25+
26+
Note:
27+
Parameter descriptions adapted from httpx_ws documentation.
1628
"""
1729

1830
def __init__(

src/fishaudio/resources/tts.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,16 @@ def stream_websocket(
232232
config: TTS configuration (audio settings, voice, model parameters)
233233
model: TTS model to use
234234
max_workers: ThreadPoolExecutor workers for concurrent sender
235+
ws_options: WebSocket connection options for configuring timeouts, message size limits, etc.
236+
Useful for long-running generations that may exceed default timeout values.
237+
See WebSocketOptions class for available parameters.
235238
236239
Returns:
237240
Iterator of audio bytes
238241
239242
Example:
240243
```python
241-
from fishaudio import FishAudio, TTSConfig, ReferenceAudio
244+
from fishaudio import FishAudio, TTSConfig, ReferenceAudio, WebSocketOptions
242245
243246
client = FishAudio(api_key="...")
244247
@@ -274,6 +277,16 @@ def text_generator():
274277
):
275278
f.write(audio_chunk)
276279
280+
# With WebSocket options for long-running generations
281+
# Useful if you're generating very long responses that may take >20 seconds
282+
ws_options = WebSocketOptions(keepalive_ping_timeout_seconds=60.0)
283+
with open("output.mp3", "wb") as f:
284+
for audio_chunk in client.tts.stream_websocket(
285+
text_generator(),
286+
ws_options=ws_options
287+
):
288+
f.write(audio_chunk)
289+
277290
# Parameters override config values
278291
config = TTSConfig(format="mp3", latency="balanced")
279292
with open("output.wav", "wb") as f:
@@ -523,13 +536,16 @@ async def stream_websocket(
523536
speed: Speech speed multiplier, e.g. 1.5 for 1.5x speed (overrides config.prosody.speed if provided)
524537
config: TTS configuration (audio settings, voice, model parameters)
525538
model: TTS model to use
539+
ws_options: WebSocket connection options for configuring timeouts, message size limits, etc.
540+
Useful for long-running generations that may exceed default timeout values.
541+
See WebSocketOptions class for available parameters.
526542
527543
Returns:
528544
Async iterator of audio bytes
529545
530546
Example:
531547
```python
532-
from fishaudio import AsyncFishAudio, TTSConfig, ReferenceAudio
548+
from fishaudio import AsyncFishAudio, TTSConfig, ReferenceAudio, WebSocketOptions
533549
534550
client = AsyncFishAudio(api_key="...")
535551
@@ -565,6 +581,16 @@ async def text_generator():
565581
):
566582
await f.write(audio_chunk)
567583
584+
# With WebSocket options for long-running generations
585+
# Useful if you're generating very long responses that may take >20 seconds
586+
ws_options = WebSocketOptions(keepalive_ping_timeout_seconds=60.0)
587+
async with aiofiles.open("output.mp3", "wb") as f:
588+
async for audio_chunk in client.tts.stream_websocket(
589+
text_generator(),
590+
ws_options=ws_options
591+
):
592+
await f.write(audio_chunk)
593+
568594
# Parameters override config values
569595
config = TTSConfig(format="mp3", latency="balanced")
570596
async with aiofiles.open("output.wav", "wb") as f:

0 commit comments

Comments
 (0)