Repository navigation
Description
Version
8.0.2
Steps to Reproduce
Bug description
The LLM analytics wrappers ignore the context distinct_id for streaming calls, but use it for non-streaming ones.
Inside a context identified with identify_context(...) (for example, one set by PosthogASGIMiddleware from the X-PostHog-Distinct-ID header), a non-streaming call records its $ai_generation under that distinct id. A streaming call made in the same context records it under its random trace id instead, and the event is personless. In LLM analytics, every streamed generation then shows up as a separate anonymous person.
How to reproduce
posthog-python 8.0.2, Python 3.13. The Gemini wrapper is shown here (no network: the provider call is faked), but the Anthropic and OpenAI wrappers share the same code path.
import asyncio
from google.genai import models as genai_models
from posthog import Posthog, new_context, identify_context
from posthog.ai.gemini import Client
class _Chunk:
text = "hi"
usage_metadata = None
candidates = []
async def _fake_stream(self, *args, **kwargs):
async def gen():
yield _Chunk()
return gen()
async def _fake_generate(self, *args, **kwargs):
return _Chunk()
genai_models.AsyncModels.generate_content_stream = _fake_stream # no network
genai_models.AsyncModels.generate_content = _fake_generate
seen = []
posthog = Posthog(
"phc_test",
host="http://127.0.0.1:9",
before_send=lambda e: seen.append((e["event"], e["distinct_id"])) or None,
sync_mode=True,
)
client = Client(api_key="fake", posthog_client=posthog)
async def main():
with new_context():
identify_context("user-123")
await client.aio.models.generate_content(model="gemini-2.5-flash", contents="hi")
stream = await client.aio.models.generate_content_stream(model="gemini-2.5-flash", contents="hi")
async for _ in stream:
pass
asyncio.run(main())
for name, (event, distinct_id) in zip(["generate_content", "generate_content_stream"], seen):
print(f"{name:24} {event} distinct_id={distinct_id}")Output:
generate_content $ai_generation distinct_id=user-123
generate_content_stream $ai_generation distinct_id=70683a83-df90-4dfc-bfde-7a9258b752ed
Cause
- The non-streaming path (
call_llm_and_track_usage/call_llm_and_track_usage_asyncinposthog/ai/utils.py) checkscontexts.get_context_distinct_id()before falling back to the trace id. - The streaming path,
capture_streaming_eventinposthog/ai/utils.py, captures withdistinct_id=_user_distinct_id(event_data.get("distinct_id")) or trace_id. When no explicitposthog_distinct_idis passed, it sends the trace id as an explicit distinct id, so the context is never consulted.
A possible fix is for capture_streaming_event to fall back to _user_distinct_id(contexts.get_context_distinct_id()) before trace_id, matching the non-streaming path.
Workaround
Pass the context id explicitly on each call: posthog_distinct_id=contexts.get_context_distinct_id().
Expected Result
Expected result
Both generations are captured under the context's distinct id, like any other event captured inside the context:
generate_content $ai_generation distinct_id=user-123
generate_content_stream $ai_generation distinct_id=user-123
Actual Result
Only the non-streaming generation uses the context's distinct id. The streaming one is captured under its random trace id ($ai_trace_id) as a personless event:
generate_content $ai_generation distinct_id=user-123
generate_content_stream $ai_generation distinct_id=70683a83-df90-4dfc-bfde-7a9258b752ed
Activity
posthog commentedon Nov 20, 2026
PostHog self-driving picked up this issue. Status: Research is done.
Somebody may already be working on it, so ask before you start a fix.
Report (team access only): https://us.posthog.com/project/2/inbox/reports/01a1f937-3980-7f7e-bcfe-a48075376915?link_source=github_comment