Repository navigation
fix(ai): use the context distinct id for streamed generations - #1286
turnipdabeets wants to merge 1 commit into
Conversation
Streamed $ai_generation events fell back to the trace id even inside an identified context, unlike non-streamed calls. Check the context's distinct id before the trace id. Fixes #1284
PR overviewNo security issues were found in this pull request. Reviews
|
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
Fixes #1284.
Inside a context identified with
identify_context(...)(for example byPosthogASGIMiddleware), non-streamed LLM calls record$ai_generationunder the context's distinct id, but streamed calls recorded it under the random trace id as a personless event. So every streamed generation showed up as a separate anonymous person.capture_streaming_eventnow checks the context's distinct id before falling back to the trace id, the same waycall_llm_and_track_usagedoes since #449. This one function covers streaming for OpenAI, Anthropic and Gemini, sync and async.The order is: explicit
posthog_distinct_id, then the context id, then the trace id (personless). Behaviour with no context is unchanged.💚 How did you test it?
test_streaming_uses_outer_context_distinct_id. It failed before the fix and passes after it.pytest posthog/test/ai: 1867 passed, 15 skipped.capture_streaming_event: the event now hasuser-123instead of a trace id.📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
capture_streaming_event, so whichever lands second may need a small rebase.🤖 Generated with Claude Code