← Back to Pull requests

Instrument OpenAI Responses WebSocket mode #2912

Open
unknown opened this pull request with 423 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 25, 2026 17:25 UTC
Author

Closes #1734

logfire.instrument_openai() only wraps HTTP requests, so client.responses.connect() (Responses API WebSocket mode) wasn't recorded at all. This PR instruments it for sync and async clients, whether instrument_openai is 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.create sent over a connection is recorded with the same streaming response from {model} took {duration}s log 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 /responses path, so both stay consistent.

Requests are captured from connection.response.create(...), connection.send(...) (dict or model), and manager.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 sending response.create to 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_id lane. Requests on the same lane run in order, and server events carry their lane's stream_id. Each event is therefore routed to the oldest pending request on its lane.

Errors

These are logged at error level with error.type:

  • an error event from the server

  • a response.failed event (error.type is the API's error code)

  • an exception raised inside with client.responses.connect() as conn:
  • If send raises, 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 websockets server stands in for OpenAI, so no API key or recorded requests are needed. The tests cover:

  • sync and async connections

  • several turns on one connection

  • two lanes running at once with interleaved events

  • a request queued on the manager before connecting, answered with an error

  • an exception or an early close mid-response

  • suppressed instrumentation and OpenAI Agents spans

  • instrumenting the classes instead of a client

  • turning the instrumentation off again
  • <!-- 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. -->

    Diff: 423 lines (+317 / -106) across 5 files Reviewer: amal-antony-dev Review duration: 9783m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50% (MEDIUM) · Higher = larger blast radius
    50%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.7% · Safe to merge
    26.7%