Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/doughty-warden-otso.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Fix Gemini web search extraction when response candidates are null.
2 changes: 1 addition & 1 deletion posthog/ai/gemini/gemini_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def extract_gemini_web_search_count(response: Any) -> int:
"""

# Check for grounding_metadata in candidates
if hasattr(response, "candidates"):
if hasattr(response, "candidates") and response.candidates:
for candidate in response.candidates:
if (
hasattr(candidate, "grounding_metadata")
Expand Down
42 changes: 42 additions & 0 deletions posthog/test/ai/gemini/test_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,48 @@ def test_empty_array_grounding_metadata_no_web_search(
assert props["$ai_output_tokens"] == 12


@pytest.mark.parametrize("candidates_value", [None, []])
def test_falsy_candidates_no_web_search(
mock_client, mock_google_genai_client, candidates_value
):
"""Test that response with falsy candidates does not crash web search extraction."""

mock_response = MagicMock()

# Mock usage metadata
mock_usage = MagicMock()
mock_usage.prompt_token_count = 5
mock_usage.candidates_token_count = 8
mock_usage.cached_content_token_count = 0
mock_usage.thoughts_token_count = 0
mock_response.usage_metadata = mock_usage

# candidates attribute exists but is falsy
mock_response.candidates = candidates_value
mock_response.text = "Hello!"

mock_google_genai_client.models.generate_content.return_value = mock_response

client = Client(api_key="test-key", posthog_client=mock_client)

response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Hi",
posthog_distinct_id="test-id",
)

assert response == mock_response
assert mock_client.capture.call_count == 1

call_args = mock_client.capture.call_args[1]
props = call_args["properties"]

# Should not crash and web search count should not be present
assert "$ai_web_search_count" not in props
assert props["$ai_input_tokens"] == 5
assert props["$ai_output_tokens"] == 8


@pytest.fixture
def mock_embed_content_response():
mock_response = MagicMock()
Expand Down
Loading