Skip to content

fix(anthropic): preserve caller errors in streaming responses - #397

Open
alexliluz wants to merge 8 commits into
open-telemetry:mainfrom
alexliluz:alexliluz/fix-anthropic-streaming-response-exception
Open

fix(anthropic): preserve caller errors in streaming responses#397
alexliluz wants to merge 8 commits into
open-telemetry:mainfrom
alexliluz:alexliluz/fix-anthropic-streaming-response-exception

Conversation

@alexliluz

Copy link
Copy Markdown

Fixes #389

Anthropic's with_streaming_response context 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.

@alexliluz
alexliluz requested a review from a team as a code owner August 16, 2026 13:40
Copilot AI lite review requested due to automatic review settings August 16, 2026 13:40
@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 16, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 16, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-08-22 16:12 UTC

Resolve merge conflicts.

Investigate required status check failures.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 / AsyncMessagesWithStreamingResponse to dynamically wrap their create methods 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.

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
alexliluz force-pushed the alexliluz/fix-anthropic-streaming-response-exception branch from 72cebda to 6361e42 Compare August 19, 2026 11:53
alexliluz and others added 4 commits August 19, 2026 20:04
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

anthropic: caller exception inside a with_streaming_response block records a successful span

4 participants