Skip to content

fix(ai): take $ai_model from the OpenAI stream - #417

Merged
marandaneto merged 1 commit into
PostHog:mainfrom
pcbeingused33:fix/stream-response-model-c8779
Dec 3, 2026
Merged

marandaneto merged 1 commit into
PostHog:mainfrom
pcbeingused33:fix/stream-response-model-c8779

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Follow-up to #389/#392 (stream vs non-stream parity) and #400 (stored prompts). For non-streaming calls, PostHogOpenAIHandler overrides $ai_model with the model the API returns. The streaming path builds its pseudo-response in TrackingStream, which never read the model, so streamed generations only kept the request's model. With the Responses API and a stored prompt, the request has no model, so streamed generations got an empty $ai_model. Chat Completions streams also kept the alias (gpt-4o) instead of the resolved snapshot that the non-streaming path reports.

Fix: TrackingStream now records model from Chat Completions chunks (top level) and from response.model in Responses API events (response.created, response.completed, ...). The completion callback puts it on the pseudo-response, so the existing "model from response" logic in CaptureEvent applies to both paths. If the stream carries no model, the request's model is kept as before. No public API change. Includes a changeset.

💚 How did you test it?

Two new tests in PostHogOpenAIHandlerTests:

  • SendAsyncTakesModelFromResponsesStreamWhenRequestHasNoModel: a /v1/responses stream for a request with a stored prompt and no model; asserts $ai_model is the model from the stream events.
  • SendAsyncTakesModelFromChatCompletionStreamChunks: a /v1/chat/completions stream for gpt-4o whose chunks report gpt-4o-2024-08-06; asserts $ai_model is the snapshot, matching the non-streaming path.

Both fail without the source change. With it, PostHogOpenAIHandlerTests passes 45/45 (dotnet test tests/PostHog.AI.Tests -f net8.0 --filter FullyQualifiedName~PostHogOpenAIHandlerTests), and dotnet format whitespace --verify-no-changes is clean on the touched files.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Prepared with Claude Code (Claude). The tests above were run locally with the .NET SDK rolling forward to net8.0, with no manual testing against the live API.

The non-streaming path overrides $ai_model with the model reported in the
response, but TrackingStream never read it, so streamed calls only kept the
request's model. For a Responses API call that uses a stored prompt the
request has no model at all, so $ai_model was empty. Read `model` from Chat
Completions chunks and from `response.model` in Responses API events.
@pcbeingused33
pcbeingused33 requested a review from a team as a code owner December 3, 2026 12:40

@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

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

