fix: handle Gemini responses without candidates#599
Conversation
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
posthog/test/ai/gemini/test_gemini.py:1173-1176
The team's convention is to prefer parameterised tests. The new test covers a single input (`candidates=None`), but the same fixed code path also handles an empty list (`[]`). Parameterising over both falsy values would give stronger regression coverage with no extra boilerplate.
```suggestion
@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()
```
### Issue 2 of 2
posthog/test/ai/gemini/test_gemini.py:1186-1187
If the test is parameterised, the `candidates` assignment needs to reference the parameter variable rather than the literal `None`.
```suggestion
# candidates attribute exists but is falsy
mock_response.candidates = candidates_value
```
Reviews (1): Last reviewed commit: "fix: handle Gemini responses without can..." | Re-trigger Greptile |
posthog-python Compliance ReportDate: 2026-05-20 12:25:35 UTC
|
| Test | Status | Duration |
|---|---|---|
| Format Validation.Event Has Required Fields | ✅ | 516ms |
| Format Validation.Event Has Uuid | ✅ | 1507ms |
| Format Validation.Event Has Lib Properties | ✅ | 1505ms |
| Format Validation.Distinct Id Is String | ✅ | 1507ms |
| Format Validation.Token Is Present | ✅ | 1505ms |
| Format Validation.Custom Properties Preserved | ✅ | 1505ms |
| Format Validation.Event Has Timestamp | ✅ | 1507ms |
| Retry Behavior.Retries On 503 | ✅ | 9517ms |
| Retry Behavior.Does Not Retry On 400 | ✅ | 3505ms |
| Retry Behavior.Does Not Retry On 401 | ✅ | 3507ms |
| Retry Behavior.Respects Retry After Header | ✅ | 9514ms |
| Retry Behavior.Implements Backoff | ✅ | 23529ms |
| Retry Behavior.Retries On 500 | ✅ | 7500ms |
| Retry Behavior.Retries On 502 | ✅ | 7516ms |
| Retry Behavior.Retries On 504 | ✅ | 7506ms |
| Retry Behavior.Max Retries Respected | ✅ | 23534ms |
| Deduplication.Generates Unique Uuids | ✅ | 1496ms |
| Deduplication.Preserves Uuid On Retry | ✅ | 7514ms |
| Deduplication.Preserves Uuid And Timestamp On Retry | ✅ | 14522ms |
| Deduplication.Preserves Uuid And Timestamp On Batch Retry | ✅ | 7509ms |
| Deduplication.No Duplicate Events In Batch | ✅ | 1503ms |
| Deduplication.Different Events Have Different Uuids | ✅ | 1507ms |
| Compression.Sends Gzip When Enabled | ✅ | 1507ms |
| Batch Format.Uses Proper Batch Structure | ✅ | 1507ms |
| Batch Format.Flush With No Events Sends Nothing | ✅ | 1005ms |
| Batch Format.Multiple Events Batched Together | ✅ | 1506ms |
| Error Handling.Does Not Retry On 403 | ✅ | 3508ms |
| Error Handling.Does Not Retry On 413 | ✅ | 3509ms |
| Error Handling.Retries On 408 | ✅ | 7509ms |
Feature_Flags Tests
View Details
| Test | Status | Duration |
|---|---|---|
| Request Payload.Request With Person Properties Device Id | ❌ | 509ms |
| Request Payload.Flags Request Uses V2 Query Param | ❌ | 300851ms |
| Request Payload.Flags Request Hits Flags Path Not Decide | ❌ | 301064ms |
| Request Payload.Flags Request Omits Authorization Header | ❌ | 301015ms |
| Request Payload.Token In Flags Body Matches Init | ❌ | 300981ms |
| Request Payload.Groups Round Trip | ❌ | 301019ms |
| Request Payload.Groups Default To Empty Object | ❌ | 300905ms |
| Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It | ❌ | 301047ms |
| Request Payload.Disable Geoip False Propagates As Geoip Disable False | ❌ | 300975ms |
| Request Payload.Disable Geoip Omitted Defaults To False | ❌ | 300996ms |
| Request Payload.Flag Keys To Evaluate Contains Only Requested Key | ❌ | 301035ms |
| Request Lifecycle.No Flags Request On Init Alone | ❌ | 300943ms |
| Request Lifecycle.No Flags Request On Normal Capture | ❌ | 301029ms |
| Request Lifecycle.Two Flag Calls Produce Two Remote Requests | ❌ | 301029ms |
| Request Lifecycle.Mock Response Value Is Returned To Caller | ❌ | 301030ms |
| Side Effect Events.Get Feature Flag Captures Feature Flag Called Event | ❌ | 301007ms |
Failures
request_payload.request_with_person_properties_device_id
Field 'token' not found in /flags request body at path 'token'. Available keys: ['distinct_id', 'groups', 'person_properties', 'group_properties', 'geoip_disable', 'device_id', 'flag_keys_to_evaluate', 'sentAt', 'api_key']
request_payload.flags_request_uses_v2_query_param
No error message
request_payload.flags_request_hits_flags_path_not_decide
No error message
request_payload.flags_request_omits_authorization_header
No error message
request_payload.token_in_flags_body_matches_init
No error message
request_payload.groups_round_trip
No error message
request_payload.groups_default_to_empty_object
No error message
request_payload.person_properties_distinct_id_auto_populated_when_caller_omits_it
No error message
request_payload.disable_geoip_false_propagates_as_geoip_disable_false
No error message
request_payload.disable_geoip_omitted_defaults_to_false
No error message
request_payload.flag_keys_to_evaluate_contains_only_requested_key
No error message
request_lifecycle.no_flags_request_on_init_alone
No error message
request_lifecycle.no_flags_request_on_normal_capture
No error message
request_lifecycle.two_flag_calls_produce_two_remote_requests
No error message
request_lifecycle.mock_response_value_is_returned_to_caller
No error message
side_effect_events.get_feature_flag_captures_feature_flag_called_event
No error message
💡 Motivation and Context
Fixes the issue reported in #418 where Gemini usage extraction can crash when the API response has a falsy
candidatesattribute, such asNone. In that case,extract_gemini_web_search_countattempted to iterate over a non-iterable value while checking for grounding/web search metadata.💚 How did you test it?
uv run pytest posthog/test/ai/gemini/test_gemini.py -quv run pytest posthog/test/ai/gemini/test_gemini.py::test_falsy_candidates_no_web_search -quv run ruff check posthog/ai/gemini/gemini_converter.py posthog/test/ai/gemini/test_gemini.py📝 Checklist
If releasing new changes
sampo addto generate a changeset file