Repository navigation
fix(ai): use context distinct_id for OpenAI and Gemini embedding events #1290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| pypi/posthog: patch | ||
| --- | ||
|
|
||
| fix(ai): OpenAI and Gemini embedding events now use the context distinct ID instead of falling back to a personless trace ID |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -465,6 +465,24 @@ def test_embeddings(mock_client, mock_embedding_response): | |
| assert isinstance(props["$ai_latency"], float) | ||
|
|
||
|
|
||
| def test_embeddings_use_context_distinct_id(mock_client, mock_embedding_response): | ||
| from posthog import identify_context, new_context | ||
|
|
||
| with patch( | ||
| "openai.resources.embeddings.Embeddings.create", | ||
| return_value=mock_embedding_response, | ||
| ): | ||
| client = OpenAI(api_key="test-key", posthog_client=mock_client) | ||
| with new_context(): | ||
| identify_context("context-user") | ||
| client.embeddings.create(model="text-embedding-3-small", input="Hello") | ||
|
|
||
| call_args = mock_client.capture_ai.call_args[1] | ||
| assert call_args["distinct_id"] == "context-user" | ||
| assert call_args["event"] == "$ai_embedding" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concern: Neither new test asserts that Evidence and impact:
Recommendation: Test sketch (not executed as a committed test; equivalent scenarios passed on head # In both test_embeddings_use_context_distinct_id and test_embed_content_uses_context_distinct_id:
assert call_args["distinct_id"] == "context-user"
assert call_args["event"] == "$ai_embedding"
assert "options" not in call_args
# Plus in posthog/test/ai/test_utils.py (or alongside the embedding tests):
def test_resolve_distinct_id_precedence_and_blank_values():
from posthog import identify_context, new_context
from posthog.ai.utils import _resolve_distinct_id
with new_context(fresh=True):
assert _resolve_distinct_id(None) is None
identify_context(" ")
assert _resolve_distinct_id(None) is None
identify_context("context-user")
assert _resolve_distinct_id(None) == "context-user"
assert _resolve_distinct_id(" ") == "context-user"
assert _resolve_distinct_id("explicit-user") == "explicit-user" |
||
| assert "options" not in call_args | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("stream_options", [None, NOT_GIVEN, {"foo": "bar"}]) | ||
| def test_streaming_stream_options_are_not_mutated( | ||
| mock_client, streaming_tool_call_chunks, stream_options | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Now that #1286 (
470a6413) has landed onmainwith the identical_user_distinct_id(...) or _user_distinct_id(contexts.get_context_distinct_id())expression incapture_streaming_event(posthog/ai/utils.py:1321-1323onmain), consider reusing_resolve_distinct_id(event_data.get("distinct_id"))there if you rebase ontomain, so both streaming and embedding paths share one helper. (No merge conflict exists either way.)