Skip to content

fix(ai): track failed OpenAI Responses stream events - #1261

Merged
marandaneto merged 1 commit into
PostHog:mainfrom
Artyfowl1710:codex/openai-failed-response-telemetry
Nov 25, 2026
Merged

marandaneto merged 1 commit into
PostHog:mainfrom
Artyfowl1710:codex/openai-failed-response-telemetry

Conversation

@Artyfowl1710

@Artyfowl1710 Artyfowl1710 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

An OpenAI Responses stream can end with response.failed over 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_error and rate_limit_exceeded remain 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 main and preserves capture-v1 options and capture_ai routing, consistent with the Capture AI SDK spec. Error formatting follows the privacy-mode contract. No public API changes.

💚 How did you test it?

  • Reproduced the privacy-mode regression before the fix: $ai_error was RuntimeError and lost the structured provider code.
  • Regression tests use the OpenAI SDK's streaming HTTP parser with httpx.MockTransport and 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.
  • On Ubuntu with Python 3.12 and OpenAI 2.54.0: 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.
  • Ruff 0.12.2, matching uv.lock: repository-wide lint and format checks passed.
  • mypy --no-site-packages --config-file mypy.ini . | mypy-baseline filter passed across 356 source files.
  • python -W error -c "import posthog" passed.
  • Verified LF line endings in all changed files and a clean diff against main.

📝 Checklist

  • All commits are signed.
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change.

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.

@Artyfowl1710
Artyfowl1710 requested a review from a team as a code owner November 16, 2026 13:29
@turnipdabeets

Copy link
Copy Markdown
Contributor

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 git blame for those files and conflict with other open PRs touching them. Could you convert them back to LF?

  • posthog/ai/openai/_streaming.py
  • posthog/ai/openai/openai.py
  • posthog/ai/openai/openai_async.py
  • posthog/test/ai/test_stream_midstream_error.py

@dustinbyrne dustinbyrne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 by openai Python SDK and are currently captured as HTTP 200 without $ai_is_error.
  • Non-streaming responses.create / responses.parse returning status == "failed" also do not currently set $ai_is_error.

Comment thread posthog/ai/openai/_streaming.py Resolved
@dustinbyrne
dustinbyrne requested a review from a team November 23, 2026 23:26
Address privacy-mode review feedback and retain capture-v1 options. Includes sync and async regression coverage. Prepared with OpenAI Codex assistance.
@Artyfowl1710
Artyfowl1710 force-pushed the codex/openai-failed-response-telemetry branch from 114ceaf to b6e89f8 Compare November 24, 2026 13:58

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good; no qualifying findings. Verdict: correct.

Thank you for your contribution!

@github-actions

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks 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
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 133.67 KiB/op 133.67 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,345.53 count/op 1,345.53 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 138.5 µs/op 144.66 µs/op 4.5% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 37.77 MiB 37.76 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 40.22 MiB 40.21 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 40.22 MiB 40.21 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 138.48 µs/op 146.71 µs/op 5.9% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 40.5 MiB 40.5 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 185.75 KiB/op 185.76 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,337.53 count/op 1,337.57 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 170.37 µs/op 172.26 µs/op 1.1% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.37 MiB 41.37 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 47.16 MiB 47.15 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 47.16 MiB 47.15 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 161.2 µs/op 163.36 µs/op 1.3% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 51.59 MiB 51.62 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 138.74 KiB/op 138.74 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,390.42 count/op 1,390.42 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 159.45 µs/op 156.23 µs/op 2% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 37.71 MiB 37.65 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 37.89 MiB 37.81 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 37.89 MiB 37.81 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 155.57 µs/op 151.35 µs/op 2.7% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 40.5 MiB 40.5 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 190.92 KiB/op 190.92 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,382.38 count/op 1,382.38 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 180.97 µs/op 192.97 µs/op 6.6% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 41.22 MiB 41.22 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 41.68 MiB 41.66 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 41.28 MiB 41.27 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 170.93 µs/op 184.66 µs/op 8% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.58 MiB 41.54 MiB <0.1% less memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 106.27 µs/op 110.28 µs/op 3.8% more time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 110.64 µs/op 112.24 µs/op 1.4% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 113.86 µs/op 109.81 µs/op 3.6% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 114.51 µs/op 111.55 µs/op 2.6% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 225.43 KiB/op 225.43 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,279.42 count/op 2,279.42 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 120.44 µs/op 120.46 µs/op <0.1% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 120.44 µs/op 120.46 µs/op <0.1% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,413.69 count/op 22,413.69 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 1.17 ms/op 1.17 ms/op 0.3% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 1.17 ms/op 1.17 ms/op 0.3% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.63 MiB/op 21.63 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 223,655.44 count/op 223,655.44 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 11.77 ms/op 11.81 ms/op 0.4% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 11.77 ms/op 11.81 ms/op 0.4% more time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 43.81 MiB 43.93 MiB 0.3% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 44.13 MiB 44.21 MiB 0.2% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 44.13 MiB 44.21 MiB 0.2% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 43.88 MiB 43.87 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 42.96 MiB 42.95 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 42.97 MiB 42.96 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 42.97 MiB 42.96 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 43.01 MiB 42.8 MiB 0.5% less memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 15.74 KiB/op 15.74 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 162.77 count/op 162.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 14.1 µs/op 14.17 µs/op 0.4% more time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 14.1 µs/op 14.17 µs/op 0.4% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 24.6 KiB/op 24.6 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 239.28 count/op 239.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 22.5 µs/op 22.61 µs/op 0.5% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 22.5 µs/op 22.61 µs/op 0.5% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 39.81 KiB/op 39.81 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 388.27 count/op 388.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 28.21 µs/op 28.24 µs/op 0.1% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 28.2 µs/op 28.24 µs/op 0.1% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 60.59 KiB/op 60.59 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 584.27 count/op 584.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 39.51 µs/op 39.47 µs/op 0.1% less time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 39.51 µs/op 39.46 µs/op 0.1% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 59.22 KiB/op 59.22 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 567.77 count/op 567.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 35.82 µs/op 35.63 µs/op 0.5% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 35.82 µs/op 35.63 µs/op 0.5% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.04 KiB/op 19.04 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 192.27 count/op 192.27 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 16.99 µs/op 16.79 µs/op 1.1% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 16.99 µs/op 16.79 µs/op 1.1% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 20.66 KiB/op 20.66 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 197.77 count/op 197.77 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 17.91 µs/op 18.14 µs/op 1.2% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 17.91 µs/op 18.14 µs/op 1.2% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 14.89 KiB/op 14.89 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 152.77 count/op 152.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 13.38 µs/op 13.21 µs/op 1.3% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 13.38 µs/op 13.21 µs/op 1.3% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 15.9 KiB/op 15.9 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 166.77 count/op 166.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 14.94 µs/op 14.87 µs/op 0.5% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 14.94 µs/op 14.87 µs/op 0.5% less time Not flagged

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 46a88b5c3701f2c9070284e2bd398c06f037bcdc → b6e89f83d6426a9187d45de29f326bbad8ffc6bc.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

@marandaneto
marandaneto merged commit 71d0e2a into PostHog:main Nov 25, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants