diff --git a/.sampo/changesets/gemini-streaming-error-http-status.md b/.sampo/changesets/gemini-streaming-error-http-status.md new file mode 100644 index 000000000..c2254d74f --- /dev/null +++ b/.sampo/changesets/gemini-streaming-error-http-status.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Streaming Gemini calls that fail now record the HTTP status of the google-genai error (for example 429 or 503) in `$ai_http_status`. Before, `capture_streaming_event` read only `status_code`, so these events were captured with a status of 0. diff --git a/posthog/ai/utils.py b/posthog/ai/utils.py index 54c601a3d..779ea705e 100644 --- a/posthog/ai/utils.py +++ b/posthog/ai/utils.py @@ -1096,9 +1096,7 @@ def capture_streaming_event( event_data["privacy_mode"], finalize_ai_content(event_data["formatted_output"], ph_client), ), - "$ai_http_status": ( - getattr(error, "status_code", 0) if error is not None else 200 - ), + "$ai_http_status": (_get_http_status(error) if error is not None else 200), **({"$ai_input_tokens": input_tokens} if input_tokens is not None else {}), **({"$ai_output_tokens": output_tokens} if output_tokens is not None else {}), "$ai_latency": event_data["latency"], diff --git a/posthog/test/ai/test_stream_midstream_error.py b/posthog/test/ai/test_stream_midstream_error.py index 1fbc8278f..386573a85 100644 --- a/posthog/test/ai/test_stream_midstream_error.py +++ b/posthog/test/ai/test_stream_midstream_error.py @@ -267,3 +267,19 @@ async def stream(): props = _props(mock_client) assert props["$ai_is_error"] is True assert props["$ai_http_status"] == 0 + + +@pytest.mark.parametrize( + ("cls_name", "code"), [("ClientError", 429), ("ServerError", 503)] +) +def test_capture_streaming_event_records_google_genai_error_status( + mock_client, cls_name, code +): + errors = pytest.importorskip("google.genai.errors") + error = getattr(errors, cls_name)( + code, {"error": {"code": code, "message": "nope", "status": "ERROR"}} + ) + capture_streaming_event(mock_client, _event_data(error=error)) + props = _props(mock_client) + assert props["$ai_http_status"] == code + assert props["$ai_is_error"] is True