chore(llmobs): dispatch stream.created from the Anthropic and OpenAI SDK streams #22128
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 481 lines diff across 6 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.370
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #22128"
general
weight: 0.35
avara1986
Description
Jira: APPSEC-71908
The Anthropic and OpenAI SDKs build every streamed response as a
StreamorAsyncStream. Every consumer reads its events from that object's event iterator (_iterator): the caller ofcreate(stream=True), the SDK message accumulators (MessageStream,ChatCompletionStream, with their snapshots and final messages), the tool runners, and the streams returned by.parse()ofwith_raw_responseandwith_streaming_response.This PR adds a product-neutral core event on that event source, so that a product can see or replace the events before any SDK consumer reads them:
ddtrace/contrib/internal/_sdk_streams.py:traced_stream_initwrapsStream.__init__/AsyncStream.__init__and dispatchesanthropic.stream.createdoropenai.stream.createdwith aStreamEventscarrier. The carrier holds the httpx response of the stream (whose request is the one the SDK sent), the event iterator, and whether the stream is async. The contrib installs the iterator that a listener leaves on the carrier.- Older SDKs set
_iteratorin the constructor. Newer ones make it on first use as a cached property, from a copy of the stream (anthropic 2.x). For those, the carrier holds a lazy iterator that makes the SDK one on the first read, after every subclass constructor has run. For example,BedrockStreamsets its AWS decoder aftersuper().__init__(). The lazy iterator refers to the stream through a weak reference, so the replaced iterator does not keep the stream alive.- When no product listens, the hook only calls
core.has_listeners.patch()functions wrap and unwrap the two constructors.Nothing listens to these events yet. #22020 uses them to move AI Guard's stream evaluation below the SDK accumulators.
Testing
tests/contrib/anthropic/test_anthropic_stream_hook.py, with a mocked transport:- a listener that replaces the events is seen by
create(stream=True)and by the sync and asyncmessages.stream()accumulators;- streams are unchanged without a listener;
- a
Streamsubclass that sets attributes aftersuper().__init__()keeps them.tests/contrib/openai/test_openai_stream_hook.py: the same for sync and async Chat Completions streams andwith_streaming_response(...).parse(), plus streams unchanged without a listener.- anthropic 0.28, <1, 1.x and 2.x;
- openai 1.0.0, <2, 3.26 and latest.
ddtrace/contrib/internal/_sdk_streams.pyis added to thellmobssuitespec component, so changes to it select the Anthropic and OpenAI suites.Risks
_iteratorattribute ofStream/AsyncStream: an instance attribute in older SDKs, and a cached property in newer ones. It skips a stream that has neither.core.has_listenerslookup per stream.Additional Notes
benchmarks/covers the Anthropic or OpenAI SDKs, so this path has no benchmark coverage. Measured directly instead, withtimeitonStream(...)construction and no listener: about +0.6-0.75 µs per streamed response (anthropic 2.8.0 on Python 3.15: ~450 ns to ~1040-1200 ns; anthropic 0.28.0 on Python 3.9: ~750 ns to ~1300-1470 ns). The cost is once per stream, not per chunk.🤖 Generated with Claude Code
[APPSEC-71908]: https://datadoghq.atlassian.net/browse/APPSEC-71908?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