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/gemini-streaming-error-http-status.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 1 addition & 3 deletions posthog/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
16 changes: 16 additions & 0 deletions posthog/test/ai/test_stream_midstream_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading