Repository navigation
fix(ai): capture initial request errors in Anthropic messages.stream() - #1249
Conversation
messages.stream() wrapped the manager's request without a try/except, so a failed initial request (401, connection error) raised without any $ai_generation error event, unlike create(stream=True). Capture the error in both sync and async clients and re-raise.
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 |
Follow-up to #1146 (same gap on the
messages.stream()path):messages.stream()did not report failures of the initial request.Cause.
Messages.stream()(sync) andAsyncMessages.stream()wrap the native manager's__api_requestwithtracked_request, which calledrequest()with no error handling. If the request fails (401, connection error, 4xx/5xx), the exception propagated with no$ai_generationevent, whereascreate(stream=True)(_create_streaming) already captures it witherror=exc.Fix.
tracked_requestnow catches the exception, captures the event with$ai_is_error/$ai_errorthrough the same_capture_streaming_event(..., error=exc)path, guarded so telemetry can never replace the provider's error, and re-raises. Same change in the async client. Includes a.sampochangeset.Tests. Added sync and async tests that make
client.postraise and assert one captured event with$ai_is_errortrue and the error text; both fail on main and pass now. Fullposthog/test/ai/anthropicpasses; ruff 0.11.12 check/format clean.This PR was prepared with AI assistance (Claude Code) and submitted by the account owner.