← Back to Pull requests

fix(anthropic): stop retaining snapshot events from messages.stream() #21667

Open
unknown opened this pull request with 293 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 11, 2026 04:58 UTC
Author

Description

Fixes #21666

On the messages.stream() / beta.messages.stream() path the SDK yields derived events (text, input_json, thinking, signature, citation, ...) next to the raw stream events. The derived ones carry the cumulative snapshot of the output so far. AnthropicStreamHandler stored every event until the stream finished, so retained memory grew quadratically with the response length. _construct_message only reads the raw event types and ignored the rest anyway.

The handlers now keep only chunks whose type _construct_message handles. The transformation table moves from inside _extract_from_chunk (it was rebuilt on every chunk) to a module-level _CHUNK_TRANSFORMATIONS, and the filter uses its keys, so the two stay in sync. Sync and async handlers share one _retain_chunk helper.

Same mocked stream as in the issue (3000 deltas of 10 chars, anthropic 2.7): before, 6005 chunks retained and 51 MB traced at the end of the stream; after, 3004 chunks and 4.7 MB.

Testing

New tests in tests/contrib/anthropic/test_anthropic_llmobs.py on the existing cassettes:

  • sync text stream helper

  • async tools stream helper (input_json)

  • sync thinking stream helper
  • Each iterates the events and checks that the derived types were yielded to the caller but not retained, that raw content_block_delta events were retained, and that the LLM Observability output (text, tool call arguments, reasoning) is unchanged. All three fail on main.

    test_anthropic.py and test_anthropic_llmobs.py pass against the latest (95857972cb3e, anthropic 0.117) and oldest (65dc301ce378, anthropic 0.28) locks. scripts/lint style, typing, spelling, security and suitespec checks pass on the changed files.

    Risks

    Low. Only events that _construct_message ignored are dropped, so span output and token counts don't change. If a future handler is added to _CHUNK_TRANSFORMATIONS, its events are retained automatically.

    Additional Notes

    The handler still keeps the raw events (one per token) until the end of the stream. Folding each event into the message as it arrives would remove that too, but it's a bigger change to the handler, so I kept this one minimal. When AI Guard stream analysis is enabled, its own buffer still holds all events. That is a separate code path and isn't changed here.

    Diff: 293 lines (+219 / -74) across 3 files Reviewer: mansur-akhmadi Review duration: 29108m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.40) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 293 lines diff across 3 files
    0.40
    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%
    42%
    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
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.340
    Real PR Conversation Timeline
    1 comments
    "Review active on #21667"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% 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: 39.9% (MEDIUM) · Higher = larger blast radius
    39.9%
    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: 43.0% · Lower = rushed or shallow review
    43.0%
    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: 22.8% · Safe to merge
    22.8%