Repository navigation
fix(ai): take $ai_model from the OpenAI stream - #417
Conversation
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.
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-dotnetBenchmarks 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. Compared 108 metric rows; 108 rows shown in all results. Omitted or shortened data remain in |
💡 Motivation and Context
Follow-up to #389/#392 (stream vs non-stream parity) and #400 (stored prompts). For non-streaming calls,
PostHogOpenAIHandleroverrides$ai_modelwith themodelthe API returns. The streaming path builds its pseudo-response inTrackingStream, which never read the model, so streamed generations only kept the request's model. With the Responses API and a storedprompt, the request has nomodel, 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:
TrackingStreamnow recordsmodelfrom Chat Completions chunks (top level) and fromresponse.modelin Responses API events (response.created,response.completed, ...). The completion callback puts it on the pseudo-response, so the existing "model from response" logic inCaptureEventapplies 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/responsesstream for a request with a storedpromptand nomodel; asserts$ai_modelis the model from the stream events.SendAsyncTakesModelFromChatCompletionStreamChunks: a/v1/chat/completionsstream forgpt-4owhose chunks reportgpt-4o-2024-08-06; asserts$ai_modelis the snapshot, matching the non-streaming path.Both fail without the source change. With it,
PostHogOpenAIHandlerTestspasses 45/45 (dotnet test tests/PostHog.AI.Tests -f net8.0 --filter FullyQualifiedName~PostHogOpenAIHandlerTests), anddotnet format whitespace --verify-no-changesis clean on the touched files.📝 Checklist
If releasing new changes
pnpm changesetto 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.