Repository navigation
feat(websocket): accept additional handshake headers in WebSocketOptions - #186
Conversation
WebSocketOptions.additional_headers adds headers to the handshake of tts.stream_websocket and asr.stream_websocket, sync and async, so a caller without OpenTelemetry can send a traceparent on a single WebSocket call as RequestOptions already allows on HTTP. The SDK's own Authorization, User-Agent and model headers are kept whatever the casing of a name given. The README gains a Tracing section: opentelemetry-instrumentation-httpx covers HTTP and both handshakes; without it, use the per-call options or an httpx_client with a request event hook.
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 2 files with indirect coverage changes 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported, and the changes include targeted coverage for custom WebSocket headers, precedence, immutability, and tracing across synchronous and asynchronous paths.
No open issues 🎉
What changed in this PR
Adds per-call custom headers to WebSocketOptions, enabling tracing and other caller-supplied metadata on synchronous and asynchronous TTS and ASR WebSocket handshakes while preserving SDK-managed header precedence.
Changes:
- Adds and documents WebSocketOptions.additional_headers with case-insensitive SDK header precedence.
- Adds WebSocket header, tracing, and request-hook integration coverage across HTTP and WebSocket paths.
- Updates tracing documentation and development dependencies.
| File | Description |
|---|---|
| uv.lock | Locks the updated SDK metadata and new OpenTelemetry development dependencies. |
| tests/unit/test_websocket_headers.py | Tests WebSocket header delivery, immutability, SDK header precedence, and default behavior. |
| tests/unit/test_tracing.py | Tests OpenTelemetry and request-hook trace propagation across HTTP and WebSocket calls. |
| tests/unit/realtime_server.py | Extends the local test server to record handshake headers and support WebSocket path testing. |
| src/fishaudio/resources/tts.py | Applies additional headers to synchronous and asynchronous TTS WebSocket handshakes. |
| src/fishaudio/resources/asr.py | Applies additional headers to synchronous and asynchronous ASR WebSocket handshakes. |
| src/fishaudio/core/websocket_options.py | Adds and documents the per-call additional_headers WebSocket option. |
| README.md | Documents OpenTelemetry, explicit traceparent, and httpx event-hook tracing approaches. |
| pyproject.toml | Adds OpenTelemetry instrumentation and SDK packages as development dependencies. |
Review effort: Lite
…s own A User-Agent given in WebSocketOptions.additional_headers was replaced by the SDK's. It is now appended, as on HTTP calls, on all four handshakes.
…eeds The README's event-hook client had httpx's 5 s timeout. The tracing note now says a tracer provider is needed and when a hand-set traceparent is replaced; tests pin both. The handshake helper is private, and the test server's comments say what it records.
…re merged An Authorization in WebSocketOptions.additional_headers now replaces the API key on the handshake, as one in RequestOptions does on an HTTP call. The handshake's model stays the method's argument. One on a supplied httpx client still does not replace the key. additional_headers is new in this branch, so nothing changes for existing callers.
get_headers() replaced the SDK's Authorization with the caller's but kept
every spelling the caller gave, so {"Authorization": a, "authorization": b}
was sent as two Authorization headers on HTTP calls and on the WebSocket
handshake. The last one given is now sent, as for User-Agent.
Codecov's patch check counted the query-params block of _prepare_request_kwargs as new lines once main had moved it; no test had ever run it. Cover it with and without method params.
Replaces #184. That design added a second propagation mechanism inside the SDK, with its own rules, defaults and warning. This one relies on the caller's httpx client and instrumentation, which already reach every path, and adds the one missing per-call input.
Problem
On main, a caller can add a header to one HTTP call (
RequestOptions(additional_headers=...)) but not to one WebSocket call:WebSocketOptionshas no header input, andWebSocketOptions(additional_headers=...)raisesTypeError. Without OpenTelemetry, the only way to send atraceparenton thetts.stream_websocketorasr.stream_websockethandshake is a customhttpx_client. Withopentelemetry-instrumentation-httpxand a tracer provider configured, HTTP calls and both handshakes already carry the trace on main; the tracing tests that do not use the new option pass there.Change
WebSocketOptions(additional_headers: Optional[dict[str, str]] = None). The headers are sent on the handshake oftts.stream_websocketandasr.stream_websocket, sync and async.RequestOptions.additional_headersare on an HTTP call: anAuthorizationgiven here, in any casing, replaces the API key and is sent once; aUser-Agentis appended to the SDK's own and sent once, the last one when two spellings are given.modelis always the method's argument; amodelgiven here, in any casing, is ignored. AnAuthorizationon a suppliedhttpx_clientdoes not replace the key. The caller's dict is not modified, and the field is not passed to httpx-ws.WebSocketOptionsdocstring updated.Public interface
additional_headersonWebSocketOptions. Nothing else: no new class, function, export or exception, and no signature change onstream_websocket.Behaviour changes
Authorization,model,User-Agentand what httpx and httpx-ws addWebSocketOptions(additional_headers=...)TypeErrorOne rule in the three SDKs
A per-call
Authorizationreplaces the API key on every transport: on an HTTP call (as onmain) and now on the WebSocket handshakes. A client-level one (here: on a suppliedhttpx_client) does not.modelon a handshake is the method's argument. typescript#20 and go#26 follow the same rule. The per-call handshake input is new in this PR, so nothing changes for existing callers.Carrying a trace
Use one way. With OpenTelemetry, configure a tracer provider and enable
opentelemetry-instrumentation-httpx: HTTP calls and both handshakes carry the current trace id, under a client span that the instrumentation creates. A WebSocket stream connects on its first iteration, so iterate it inside the span. Without OpenTelemetry, passtraceparentinRequestOptionson HTTP calls and inWebSocketOptions.additional_headerson WebSocket calls, or give the client anhttpx_clientwith a request event hook; a client you pass in keeps its own settings, so the README's example sets its timeout. Atraceparentthat the instrumentation sends replaces one set by hand; with no tracer provider it sends none and the hand-set one goes out as it is.Not in this change
No header provider, no default OpenTelemetry behaviour, no runtime dependency, no validation, no warning, no new exception. A header value httpx rejects raises httpx's own error.
fish_audio_sdkis unchanged.Tests
All against the local WebSocket and HTTP test servers. The WebSocket server records the handshake's headers as its parser reports them: names in lower case, in order, repeated names kept.
test_websocket_headers.py(each on the four WebSocket paths): the headers are sent and the dict is not changed; anAUTHORIZATIONreplaces the API key and is sent once; aModeldoes not replace the method's model; anAuthorizationon a suppliedhttpx_clientdoes not replace the key; auser-agentis appended to the SDK's, the last of two spellings when both are given; default options send the same header names as no options; the default handshake's header names in order, and the values ofauthorization,modelanduser-agent, are written out.test_tracing.py, with the realopentelemetry-instrumentation-httpx(dev dependency only) and a tracer provider: under an active span,tts.convert,asr.transcribe(sync and async) and the four handshakes each carry exactly onetraceparentwith the span's trace id; the four handshakes andtts.convertare checked to be sent under a span of their own; a stream created inside the span but iterated after it ends gets a different trace id; a hand-settraceparentis replaced on an HTTP call and on the four handshakes, and without a tracer provider it is sent as it is; a request event hook (async hook for the async client) adds its header to the HTTP calls and both handshakes.poe checkandpoe testpass withmainmerged in.Live run against production
Sync and async, each of
tts.convert,asr.transcribe,tts.stream_websocketandasr.stream_websocketreturnedx-fish-trace-idequal to the expected trace id: (a) withopentelemetry-instrumentation-httpxand an active span, (b) without OpenTelemetry, withRequestOptionsandWebSocketOptions(additional_headers={"traceparent": ...}), (c) with the README's event-hook client. 24 of 24 matched.Summary by CodeRabbit