Skip to content

Commit c28a2bb

Browse files
fix(ai): record an integer $ai_http_status in the LangChain callback handler
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent a5b93ee commit c28a2bb

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pypi/posthog: patch
3+
---
4+
5+
The LangChain callback handler now records `$ai_http_status` as an integer for failed generations. An error that carries a string `code` and no status code (such as OpenAI's `rate_limit_exceeded`) gave a string status; it now gives `0`, as an error without a status already did.

‎posthog/ai/langchain/callbacks.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,12 @@ def _get_http_status(error: BaseException) -> int:
10361036
# OpenAI: https://github.com/openai/openai-python/blob/main/src/openai/_exceptions.py
10371037
# Anthropic: https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/_exceptions.py
10381038
# Google: https://github.com/googleapis/python-api-core/blob/main/google/api_core/exceptions.py
1039-
status_code = getattr(error, "status_code", getattr(error, "code", 0))
1040-
return status_code
1039+
# `code` is a string on some errors (OpenAI's "rate_limit_exceeded"), so only ints count
1040+
for attribute in ("status_code", "code"):
1041+
value = getattr(error, attribute, None)
1042+
if isinstance(value, int) and not isinstance(value, bool):
1043+
return value
1044+
return 0
10411045

10421046

10431047
def _get_langchain_run_name(

‎posthog/test/ai/langchain/test_callbacks.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,3 +2953,64 @@ def test_stop_reason_resolution(
29532953

29542954
props = mock_client.capture.call_args.kwargs["properties"]
29552955
assert props.get("$ai_stop_reason") == expected
2956+
2957+
2958+
class TestGetHttpStatus:
2959+
def test_uses_the_status_code_of_an_http_error(self):
2960+
from posthog.ai.langchain.callbacks import _get_http_status
2961+
2962+
error = Exception("rate limited")
2963+
error.status_code = 429 # type: ignore[attr-defined]
2964+
2965+
assert _get_http_status(error) == 429
2966+
2967+
def test_uses_an_integer_code_when_there_is_no_status_code(self):
2968+
from posthog.ai.langchain.callbacks import _get_http_status
2969+
2970+
error = Exception("quota")
2971+
error.code = 503 # type: ignore[attr-defined]
2972+
2973+
assert _get_http_status(error) == 503
2974+
2975+
def test_ignores_a_string_code(self):
2976+
"""OpenAI's APIError carries code="rate_limit_exceeded" and no status code."""
2977+
from posthog.ai.langchain.callbacks import _get_http_status
2978+
2979+
error = Exception("Rate limit reached")
2980+
error.code = "rate_limit_exceeded" # type: ignore[attr-defined]
2981+
2982+
assert _get_http_status(error) == 0
2983+
2984+
def test_falls_back_to_zero_without_a_status(self):
2985+
from posthog.ai.langchain.callbacks import _get_http_status
2986+
2987+
assert _get_http_status(Exception("boom")) == 0
2988+
2989+
def test_an_error_event_records_an_integer_status(self):
2990+
import httpx
2991+
import openai
2992+
from unittest.mock import MagicMock
2993+
from uuid import uuid4
2994+
2995+
from posthog.ai.langchain import CallbackHandler
2996+
2997+
client = MagicMock()
2998+
client.privacy_mode = False
2999+
callback = CallbackHandler(client)
3000+
run_id = uuid4()
3001+
callback._set_llm_metadata(
3002+
serialized={},
3003+
run_id=run_id,
3004+
messages=[{"role": "user", "content": "test"}],
3005+
metadata={"ls_provider": "openai", "ls_model_name": "gpt-5.6-sol"},
3006+
)
3007+
error = openai.APIError(
3008+
"Rate limit reached",
3009+
request=httpx.Request("POST", "https://api.openai.com/v1/chat/completions"),
3010+
body={"code": "rate_limit_exceeded", "message": "Rate limit reached"},
3011+
)
3012+
3013+
callback._pop_run_and_capture_generation(run_id, None, error)
3014+
3015+
properties = client.capture.call_args_list[0][1]["properties"]
3016+
assert properties["$ai_http_status"] == 0

0 commit comments

Comments
 (0)