Repository navigation
fix(ai): record an Anthropic stream error event's status, not 200 - #1253
Conversation
… of 200 Anthropic sends errors that happen after a stream starts, such as overloaded_error, as an SSE error event, and the anthropic SDK raises them with the stream response's 200 status. Map the error type to its documented HTTP status (0 when unknown) so these failures are not recorded as a 200.
marandaneto
left a comment
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
Thank you for your contribution!
SDK performance: posthog-python2 changes worth reviewing: 2 observed regressions. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.
View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Each shard uses its own runner; compare Before PR against With PR within a row, not across shards. Compared 102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in |
Motivation and Context
When an Anthropic stream fails after it has started (
overloaded_errorunder load is the common case), the API sends an SSEerrorevent and the anthropic SDK raisesAPIStatusErrorwith the stream response's status, 200. Both the native wrapper and the LangChain callback then capture$ai_is_error: truewith$ai_http_status: 200, so filters and alerts on 529 or 429 miss these failures.test_stream_midstream_error.pyalready says a mid-stream error must not be recorded as 200, but its fake error carries 529, which the real SDK never sends._get_http_status(inai/utils.pyand the LangChain callback) now maps a below-400 status on such an error to the status its documented error type stands for (529 foroverloaded_error, 429 forrate_limit_error, and so on), or 0 for an unknown type. Errors that carry a real 4xx/5xx status are unchanged.How did you test it?
New cases in
test_http_status.pybuild the error the real SDK raises for anerrorevent; 4 of them fail on main (200 instead of 529/429/500/0) and pass with the fix.More
Anthropicwrapper (create(stream=True)) and realChatAnthropic.stream(anthropic 1.13.0, langchain-anthropic 1.7.6) over a mock HTTP transport sendingmessage_startthen anoverloaded_errorevent: main records$ai_http_status200 on both, the fix 529.posthog/test/ai: 1612 pass; the same 2 Gemini tests (test_new_client_basic_generation, sync and async) fail on main too. ruff 0.12.2 format and check clean. Not run: mypy, Linux.Checklist
Agent context
Autonomy: Human-driven (agent-assisted). Claude Code (Claude Opus 5.5) found the bug by driving the real SDK with a stream that errors after
message_start, wrote the fix and tests, and ran the checks above.