Repository navigation
Conversation
…SDK streams Wrap Stream.__init__ and AsyncStream.__init__ of both SDKs and dispatch anthropic.stream.created / openai.stream.created with the stream response and its event iterator, so a product can see or replace the events before any SDK consumer reads them. Newer SDKs make the iterator on first use; the hook then hands listeners a lazy iterator, so a subclass constructor still runs first. APPSEC-71908 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codeowners resolved asResolved from the full PR diff against |
Dependency direction analysis
|
🎉 All green!🧪 All tests passed 🔗 Commit SHA: 0f418f8 | Docs | View more details | Give us feedback! |
BenchmarksBenchmark execution time: 2026-11-26 16:23:31 Comparing candidate commit 0f418f8 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 40 metrics, 0 unstable metrics.
|
APPSEC-71908 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eam alive APPSEC-71908 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Notes on the review of this PR:
|
Codex Review Summary: ✅ CompletedThis 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0f418f8e26
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
More details
Runtime compatibility across Anthropic and OpenAI SDK versions remains unverified because SDK dependencies were unavailable and upstream source retrieval failed.
🤖 Bits Code Review · Commit 0f418f8
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._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.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:create(stream=True)and by the sync and asyncmessages.stream()accumulators;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.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