Skip to content

fix(llma): use distinct_id from outer context if not provided#449

Merged
andrewm4894 merged 5 commits intoPostHog:masterfrom
ethanporcaro:distinct-id-context-fix
Mar 5, 2026
Merged

fix(llma): use distinct_id from outer context if not provided#449
andrewm4894 merged 5 commits intoPostHog:masterfrom
ethanporcaro:distinct-id-context-fix

Conversation

@ethanporcaro
Copy link
Contributor

Related to #443

Right now, the LLM analytics wrappers accept a posthog_distinct_id parameter, but override any existing distinct_id, even if the parameter is None.

The utility functions pass

ph_client.capture(
                    distinct_id=posthog_distinct_id or posthog_trace_id,
...

However, if posthog_distinct_id is None (default) it will default to the posthog_trace_id, even if an ID exists on the outer context.

This PR modifies the logic to do the following:

  • If a custom distinct_id is passed by parameter, it is used in identify_context
  • If no context exists (meaning the above never happened, and no existing context is available), identify with the trace_id.
  • Call ph_client.capture without the distinct_id parameter, so that it uses whatever was set from the above steps.

Let me know if any changes are needed or if I should implement this differently.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@andrewm4894 andrewm4894 requested a review from a team March 3, 2026 17:23
@ethanporcaro
Copy link
Contributor Author

Some of these failed tests are passing on my machine. Do the tests set an outer distinct_id?

@ethanporcaro
Copy link
Contributor Author

Nevermind, I realize I was running them incorrectly. I think I found the issue and will push a commit soon

@ethanporcaro
Copy link
Contributor Author

Should be good to go

@andrewm4894 andrewm4894 self-assigned this Mar 5, 2026
@andrewm4894 andrewm4894 self-requested a review March 5, 2026 12:31
@andrewm4894
Copy link
Member

Thanks for the contribution @ethanporcaro! Great bug find — the outer context distinct_id was indeed being ignored.

I pushed a commit on top with a few tweaks:

  1. Fixed $process_person_profile bug — The personless check (posthog_distinct_id is None) didn't account for outer context distinct_id, so events from users who set identify_context() in an outer scope would be incorrectly marked as personless. Changed to use a has_person_distinct_id flag that checks both the explicit param and the context.

  2. Fixed typo — "district_id" → "distinct_id" in comments.

  3. Added test coverage — 4 new tests covering the key scenarios:

    • No distinct_id provided → uses trace_id, marked personless
    • Explicit posthog_distinct_id param → uses it, creates person profile
    • Outer context identify_context() → uses outer context id, creates person profile
    • Both explicit param and outer context → explicit param wins

ethanporcaro and others added 4 commits March 5, 2026 12:35
…, add tests

- Fix personless check to consider outer context distinct_id (not just the
  explicit param), so events from users who set distinct_id via outer context
  are not incorrectly marked as personless.
- Fix typo: "district_id" -> "distinct_id" in comments.
- Add test coverage for distinct_id resolution: no id (personless), explicit
  param, outer context, and explicit overriding outer context.
@andrewm4894 andrewm4894 force-pushed the distinct-id-context-fix branch from 91ad399 to 0067cdf Compare March 5, 2026 12:38
@ethanporcaro
Copy link
Contributor Author

Awesome, thanks for the help

@andrewm4894 andrewm4894 merged commit b206669 into PostHog:master Mar 5, 2026
19 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants