From 46128afdb2925e7aa350f30f7072f82261417007 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sat, 31 Oct 2026 01:16:10 -0400 Subject: [PATCH 1/5] fix(ai): count Gemini tool use prompt tokens as input tokens Built-in tools (Google Search, URL context) report the retrieved content in tool_use_prompt_token_count, separately from prompt_token_count, so input tokens were under-reported on grounded responses. Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Alex Castillo --- posthog/ai/gemini/gemini_converter.py | 10 ++++++- .../test/ai/gemini/test_gemini_converter.py | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/posthog/ai/gemini/gemini_converter.py b/posthog/ai/gemini/gemini_converter.py index 09ae82d25..e1922f636 100644 --- a/posthog/ai/gemini/gemini_converter.py +++ b/posthog/ai/gemini/gemini_converter.py @@ -528,8 +528,16 @@ def _extract_usage_from_metadata(metadata: Any) -> TokenUsage: Returns: TokenUsage with standardized usage """ + input_tokens = getattr(metadata, "prompt_token_count", 0) + + # Built-in tools (Google Search, URL context) report the retrieved content + # separately from prompt_token_count, but it is billed as input + tool_use_tokens = getattr(metadata, "tool_use_prompt_token_count", 0) + if isinstance(tool_use_tokens, int) and tool_use_tokens > 0: + input_tokens += tool_use_tokens + usage = TokenUsage( - input_tokens=getattr(metadata, "prompt_token_count", 0), + input_tokens=input_tokens, output_tokens=getattr(metadata, "candidates_token_count", 0), ) diff --git a/posthog/test/ai/gemini/test_gemini_converter.py b/posthog/test/ai/gemini/test_gemini_converter.py index 7cdd211c7..5ba7c802a 100644 --- a/posthog/test/ai/gemini/test_gemini_converter.py +++ b/posthog/test/ai/gemini/test_gemini_converter.py @@ -11,6 +11,7 @@ from posthog.ai.gemini.gemini_converter import ( extract_gemini_content_from_chunk, + extract_gemini_usage_from_response, format_gemini_input, format_gemini_response, format_gemini_streaming_output, @@ -311,3 +312,31 @@ def test_streaming_chunk_thought_part_becomes_thinking_block(self): {"type": "thinking", "thinking": "Hmm, "}, {"type": "text", "text": "done"}, ] + + +class TestGeminiUsage: + def test_tool_use_prompt_tokens_count_as_input(self): + # Built-in tools (Google Search, URL context) report the retrieved content + # in tool_use_prompt_token_count, separately from prompt_token_count. + resp = types.GenerateContentResponse( + usage_metadata=types.GenerateContentResponseUsageMetadata( + prompt_token_count=20, + tool_use_prompt_token_count=300, + candidates_token_count=10, + total_token_count=330, + ) + ) + usage = extract_gemini_usage_from_response(resp) + assert usage["input_tokens"] == 320 + assert usage["output_tokens"] == 10 + + def test_input_tokens_without_tool_use(self): + resp = types.GenerateContentResponse( + usage_metadata=types.GenerateContentResponseUsageMetadata( + prompt_token_count=20, + candidates_token_count=10, + total_token_count=30, + ) + ) + usage = extract_gemini_usage_from_response(resp) + assert usage["input_tokens"] == 20 From 2b3a7da565db75c7e6b62d7f800516fe41b1d5bf Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sat, 31 Oct 2026 01:18:26 -0400 Subject: [PATCH 2/5] chore: add changeset for Gemini tool use prompt tokens Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Alex Castillo --- .sampo/changesets/gemini-tool-use-prompt-tokens.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .sampo/changesets/gemini-tool-use-prompt-tokens.md diff --git a/.sampo/changesets/gemini-tool-use-prompt-tokens.md b/.sampo/changesets/gemini-tool-use-prompt-tokens.md new file mode 100644 index 000000000..ce7f36b0d --- /dev/null +++ b/.sampo/changesets/gemini-tool-use-prompt-tokens.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Count Gemini tool use prompt tokens (Google Search, URL context) as input tokens in captured generations. From 5f474f827ebe72436b25d66aecbf6ff1d94b8aa1 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Mon, 2 Nov 2026 10:50:32 -0500 Subject: [PATCH 3/5] fix(ai): keep unbilled Google Search retrieval out of Gemini input tokens tool_use_prompt_token_count is only added to input tokens when the response carries url_context_metadata (billed as input); Search retrieval is not. Co-Authored-By: Claude Sonnet 5.5 --- .../gemini-tool-use-prompt-tokens.md | 2 +- posthog/ai/gemini/gemini_converter.py | 35 ++++++++++++++---- .../test/ai/gemini/test_gemini_converter.py | 37 +++++++++++++++---- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/.sampo/changesets/gemini-tool-use-prompt-tokens.md b/.sampo/changesets/gemini-tool-use-prompt-tokens.md index ce7f36b0d..6c5f98d9a 100644 --- a/.sampo/changesets/gemini-tool-use-prompt-tokens.md +++ b/.sampo/changesets/gemini-tool-use-prompt-tokens.md @@ -2,4 +2,4 @@ pypi/posthog: patch --- -Count Gemini tool use prompt tokens (Google Search, URL context) as input tokens in captured generations. +Count Gemini URL context tool use prompt tokens as input tokens in captured generations; Google Search retrieval is not billed as input and stays out of the total. diff --git a/posthog/ai/gemini/gemini_converter.py b/posthog/ai/gemini/gemini_converter.py index e1922f636..fa29ac87a 100644 --- a/posthog/ai/gemini/gemini_converter.py +++ b/posthog/ai/gemini/gemini_converter.py @@ -517,24 +517,39 @@ def extract_gemini_web_search_count(response: Any) -> int: return 0 -def _extract_usage_from_metadata(metadata: Any) -> TokenUsage: +def _has_url_context(response: Any) -> bool: + """Whether a response or chunk carries URL context retrieval metadata.""" + candidates = getattr(response, "candidates", None) + if not isinstance(candidates, (list, tuple)): + return False + return any(getattr(c, "url_context_metadata", None) for c in candidates) + + +def _extract_usage_from_metadata( + metadata: Any, bill_tool_use_tokens: bool = False +) -> TokenUsage: """ Common logic to extract usage from Gemini metadata. Used by both streaming and non-streaming paths. Args: metadata: usage_metadata from Gemini response or chunk + bill_tool_use_tokens: whether tool_use_prompt_token_count is billed as + input (URL context). Google Search retrieval is not billed as input. Returns: TokenUsage with standardized usage """ input_tokens = getattr(metadata, "prompt_token_count", 0) - # Built-in tools (Google Search, URL context) report the retrieved content - # separately from prompt_token_count, but it is billed as input - tool_use_tokens = getattr(metadata, "tool_use_prompt_token_count", 0) - if isinstance(tool_use_tokens, int) and tool_use_tokens > 0: - input_tokens += tool_use_tokens + # Built-in tools report the retrieved content separately from + # prompt_token_count. URL context content is billed as input; Google Search + # retrieval is not, so only add it when the response used URL context. + # (A response that used both tools cannot be split from these counts.) + if bill_tool_use_tokens: + tool_use_tokens = getattr(metadata, "tool_use_prompt_token_count", 0) + if isinstance(tool_use_tokens, int) and tool_use_tokens > 0: + input_tokens += tool_use_tokens usage = TokenUsage( input_tokens=input_tokens, @@ -581,7 +596,9 @@ def extract_gemini_usage_from_response(response: Any) -> TokenUsage: if not hasattr(response, "usage_metadata") or not response.usage_metadata: return TokenUsage() - usage = _extract_usage_from_metadata(response.usage_metadata) + usage = _extract_usage_from_metadata( + response.usage_metadata, bill_tool_use_tokens=_has_url_context(response) + ) # Add web search count if present web_search_count = extract_gemini_web_search_count(response) @@ -613,7 +630,9 @@ def extract_gemini_usage_from_chunk(chunk: Any) -> TokenUsage: if not hasattr(chunk, "usage_metadata") or not chunk.usage_metadata: return usage - usage_from_metadata = _extract_usage_from_metadata(chunk.usage_metadata) + usage_from_metadata = _extract_usage_from_metadata( + chunk.usage_metadata, bill_tool_use_tokens=_has_url_context(chunk) + ) # Merge the usage from metadata with any web search count we found usage.update(usage_from_metadata) diff --git a/posthog/test/ai/gemini/test_gemini_converter.py b/posthog/test/ai/gemini/test_gemini_converter.py index 5ba7c802a..4227b6d08 100644 --- a/posthog/test/ai/gemini/test_gemini_converter.py +++ b/posthog/test/ai/gemini/test_gemini_converter.py @@ -10,6 +10,7 @@ GEMINI_AVAILABLE = False from posthog.ai.gemini.gemini_converter import ( + extract_gemini_usage_from_chunk, extract_gemini_content_from_chunk, extract_gemini_usage_from_response, format_gemini_input, @@ -315,20 +316,40 @@ def test_streaming_chunk_thought_part_becomes_thinking_block(self): class TestGeminiUsage: - def test_tool_use_prompt_tokens_count_as_input(self): - # Built-in tools (Google Search, URL context) report the retrieved content - # in tool_use_prompt_token_count, separately from prompt_token_count. - resp = types.GenerateContentResponse( + @staticmethod + def _response(**candidate_kwargs): + return types.GenerateContentResponse( + candidates=[types.Candidate(**candidate_kwargs)], usage_metadata=types.GenerateContentResponseUsageMetadata( prompt_token_count=20, tool_use_prompt_token_count=300, candidates_token_count=10, total_token_count=330, - ) + ), ) - usage = extract_gemini_usage_from_response(resp) - assert usage["input_tokens"] == 320 - assert usage["output_tokens"] == 10 + + def test_url_context_tool_use_tokens_count_as_input(self): + # URL context content is billed as input. + resp = self._response(url_context_metadata=types.UrlContextMetadata()) + for extract in ( + extract_gemini_usage_from_response, + extract_gemini_usage_from_chunk, + ): + usage = extract(resp) + assert usage["input_tokens"] == 320 + assert usage["output_tokens"] == 10 + + def test_search_retrieval_tokens_are_not_billed_as_input(self): + # Google Search retrieval is reported in tool_use_prompt_token_count but + # is not charged as input. + resp = self._response( + grounding_metadata=types.GroundingMetadata(web_search_queries=["q"]) + ) + for extract in ( + extract_gemini_usage_from_response, + extract_gemini_usage_from_chunk, + ): + assert extract(resp)["input_tokens"] == 20 def test_input_tokens_without_tool_use(self): resp = types.GenerateContentResponse( From 480bc137ede5c97ac228a280719708a5f69ef0b8 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Mon, 2 Nov 2026 11:10:04 -0500 Subject: [PATCH 4/5] fix(ai): carry Gemini URL context flag across stream chunks URL context metadata can arrive on a different chunk than the final usage_metadata, so the tool-use prompt tokens were dropped in streaming. Track the flag across chunks in the sync and async clients. Co-Authored-By: Claude Sonnet 5.5 --- posthog/ai/gemini/gemini.py | 9 ++++- posthog/ai/gemini/gemini_async.py | 9 ++++- posthog/ai/gemini/gemini_converter.py | 14 +++++--- posthog/test/ai/gemini/test_gemini.py | 37 +++++++++++++++++++++ posthog/test/ai/gemini/test_gemini_async.py | 37 +++++++++++++++++++++ 5 files changed, 100 insertions(+), 6 deletions(-) diff --git a/posthog/ai/gemini/gemini.py b/posthog/ai/gemini/gemini.py index dad87da9a..c1c4e54f6 100644 --- a/posthog/ai/gemini/gemini.py +++ b/posthog/ai/gemini/gemini.py @@ -25,6 +25,7 @@ extract_gemini_embedding_token_count as extract_gemini_embedding_token_count, extract_gemini_stop_reason_from_chunk, extract_gemini_usage_from_chunk, + has_gemini_url_context, format_gemini_streaming_output as format_gemini_streaming_output, ) @@ -267,10 +268,16 @@ def generator(): nonlocal accumulated_content nonlocal stop_reason stream_error: Optional[BaseException] = None + url_context_seen = False try: for chunk in response: # Extract usage stats from chunk - chunk_usage = extract_gemini_usage_from_chunk(chunk) + url_context_seen = url_context_seen or has_gemini_url_context( + chunk + ) + chunk_usage = extract_gemini_usage_from_chunk( + chunk, url_context_seen=url_context_seen + ) if chunk_usage: # Gemini reports cumulative totals, not incremental values diff --git a/posthog/ai/gemini/gemini_async.py b/posthog/ai/gemini/gemini_async.py index 6c70efd9b..793c12a07 100644 --- a/posthog/ai/gemini/gemini_async.py +++ b/posthog/ai/gemini/gemini_async.py @@ -25,6 +25,7 @@ extract_gemini_embedding_token_count as extract_gemini_embedding_token_count, extract_gemini_stop_reason_from_chunk, extract_gemini_usage_from_chunk, + has_gemini_url_context, format_gemini_streaming_output as format_gemini_streaming_output, ) @@ -254,10 +255,16 @@ async def async_generator(): nonlocal stop_reason stream_error: Optional[BaseException] = None + url_context_seen = False try: async for chunk in response: # Extract usage stats from chunk - chunk_usage = extract_gemini_usage_from_chunk(chunk) + url_context_seen = url_context_seen or has_gemini_url_context( + chunk + ) + chunk_usage = extract_gemini_usage_from_chunk( + chunk, url_context_seen=url_context_seen + ) if chunk_usage: # Gemini reports cumulative totals, not incremental values diff --git a/posthog/ai/gemini/gemini_converter.py b/posthog/ai/gemini/gemini_converter.py index fa29ac87a..81b4f630b 100644 --- a/posthog/ai/gemini/gemini_converter.py +++ b/posthog/ai/gemini/gemini_converter.py @@ -517,7 +517,7 @@ def extract_gemini_web_search_count(response: Any) -> int: return 0 -def _has_url_context(response: Any) -> bool: +def has_gemini_url_context(response: Any) -> bool: """Whether a response or chunk carries URL context retrieval metadata.""" candidates = getattr(response, "candidates", None) if not isinstance(candidates, (list, tuple)): @@ -597,7 +597,7 @@ def extract_gemini_usage_from_response(response: Any) -> TokenUsage: return TokenUsage() usage = _extract_usage_from_metadata( - response.usage_metadata, bill_tool_use_tokens=_has_url_context(response) + response.usage_metadata, bill_tool_use_tokens=has_gemini_url_context(response) ) # Add web search count if present @@ -608,12 +608,17 @@ def extract_gemini_usage_from_response(response: Any) -> TokenUsage: return usage -def extract_gemini_usage_from_chunk(chunk: Any) -> TokenUsage: +def extract_gemini_usage_from_chunk( + chunk: Any, url_context_seen: bool = False +) -> TokenUsage: """ Extract usage statistics from a Gemini streaming chunk. Args: chunk: Streaming chunk from Gemini API + url_context_seen: whether an earlier chunk of the same stream carried + URL context metadata. Tool indicators can arrive on a different + chunk than the final usage_metadata, so callers carry this flag. Returns: TokenUsage with standardized usage statistics @@ -631,7 +636,8 @@ def extract_gemini_usage_from_chunk(chunk: Any) -> TokenUsage: return usage usage_from_metadata = _extract_usage_from_metadata( - chunk.usage_metadata, bill_tool_use_tokens=_has_url_context(chunk) + chunk.usage_metadata, + bill_tool_use_tokens=url_context_seen or has_gemini_url_context(chunk), ) # Merge the usage from metadata with any web search count we found diff --git a/posthog/test/ai/gemini/test_gemini.py b/posthog/test/ai/gemini/test_gemini.py index b616dcd89..c5e8409b3 100644 --- a/posthog/test/ai/gemini/test_gemini.py +++ b/posthog/test/ai/gemini/test_gemini.py @@ -1741,3 +1741,40 @@ def test_ai_lane_client_routes_through_capture_ai( mock_client.capture.assert_not_called() assert mock_client.capture_ai.call_count == 1 assert mock_client.capture_ai.call_args[1]["event"] == "$ai_generation" + + +def _url_context_stream_chunks(): + from google.genai import types + + # URL context metadata arrives on an early chunk; usage on the last one. + first = types.GenerateContentResponse( + candidates=[types.Candidate(url_context_metadata=types.UrlContextMetadata())] + ) + last = types.GenerateContentResponse( + usage_metadata=types.GenerateContentResponseUsageMetadata( + prompt_token_count=20, + tool_use_prompt_token_count=300, + candidates_token_count=10, + total_token_count=330, + ) + ) + return first, last + + +def test_streaming_url_context_on_earlier_chunk_bills_tool_use_tokens( + mock_client, mock_google_genai_client +): + first, last = _url_context_stream_chunks() + mock_google_genai_client.models.generate_content_stream.return_value = iter( + [first, last] + ) + + client = Client(api_key="test-key", posthog_client=mock_client) + response = client.models.generate_content_stream( + model="gemini-2.0-flash", contents=["Summarise this page"] + ) + list(response) + + props = mock_client.capture.call_args[1]["properties"] + assert props["$ai_input_tokens"] == 320 + assert props["$ai_output_tokens"] == 10 diff --git a/posthog/test/ai/gemini/test_gemini_async.py b/posthog/test/ai/gemini/test_gemini_async.py index b4e63c019..9cdc0fdbb 100644 --- a/posthog/test/ai/gemini/test_gemini_async.py +++ b/posthog/test/ai/gemini/test_gemini_async.py @@ -1178,3 +1178,40 @@ async def mock_streaming_response(): call_args = mock_client.capture.call_args[1] assert call_args["event"] == "$ai_generation" assert call_args["properties"]["$ai_provider"] == "gemini" + + +async def test_async_streaming_url_context_on_earlier_chunk_bills_tool_use_tokens( + mock_client, mock_google_genai_client +): + from google.genai import types + + first = types.GenerateContentResponse( + candidates=[types.Candidate(url_context_metadata=types.UrlContextMetadata())] + ) + last = types.GenerateContentResponse( + usage_metadata=types.GenerateContentResponseUsageMetadata( + prompt_token_count=20, + tool_use_prompt_token_count=300, + candidates_token_count=10, + total_token_count=330, + ) + ) + + async def stream(): + yield first + yield last + + mock_google_genai_client.aio.models.generate_content_stream = AsyncMock( + return_value=stream() + ) + + client = AsyncClient(api_key="test-key", posthog_client=mock_client) + response = await client.models.generate_content_stream( + model="gemini-2.0-flash", contents=["Summarise this page"] + ) + async for _ in response: + pass + + props = mock_client.capture.call_args[1]["properties"] + assert props["$ai_input_tokens"] == 320 + assert props["$ai_output_tokens"] == 10 From f1d76c0e100faa93f8f1862e8a0032f6f9a192e5 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Mon, 2 Nov 2026 20:12:47 +0100 Subject: [PATCH 5/5] fix(ci): format Gemini clients and refresh API snapshot --- posthog/ai/gemini/gemini.py | 4 +--- posthog/ai/gemini/gemini_async.py | 4 +--- references/public_api_snapshot.txt | 5 ++++- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/posthog/ai/gemini/gemini.py b/posthog/ai/gemini/gemini.py index c1c4e54f6..485e2adee 100644 --- a/posthog/ai/gemini/gemini.py +++ b/posthog/ai/gemini/gemini.py @@ -272,9 +272,7 @@ def generator(): try: for chunk in response: # Extract usage stats from chunk - url_context_seen = url_context_seen or has_gemini_url_context( - chunk - ) + url_context_seen = url_context_seen or has_gemini_url_context(chunk) chunk_usage = extract_gemini_usage_from_chunk( chunk, url_context_seen=url_context_seen ) diff --git a/posthog/ai/gemini/gemini_async.py b/posthog/ai/gemini/gemini_async.py index 793c12a07..6823743ed 100644 --- a/posthog/ai/gemini/gemini_async.py +++ b/posthog/ai/gemini/gemini_async.py @@ -259,9 +259,7 @@ async def async_generator(): try: async for chunk in response: # Extract usage stats from chunk - url_context_seen = url_context_seen or has_gemini_url_context( - chunk - ) + url_context_seen = url_context_seen or has_gemini_url_context(chunk) chunk_usage = extract_gemini_usage_from_chunk( chunk, url_context_seen=url_context_seen ) diff --git a/references/public_api_snapshot.txt b/references/public_api_snapshot.txt index 3eadb81bd..fbac711ed 100644 --- a/references/public_api_snapshot.txt +++ b/references/public_api_snapshot.txt @@ -113,6 +113,7 @@ alias posthog.ai.gemini.gemini.extract_gemini_stop_reason_from_chunk -> posthog. alias posthog.ai.gemini.gemini.extract_gemini_usage_from_chunk -> posthog.ai.gemini.gemini_converter.extract_gemini_usage_from_chunk alias posthog.ai.gemini.gemini.finalize_ai_content -> posthog.ai.utils.finalize_ai_content alias posthog.ai.gemini.gemini.format_gemini_streaming_output -> posthog.ai.gemini.gemini_converter.format_gemini_streaming_output +alias posthog.ai.gemini.gemini.has_gemini_url_context -> posthog.ai.gemini.gemini_converter.has_gemini_url_context alias posthog.ai.gemini.gemini.merge_system_prompt -> posthog.ai.utils.merge_system_prompt alias posthog.ai.gemini.gemini.merge_usage_stats -> posthog.ai.utils.merge_usage_stats alias posthog.ai.gemini.gemini.setup -> posthog.setup @@ -129,6 +130,7 @@ alias posthog.ai.gemini.gemini_async.extract_gemini_stop_reason_from_chunk -> po alias posthog.ai.gemini.gemini_async.extract_gemini_usage_from_chunk -> posthog.ai.gemini.gemini_converter.extract_gemini_usage_from_chunk alias posthog.ai.gemini.gemini_async.finalize_ai_content -> posthog.ai.utils.finalize_ai_content alias posthog.ai.gemini.gemini_async.format_gemini_streaming_output -> posthog.ai.gemini.gemini_converter.format_gemini_streaming_output +alias posthog.ai.gemini.gemini_async.has_gemini_url_context -> posthog.ai.gemini.gemini_converter.has_gemini_url_context alias posthog.ai.gemini.gemini_async.merge_system_prompt -> posthog.ai.utils.merge_system_prompt alias posthog.ai.gemini.gemini_async.merge_usage_stats -> posthog.ai.utils.merge_usage_stats alias posthog.ai.gemini.gemini_async.setup -> posthog.setup @@ -1414,13 +1416,14 @@ function posthog.ai.gemini.gemini_converter.extract_gemini_stop_reason(response: function posthog.ai.gemini.gemini_converter.extract_gemini_stop_reason_from_chunk(chunk: Any) -> Optional[str] function posthog.ai.gemini.gemini_converter.extract_gemini_system_instruction(config: Any) -> Optional[str] function posthog.ai.gemini.gemini_converter.extract_gemini_tools(kwargs: Dict[str, Any]) -> Optional[Any] -function posthog.ai.gemini.gemini_converter.extract_gemini_usage_from_chunk(chunk: Any) -> TokenUsage +function posthog.ai.gemini.gemini_converter.extract_gemini_usage_from_chunk(chunk: Any, url_context_seen: bool = False) -> TokenUsage function posthog.ai.gemini.gemini_converter.extract_gemini_usage_from_response(response: Any) -> TokenUsage function posthog.ai.gemini.gemini_converter.extract_gemini_web_search_count(response: Any) -> int function posthog.ai.gemini.gemini_converter.format_gemini_input(contents: Any) -> List[FormattedMessage] function posthog.ai.gemini.gemini_converter.format_gemini_input_with_system(contents: Any, config: Any = None) -> List[FormattedMessage] function posthog.ai.gemini.gemini_converter.format_gemini_response(response: Any) -> List[FormattedMessage] function posthog.ai.gemini.gemini_converter.format_gemini_streaming_output(accumulated_content: Union[str, List[Any]]) -> List[FormattedMessage] +function posthog.ai.gemini.gemini_converter.has_gemini_url_context(response: Any) -> bool function posthog.ai.media.bytes_to_base64(data: bytes) -> str function posthog.ai.media.ensure_serializable(obj: Any) -> Any function posthog.ai.media.normalize_part_keys(d: dict) -> dict