SDK performance: posthog-dotnet

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) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Capture and deliver events (4,096-byte payload, burst) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Capture and deliver events (32-byte payload, steady) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Capture and deliver events (4,096-byte payload, steady) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Queue an event (32-byte payload) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Queue an event (4,096-byte payload) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate all 10 flags Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate all 100 flags Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate all 1,000 flags Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate one boolean flag Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a cohort-filtered flag Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a flag (early match) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a flag (late match) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a flag (no match) Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a property-filtered flag Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a regex-filtered flag Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a rollout flag Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Evaluate a multivariate flag Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​; head:​ CLR byte counters do not expose exact object allocation counts; native allocations are not included.​
Capture and deliver events (32-byte payload, burst) Allocated memory per operation 5.37 KiB/op 5.37 KiB/op No observed change Not flagged
Capture and deliver events (32-byte payload, burst) Time per operation 4.35 µs/op 4.39 µs/op 0.9% more time Not flagged
Capture and deliver events (32-byte payload, burst) idle_managed_heap_bytes (estimated) 823.67 KiB 822.48 KiB 0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst) Idle process memory (RSS) 55.05 MiB 55.02 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst) Observed peak process memory (RSS) 57.64 MiB 57.67 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst) Managed heap after settling (estimated) 2.24 MiB 2.24 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst) Process memory after settling (RSS) 57.73 MiB 57.76 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst) Process CPU time per operation 4.38 µs/op 4.43 µs/op 1% more time Not flagged
Capture and deliver events (32-byte payload, burst) Lifetime peak process memory (RSS) 57.73 MiB 57.76 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst) Allocated memory per operation 39.87 KiB/op 39.87 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst) Time per operation 16.05 µs/op 16.18 µs/op 0.8% more time Not flagged
Capture and deliver events (4,096-byte payload, burst) idle_managed_heap_bytes (estimated) 2.13 MiB 2.13 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst) Idle process memory (RSS) 56.85 MiB 56.96 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst) Observed peak process memory (RSS) 81.98 MiB 82.08 MiB 0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst) Managed heap after settling (estimated) 25.63 MiB 25.63 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst) Process memory after settling (RSS) 82.15 MiB 82.25 MiB 0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst) Process CPU time per operation 17.41 µs/op 17.61 µs/op 1.1% more time Not flagged
Capture and deliver events (4,096-byte payload, burst) Lifetime peak process memory (RSS) 82.15 MiB 82.25 MiB 0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady) Allocated memory per operation 18.15 KiB/op 18.15 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady) Time per operation 8.14 µs/op 8.12 µs/op 0.3% less time Not flagged
Capture and deliver events (32-byte payload, steady) idle_managed_heap_bytes (estimated) 822.48 KiB 823.68 KiB 0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady) Idle process memory (RSS) 55.05 MiB 55.07 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady) Observed peak process memory (RSS) 55.62 MiB 55.64 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady) Managed heap after settling (estimated) 938.29 KiB 939.48 KiB 0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady) Process memory after settling (RSS) 55.61 MiB 55.63 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady) Process CPU time per operation 8.29 µs/op 8.27 µs/op 0.3% less time Not flagged
Capture and deliver events (32-byte payload, steady) Lifetime peak process memory (RSS) 55.61 MiB 55.63 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady) Allocated memory per operation 30.97 KiB/op 30.96 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady) Time per operation 12.85 µs/op 12.68 µs/op 1.3% less time Not flagged
Capture and deliver events (4,096-byte payload, steady) idle_managed_heap_bytes (estimated) 2.13 MiB 2.13 MiB 0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady) Idle process memory (RSS) 57 MiB 56.96 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady) Observed peak process memory (RSS) 64.51 MiB 64.41 MiB 0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady) Managed heap after settling (estimated) 3.12 MiB 3.12 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady) Process memory after settling (RSS) 60.71 MiB 60.71 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady) Process CPU time per operation 15.05 µs/op 14.89 µs/op 1.1% less time Not flagged
Capture and deliver events (4,096-byte payload, steady) Lifetime peak process memory (RSS) 64.24 MiB 64.09 MiB 0.2% less memory Not flagged
Queue an event (32-byte payload) Allocated memory per operation 1.67 KiB/op 1.67 KiB/op No observed change Not flagged
Queue an event (32-byte payload) Time per operation 1.35 µs/op 1.35 µs/op 0.1% less time Not flagged
Queue an event (32-byte payload) Process CPU time per operation 1.36 µs/op 1.36 µs/op 0.1% less time Not flagged
Queue an event (4,096-byte payload) Allocated memory per operation 1.67 KiB/op 1.67 KiB/op No observed change Not flagged
Queue an event (4,096-byte payload) Time per operation 1.29 µs/op 1.29 µs/op 0.1% more time Not flagged
Queue an event (4,096-byte payload) Process CPU time per operation 1.3 µs/op 1.31 µs/op 0.2% more time Not flagged
Evaluate all 10 flags Allocated memory per operation 22.38 KiB/op 22.38 KiB/op No observed change Not flagged
Evaluate all 10 flags Time per operation 28.92 µs/op 29.11 µs/op 0.7% more time Not flagged
Evaluate all 10 flags Process CPU time per operation 28.92 µs/op 29.12 µs/op 0.7% more time Not flagged
Evaluate all 100 flags Allocated memory per operation 217.06 KiB/op 217.06 KiB/op No observed change Not flagged
Evaluate all 100 flags Time per operation 261.03 µs/op 261.54 µs/op 0.2% more time Not flagged
Evaluate all 100 flags Process CPU time per operation 261.13 µs/op 261.64 µs/op 0.2% more time Not flagged
Evaluate all 1,000 flags Allocated memory per operation 2.11 MiB/op 2.11 MiB/op No observed change Not flagged
Evaluate all 1,000 flags Time per operation 2.26 ms/op 2.25 ms/op 0.5% less time Not flagged
Evaluate all 1,000 flags Process CPU time per operation 2.26 ms/op 2.25 ms/op 0.4% less time Not flagged
Evaluate 1,000 flags under concurrent load idle_managed_heap_bytes (estimated) 4.77 MiB 4.79 MiB 0.4% more memory Not flagged
Evaluate 1,000 flags under concurrent load Idle process memory (RSS) 108.19 MiB 108.09 MiB <0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load Observed peak process memory (RSS) 113.87 MiB 113.77 MiB <0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load Managed heap after settling (estimated) 4.87 MiB 4.89 MiB 0.4% more memory Not flagged
Evaluate 1,000 flags under concurrent load Process memory after settling (RSS) 113.98 MiB 113.88 MiB <0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load Lifetime peak process memory (RSS) 113.98 MiB 113.88 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions idle_managed_heap_bytes (estimated) 19.56 MiB 19.58 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions Idle process memory (RSS) 140.75 MiB 140.73 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions Observed peak process memory (RSS) 148.23 MiB 146.64 MiB 1.1% less memory Not flagged
Refresh 1,000 flag definitions Managed heap after settling (estimated) 20.55 MiB 20.57 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions Process memory after settling (RSS) 147.05 MiB 145.72 MiB 0.9% less memory Not flagged
Refresh 1,000 flag definitions Lifetime peak process memory (RSS) 147.67 MiB 146.12 MiB 1.1% less memory Not flagged
Evaluate one boolean flag Allocated memory per operation 1.63 KiB/op 1.63 KiB/op No observed change Not flagged
Evaluate one boolean flag Time per operation 2.18 µs/op 2.16 µs/op 0.9% less time Not flagged
Evaluate one boolean flag Process CPU time per operation 2.18 µs/op 2.16 µs/op 0.9% less time Not flagged
Evaluate a cohort-filtered flag Allocated memory per operation 1.89 KiB/op 1.89 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag Time per operation 2.19 µs/op 2.19 µs/op 0.2% more time Not flagged
Evaluate a cohort-filtered flag Process CPU time per operation 2.19 µs/op 2.19 µs/op 0.2% more time Not flagged
Evaluate a flag (early match) Allocated memory per operation 3.49 KiB/op 3.49 KiB/op No observed change Not flagged
Evaluate a flag (early match) Time per operation 3.48 µs/op 3.53 µs/op 1.5% more time Not flagged
Evaluate a flag (early match) Process CPU time per operation 3.48 µs/op 3.53 µs/op 1.5% more time Not flagged
Evaluate a flag (late match) Allocated memory per operation 5.09 KiB/op 5.09 KiB/op No observed change Not flagged
Evaluate a flag (late match) Time per operation 4.61 µs/op 4.63 µs/op 0.4% more time Not flagged
Evaluate a flag (late match) Process CPU time per operation 4.62 µs/op 4.63 µs/op 0.4% more time Not flagged
Evaluate a flag (no match) Allocated memory per operation 4.88 KiB/op 4.88 KiB/op No observed change Not flagged
Evaluate a flag (no match) Time per operation 4.08 µs/op 4.05 µs/op 0.7% less time Not flagged
Evaluate a flag (no match) Process CPU time per operation 4.08 µs/op 4.05 µs/op 0.7% less time Not flagged
Evaluate a property-filtered flag Allocated memory per operation 1.85 KiB/op 1.85 KiB/op No observed change Not flagged
Evaluate a property-filtered flag Time per operation 2.09 µs/op 2.07 µs/op 0.8% less time Not flagged
Evaluate a property-filtered flag Process CPU time per operation 2.09 µs/op 2.07 µs/op 0.8% less time Not flagged
Evaluate a regex-filtered flag Allocated memory per operation 6.28 KiB/op 6.28 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag Time per operation 7 µs/op 7.02 µs/op 0.3% more time Not flagged
Evaluate a regex-filtered flag Process CPU time per operation 7.01 µs/op 7.03 µs/op 0.3% more time Not flagged
Evaluate a rollout flag Allocated memory per operation 1.73 KiB/op 1.73 KiB/op No observed change Not flagged
Evaluate a rollout flag Time per operation 2.6 µs/op 2.59 µs/op 0.2% less time Not flagged
Evaluate a rollout flag Process CPU time per operation 2.6 µs/op 2.6 µs/op 0.2% less time Not flagged
Evaluate a multivariate flag Allocated memory per operation 1.77 KiB/op 1.77 KiB/op No observed change Not flagged
Evaluate a multivariate flag Time per operation 2.32 µs/op 2.31 µs/op 0.2% less time Not flagged
Evaluate a multivariate flag Process CPU time per operation 2.32 µs/op 2.32 µs/op 0.3% 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.

Compared d8980ddd0e74151d04ce8f5074b04b52cbe8455f → 887dad85c559ce3428fc9898e2182aa073082a69.

Full report and diagnostics

108 metric rows; 108 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 13368e4 into PostHog:main Dec 3, 2026
22 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.

2 participants