Instrument OpenAI Responses WebSocket mode #2912
All Vouch review confidence checks passed
Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 423 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.362
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #2912"
general
weight: 0.35
amal-antony-dev
Closes #1734
logfire.instrument_openai()only wraps HTTP requests, soclient.responses.connect()(Responses API WebSocket mode) wasn't recorded at all. This PR instruments it for sync and async clients, whetherinstrument_openaiis called on a client or on a client class. As with HTTP, instrumenting a class only affects that class and its subclasses.What gets recorded
Each
response.createsent over a connection is recorded with the samestreaming response from {model} took {duration}slog that streamed HTTP Responses calls produce. It has the same attributes: input and output messages, system instructions, request parameters, response id and model, usage and cost. The request attributes come from a helper shared with the HTTP/responsespath, so both stay consistent.Requests are captured from
connection.response.create(...),connection.send(...)(dict or model), andmanager.send(...)before the connection opens.There's no separate request span, unlike HTTP. Over HTTP, the request span only lasts until the headers arrive, and the content goes on the streaming log. Over WebSocket,
response.create()only writes to an open socket and returns immediately, so a span there would be empty. The log's duration runs from sendingresponse.createto the final event. Happy to change this if you'd prefer a span per response.Concurrent responses
One connection can run several responses at once, one per
stream_idlane. Requests on the same lane run in order, and server events carry their lane'sstream_id. Each event is therefore routed to the oldest pending request on its lane.Errors
These are logged at error level with
error.type:errorevent from the serverresponse.failedevent (error.typeis the API's error code)with client.responses.connect() as conn:If
sendraises, the request is removed again, so it doesn't take the next response. Requests that aren't recorded (suppressed instrumentation, or inside an OpenAI Agents span) keep their place in their lane, so their responses aren't assigned to later requests.A connection closed after a response started but before it finished still records what was received. Requests that never received any event are dropped.
Tests
A local
websocketsserver stands in for OpenAI, so no API key or recorded requests are needed. The tests cover:<!-- This is an auto-generated description by cubic. -->
<a href="https://cubic.dev/pr/pydantic/logfire/pull/2912?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->