Repository navigation
fix(anthropic): end the span when a streaming messages.create call fails - #991
alexcastgonz (pcbeingused333) wants to merge 1 commit into
Conversation
The sync Messages.create(stream=True) path started the span and then called the SDK with no error handling. When the request itself failed (for example a 4xx or a connection error), the span was never ended and the error was never logged, so the failed call showed up as an unfinished span without an error. The async client already logs the error and ends the span in that case. The sync path now does the same. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Codex Review SummaryThis 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.
these cassettes are not valid
status:
code: 401
message: Unauthorized
|
Thanks for the look. I'm not sure which part is invalid, so here is what the cassettes are and the options. Both files record one real call to If the objection is the 401 itself: Which would you prefer?
I'll update the PR once you pick one. |
|
No I want an actual cassette recording, the agentscope cassette is wrong and needs to be updated. I'll take care of that. |
Messages.create(stream=True)on the sync wrapped Anthropic client goes through__trace_stream, which starts theanthropic.messages.streamspan and then calls the SDK with no error handling. If the request itself fails (a 4xx from the API, a connection error), the exception propagates and the span is never ended and never gets the error, so the failed call is traced as an unfinished span with no error. The async client's__create_with_stream_truealready logs the error and ends the span in that case, as do the sync and async non-streaming calls.__trace_streamnow wraps the SDK call in the sametry/except: on an exception it logs the error on the span, ends it and re-raises. Successful calls are unchanged.Test:
test_anthropic_sync_create_stream_logs_error_and_ends_spancallsmessages.create(stream=True)on the wrapped client with an invalid API key, so the API answers with a real 401, and checks that the call raisesAuthenticationErrorand that the span has the error. The response is recorded in a cassette (cassettes/latestandcassettes/0.48.0; the repo filter removes thex-api-keyheader and the recording holds no credentials). It fails on main (the span has noerror) and passes with the change. The other tests of the file were not run in the environment used here.This change was made with Claude Code.
🤖 Generated with Claude Code