Repository navigation
fix(ai): track failed OpenAI Responses stream events - #1261
marandaneto merged 1 commit into
Conversation
|
Thanks @Artyfowl1710, the fix looks good. One thing before we can merge: four files were converted from LF to CRLF line endings, which is why the diff shows +2091/−1994 instead of about +97/−2. It would rewrite
|
372c613 to
114ceaf
Compare
dustinbyrne
left a comment
There was a problem hiding this comment.
AI-assisted review.
Thanks for fixing the LF line endings and tracking streamed response.failed events across both sync and async OpenAI Responses wrappers. Verified with focused pytest runs and a local sdk-mock-server smoke test that response.failed still reaches callers unchanged while recording $ai_is_error: true, $ai_http_status: 0, $ai_stop_reason: "failed", and privacy-mode message redaction.
One concern before approval (see inline comment on posthog/ai/openai/_streaming.py): wrapping the failure in a bare RuntimeError(message) drops response.error.code, which collapses every privacy-mode response.failed event to bare "RuntimeError" instead of preserving the provider error code as documented in PostHog's privacy-mode contract. I also left one optional test-coverage suggestion on posthog/test/ai/test_stream_midstream_error.py.
Separately, this branch currently has a merge conflict with main following the 8.0 capture-v1 changes (#1016), which added options=options adjacent to error= in posthog/ai/openai/openai.py and posthog/ai/openai/openai_async.py and updated posthog/test/ai/test_stream_midstream_error.py to assert on capture_ai.
Non-blocking follow-ups outside the scope of this PR:
- Streamed
ResponseErrorEvent(type == "error") events are also yielded without raising byopenaiPython SDK and are currently captured as HTTP 200 without$ai_is_error. - Non-streaming
responses.create/responses.parsereturningstatus == "failed"also do not currently set$ai_is_error.
Address privacy-mode review feedback and retain capture-v1 options. Includes sync and async regression coverage. Prepared with OpenAI Codex assistance.
114ceaf to
b6e89f8
Compare
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-pythonBenchmarks completed. No changes flagged for review. 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
An OpenAI Responses stream can end with
response.failedover a successful HTTP connection. The Python SDK forwards the event but previously recorded the generation without$ai_is_error, so dashboards missed these failures.Both sync and async Responses wrappers now record the terminal event through the existing streaming error path while forwarding it unchanged to callers. A private error type retains the provider code for the shared privacy formatter: safe codes such as
server_errorandrate_limit_exceededremain available while free-text messages and unsafe code values are redacted. Missing or blank messages use a fallback, and exceptions raised during iteration retain precedence.The branch is updated to current
mainand preserves capture-v1 options andcapture_airouting, consistent with the Capture AI SDK spec. Error formatting follows the privacy-mode contract. No public API changes.💚 How did you test it?
$ai_errorwasRuntimeErrorand lost the structured provider code.httpx.MockTransportand HTTP 200 SSE responses. Both wrappers cover safe provider codes, missing/empty/whitespace messages, unsafe code redaction, unchanged events, privacy mode and capture options. Separate tests verify raised-exception precedence and identity.pytest -q -p no:cacheprovider --timeout=30 posthog/test/ai/openai posthog/test/ai/test_stream_midstream_error.py -k "openai or capture_streaming_event"— 254 passed, 3 skipped, 11 deselected.uv.lock: repository-wide lint and format checks passed.mypy --no-site-packages --config-file mypy.ini . | mypy-baseline filterpassed across 356 source files.python -W error -c "import posthog"passed.main.📝 Checklist
A patch changeset is included for
pypi/posthog.Agent context
Autonomy: Fully autonomous. OpenAI Codex prepared the change, addressed review feedback, resolved conflicts and ran the checks above. The commit is signed by GitHub. Tests use mocked provider HTTP responses; no live provider credentials or calls are required.