fix(anthropic): preserve caller errors in streaming responses - #397
Open
alexliluz wants to merge 8 commits into
Open
fix(anthropic): preserve caller errors in streaming responses#397alexliluz wants to merge 8 commits into
alexliluz wants to merge 8 commits into
Conversation
Pull request dashboard statusWaiting on the author · refreshed 2026-08-22 16:12 UTC Resolve merge conflicts. Investigate required status check failures. Status above doesn't look right?
|
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR addresses a previously known instrumentation gap where caller-side exceptions raised inside Anthropic streaming raw-response context managers were not reaching the raw-response proxy, causing spans to be incorrectly finalized as successful.
Changes:
- Adds sync/async context-manager wrappers that forward caller exceptions to the raw-response proxy before delegating to the Anthropic SDK context manager.
- Instruments
MessagesWithStreamingResponse/AsyncMessagesWithStreamingResponseto dynamically wrap theircreatemethods at construction time. - Unskips existing sync/async tests covering user exceptions in streaming raw-response contexts.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| instrumentation/opentelemetry-instrumentation-genai-anthropic/tests/test_sync_messages.py | Unskips the sync test that asserts spans fail when users raise during streaming raw-response usage. |
| instrumentation/opentelemetry-instrumentation-genai-anthropic/tests/test_async_messages.py | Unskips the async test that asserts spans fail when users raise during streaming raw-response usage. |
| instrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/wrappers.py | Introduces sync/async response context-manager wrappers to preserve caller exceptions. |
| instrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/patch.py | Adds wrapping logic to patch dynamically-created with_streaming_response.create methods. |
| instrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/_raw_response.py | Adds a _fail() hook to finalize spans on caller-side failures. |
| instrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/init.py | Wires the new patch points into instrument/uninstrument flows. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
lmolkova
reviewed
Aug 16, 2026
Forward caller failures through the SDK response context managers while retaining the native SDK types and idempotent stream finalization. Add sync and async regression coverage. Assisted-by: ChatGPT 5.2
alexliluz
force-pushed
the
alexliluz/fix-anthropic-streaming-response-exception
branch
from
August 19, 2026 11:53
72cebda to
6361e42
Compare
Add sync and async regression coverage for preserving the SDK streaming response context manager across instrument and uninstrument cycles.\n\nAssisted-by: ChatGPT 5.2
Assisted-by: ChatGPT 5.2
Assisted-by: ChatGPT 5.2
lmolkova
approved these changes
Aug 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #389
Anthropic's
with_streaming_responsecontext manager closes the raw response without forwarding caller exceptions, so an error raised while consuming a stream is recorded as a successful span. Wrap the dynamically-created sync and async streaming methods and pass the exception to the active raw-response stream wrapper before SDK cleanup.Tests: targeted sync/async regression tests pass on the latest and oldest supported environments; pyright and ruff pass. The full suite still has five pre-existing timeout-classification failures in this environment.
AI disclosure: OpenAI Codex (GPT-5) assisted with repository inspection, implementation, and test execution; the changes and results were manually reviewed.