Repository navigation
fix(ai): capture error event when streaming request fails to open - #1213
Closed
pcbeingused333 wants to merge 1 commit into
Closed
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
OpenAI chat/responses and Anthropic create(stream=True) called the provider outside the tracked generator, so a 401/429/5xx raised while opening the stream was never reported. Capture an error event (with the HTTP status) before re-raising. Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
Contributor
Author
|
Closing: main already includes the same fix via #1146, so this PR is now a duplicate. Thanks! |
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.
Related to #1188 and #1110 (same family: provider errors not reaching
$ai_generation).💡 Motivation and Context
create(stream=True)in the OpenAI (chat completions, responses; sync and async) and Anthropic (sync and async) wrappers called the provider outside the tracked generator. A request that fails while opening the stream (401, 429, 5xx) raised straight to the caller and no event was captured; only mid-stream errors were reported, and the non-streaming path (call_llm_and_track_usage) already captures open-time errors.Changes:
_create_streaming; on failure, capture the existing streaming event with the error (so$ai_is_error,$ai_errorand$ai_http_statusare set) and re-raise.💚 How did you test it?
Added six tests to
test_stream_midstream_error.py(chat, responses, Anthropic; sync and async) where the providercreateraises a 429-style error: they fail onmain(no event) and pass with the change.pytest posthog/test/ai/openai posthog/test/ai/anthropic posthog/test/ai/test_stream_midstream_error.py posthog/test/ai/test_async_stream_wrapper.pypasses (196 passed, 2 skipped).ruff format --checkpasses on the touched files.📝 Checklist
If releasing new changes
sampo addto generate a changeset file (.sampo/changesets/ai-stream-open-error.md, patch)🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Prepared with Claude Code. Scope is
create(stream=True)only. Two related paths are left out to keep the diff small: Anthropicmessages.stream()still captures nothing when the request fails to open (checked against a mocked 429), and from reading the code the async Gemini wrapper awaits the request outside its generator, so it likely has the same gap. Sync Gemini was not touched (its stream is a lazy generator).