fishaudio.core.websocket_options
WebSocket-level options for WebSocket connections.WebSocketOptions Objects
additional_headers are passed directly to httpx_ws’s
connect_ws/aconnect_ws functions. For complete documentation, see
https://frankie567.github.io/httpx-ws/reference/httpx_ws/
Attributes:
keepalive_ping_timeout_seconds- Maximum delay the client will wait for an answer to its Ping event. If the delay is exceeded, WebSocketNetworkError will be raised and the connection closed. Default: 20 seconds.keepalive_ping_interval_seconds- Interval at which the client will automatically send a Ping event to keep the connection alive. Set to None to disable this mechanism. Default: 20 seconds.max_message_size_bytes- Message size in bytes to receive from the server.Default- 65536 bytes (64 KiB).queue_size- Size of the queue where received messages will be held until they are consumed. If the queue is full, the client will stop receiving messages from the server until the queue has room available. Default: 512.additional_headers- Extra headers to send on the WebSocket handshake, for example{"traceparent": ...}. They are merged asRequestOptions.additional_headersare on an HTTP call: anAuthorizationhere replaces the API key and aUser-Agentis appended to the SDK’s own, in any casing; each is sent once, and with two spellings the last one given is used.modelis always the method’s argument. The dict is not modified.
to_httpx_ws_kwargs
fishaudio.core._sse
Minimal Server-Sent Events decoder.ServerSentEvent Objects
SSEDecoder Objects
feed(); each call returns the events that
were completed by those bytes. Lines end with CRLF, LF or CR, the three
endings the SSE specification allows; other Unicode line separators stay
inside the payload. An event left unterminated when the stream ends is
dropped, as the specification requires.
feed
fishaudio.core.omit
OMIT sentinel for distinguishing None from not-provided parameters.fishaudio.core.client_wrapper
HTTP client wrapper for managing requests and authentication.PRODUCT
The SDK’s product,fish-audio-<language>/<version>: the format every Fish
Audio SDK sends, so the API can tell which SDK and version made a request.
build_user_agent
fish-audio-python/<version> (python <python version>; <os>).
Only the runtime, its version and the OS family are included: nothing
that identifies a person or machine.
USER_AGENT
The User-Agent the SDK sends, computed once at import, e.g.fish-audio-python/1.6.0 (python 3.12.1; linux).
user_agent
caller if set.
A caller’s value is appended after the SDK’s product and runtime comment
rather than allowed to replace them, so an app can name itself without
hiding which SDK made the request. A value that already starts with the
SDK’s User-Agent is not doubled.
BaseClientWrapper Objects
get_headers
additional_headers replaces the SDK’s header of that
name whatever its case, so the two are never sent side by side. The
exception is User-Agent: one in additional_headers (any
casing), or else one set on a user-supplied httpx client, is appended
to the SDK’s own. Authorization and User-Agent are each sent
once: given in two spellings, the last one given is used.
ClientWrapper Objects
request
method- HTTP method (GET, POST, etc.)path- API endpoint pathrequest_options- Optional request-level overridesstream- If True, return as soon as the response headers arrive and leave the body unread. The caller must consume it withiter_bytes()and then callclose()on the response.**kwargs- Additional arguments to pass to httpx.request
APIError- On non-2xx responses
client
close
AsyncClientWrapper Objects
request
method- HTTP method (GET, POST, etc.)path- API endpoint pathrequest_options- Optional request-level overridesstream- If True, return as soon as the response headers arrive and leave the body unread. The caller must consume it withaiter_bytes()and then callaclose()on the response.**kwargs- Additional arguments to pass to httpx.request
APIError- On non-2xx responses
client
close
fishaudio.core.request_options
Request-level options for API calls.RequestOptions Objects
timeout- Override the client’s default timeout (in seconds)max_retries- Override the client’s default max retriesadditional_headers- Additional headers to include in the requestadditional_query_params- Additional query parameters to include
get_timeout
fishaudio.core.iterators
Audio stream wrappers with collection utilities.AudioStream Objects
.collect() method to gather all chunks into a single bytes object.
Examples:
__init__
iterator- The underlying iterator of audio byteson_close- Called by close() to release the underlying resource, for example the open HTTP responseheaders- Headers of the HTTP response that opened the stream
__iter__
close
collect
AsyncAudioStream Objects
.collect() method to gather all chunks into a single bytes object.
Examples:
__init__
async_iterator- The underlying async iterator of audio byteson_close- Awaited by aclose() to release the underlying resource, for example the open HTTP responseheaders- Headers of the HTTP response that opened the stream
__aiter__
aclose
collect
_Timeline Objects
duration
TTSTimestampStream Objects
TTSTimestampEvent objects as the server produces them.
While you iterate, segments holds the timings received so far, measured
from the start of the complete audio. Alternatively, call collect()
instead of iterating to get the complete audio with its final timings.
Examples:
__init__
iterator- The underlying iterator of eventson_close- Called by close() to release the underlying resource, for example the open HTTP responseheaders- Headers of the HTTP response that opened the stream
segments
close
collect
segments when the stream ends.
Returns:
TTSTimestampResult with the complete audio and final timings. When
the API has no timings for the text, segments is empty and
duration is None; the audio is still complete.
Raises:
RuntimeError- If iteration has already started
AsyncTTSTimestampStream Objects
TTSTimestampEvent objects as the server produces them.
While you iterate, segments holds the timings received so far, measured
from the start of the complete audio. Alternatively, call collect()
instead of iterating to get the complete audio with its final timings.
Examples:
__init__
async_iterator- The underlying async iterator of eventson_close- Awaited by aclose() to release the underlying resource, for example the open HTTP responseheaders- Headers of the HTTP response that opened the stream
segments
aclose
collect
segments when the stream ends.
Returns:
TTSTimestampResult with the complete audio and final timings. When
the API has no timings for the text, segments is empty and
duration is None; the audio is still complete.
Raises:
RuntimeError- If iteration has already started
fishaudio.core._ws_utils
Internal helpers for synchronous httpx-ws sessions.interrupt_writes
drain_reader
close() frees the fd.
Why this is needed (Linux only in practice):
httpx-ws’s sync WebSocketSession reads on a background thread and closes the
socket from the calling thread. A read that has not started yet is harmless:
CPython’s SSLSocket._real_close sets _sslobj = None before closing the
fd, so a later read falls through to socket.recv on fd -1 and fails with
EBADF. The problem is the read that is already in flight. On Linux,
close() does not interrupt a thread blocked in recv(). OpenSSL (with
read_ahead off) fetches a TLS record header and body with separate read(fd)
calls on the fd number cached in its BIO, so when the server’s close-frame reply
arrives the second read() can land on a brand-new connection that has since
reused the same fd number, and that new session fails with
[SSL: WRONG_VERSION_NUMBER] or a similar TLS error. macOS wakes the blocked
reader with EBADF on close(), so it is unaffected.
The fix is to end the in-flight read before the fd is freed: send the close
frame (so the server still sees a clean 1000 close rather than 1006; a peer
that has stopped reading does not get one, see below), then
shutdown(SHUT_RDWR) the socket, which wakes the blocked reader with EOF, and
join the reader thread. The later close() sees LOCAL_CLOSING and skips
sending a second frame.
Residual risk: if httpx-ws has already closed the socket on one of its own error
paths (keepalive ping timeout, or a sender WriteError triggering close()),
the fd is already freed, shutdown() fails with EBADF, and the in-flight
read can still race. That can only be fixed inside httpx-ws’s close().
