Skip to content

fix(ai): treat an empty or whitespace distinct id as missing in AI integrations - #1158

Merged
eli-r-ph merged 3 commits into
v1from
eli/v1-ai-blank-distinct-id
Nov 2, 2026
Merged

eli-r-ph merged 3 commits into
v1from
eli/v1-ai-blank-distinct-id

Conversation

@eli-r-ph

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

The client treats an empty or whitespace-only distinct_id as missing, but the AI integrations did not. With posthog_distinct_id="", they sent the trace or run ID as an explicit distinct ID without process_person_profile: false, so PostHog created a person for each trace. The personless option belongs to any AI event that has no real user ID.

Changes

  • New _user_distinct_id helper in posthog/ai/utils.py: returns the caller's ID unchanged, or None when it is missing, empty or whitespace (the client's _explicit_distinct_id rule).
  • The streaming and non-streaming wrappers, Gemini and OpenAI embeddings, LangChain (spans and traces) and OpenAI Agents use it for both the fallback ID and the personless flag.
  • A whitespace-only ID now falls back to the trace or run ID, like an empty or missing one. It was already personless, under a generated ID.
  • Patch changeset.

💚 How did you test it?

  • Existing personless tests now also run with "" and " ": Anthropic (sync and async, response and stream), Gemini embeddings, OpenAI Agents traces, LangChain. They fail on the empty and whitespace cases without the change.
  • Full suite with --extra test: 5352 passed. ruff check, ruff format --check, and mypy as in CI pass.

📝 Checklist

  • 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 sampo add to generate a changeset file

🤖 Generated with Claude Code

@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.

eli-r-ph and others added 3 commits November 2, 2026 09:07
…tegrations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eli-r-ph
eli-r-ph force-pushed the eli/v1-ai-blank-distinct-id branch from 1d44d95 to 2cd9840 Compare November 2, 2026 17:10
@eli-r-ph
eli-r-ph merged commit 4d54d2b into v1 Nov 2, 2026
40 checks passed
@eli-r-ph
eli-r-ph deleted the eli/v1-ai-blank-distinct-id branch November 2, 2026 17:32
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