Skip to content

Commit 0a56e8d

Browse files
chore(llma): add integration tests for $ai_stop_reason and sampo changeset
1 parent 3a72386 commit 0a56e8d

5 files changed

Lines changed: 67 additions & 0 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: minor
3+
---
4+
5+
feat(ai): add $ai_stop_reason extraction for all providers

posthog/test/ai/anthropic/test_anthropic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
from unittest.mock import patch
34

45
import pytest
@@ -1401,3 +1402,22 @@ def test_explicit_distinct_id_overrides_outer_context(
14011402

14021403
call_args = mock_client.capture.call_args[1]
14031404
assert call_args["distinct_id"] == "explicit-user-789"
1405+
1406+
1407+
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
1408+
1409+
1410+
@pytest.mark.skipif(not ANTHROPIC_API_KEY, reason="ANTHROPIC_API_KEY is not set")
1411+
def test_integration_stop_reason(mock_client):
1412+
client = Anthropic(api_key=ANTHROPIC_API_KEY, posthog_client=mock_client)
1413+
client.messages.create(
1414+
model="claude-sonnet-4-20250514",
1415+
messages=[{"role": "user", "content": "Say hi"}],
1416+
max_tokens=5,
1417+
posthog_distinct_id="test-id",
1418+
)
1419+
1420+
props = mock_client.capture.call_args[1]["properties"]
1421+
assert props["$ai_stop_reason"] in ("end_turn", "max_tokens")
1422+
assert props["$ai_provider"] == "anthropic"
1423+
assert props["$ai_input_tokens"] > 0

posthog/test/ai/gemini/test_gemini.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
from unittest.mock import MagicMock, patch
34

45
import pytest
@@ -1286,3 +1287,22 @@ def mock_streaming_response():
12861287
assert mock_client.capture.call_count == 1
12871288
props = mock_client.capture.call_args[1]["properties"]
12881289
assert props["$ai_stop_reason"] == "STOP"
1290+
1291+
1292+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
1293+
1294+
1295+
@pytest.mark.skipif(not GEMINI_API_KEY, reason="GEMINI_API_KEY is not set")
1296+
def test_integration_stop_reason(mock_client):
1297+
client = Client(api_key=GEMINI_API_KEY, posthog_client=mock_client)
1298+
client.models.generate_content(
1299+
model="gemini-2.0-flash",
1300+
contents="Say hi",
1301+
posthog_distinct_id="test-id",
1302+
)
1303+
1304+
props = mock_client.capture.call_args[1]["properties"]
1305+
assert props["$ai_stop_reason"] is not None
1306+
assert isinstance(props["$ai_stop_reason"], str)
1307+
assert props["$ai_provider"] == "gemini"
1308+
assert props["$ai_input_tokens"] > 0

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ def test_openai_chain(mock_client):
894894
)
895895
assert gen_props["$ai_input_tokens"] == 20
896896
assert gen_props["$ai_output_tokens"] == 1
897+
assert gen_props["$ai_stop_reason"] == "stop"
897898

898899

899900
@pytest.mark.skipif(not OPENAI_API_KEY, reason="OpenAI API key not set")
@@ -1172,6 +1173,7 @@ def test_anthropic_chain(mock_client):
11721173
)
11731174
assert gen_props["$ai_input_tokens"] == 17
11741175
assert gen_props["$ai_output_tokens"] == 4
1176+
assert gen_props["$ai_stop_reason"] == "end_turn"
11751177

11761178
assert trace_args["event"] == "$ai_trace"
11771179
assert trace_props["$ai_input_state"] == {}

posthog/test/ai/openai/test_openai.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import time
34
from unittest.mock import AsyncMock, patch
45

@@ -2154,3 +2155,22 @@ async def chunk_iterable():
21542155
props = call_args["properties"]
21552156

21562157
assert props["$ai_model"] == "gpt-4o-mini-async-stored"
2158+
2159+
2160+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
2161+
2162+
2163+
@pytest.mark.skipif(not OPENAI_API_KEY, reason="OPENAI_API_KEY is not set")
2164+
def test_integration_stop_reason(mock_client):
2165+
client = OpenAI(api_key=OPENAI_API_KEY, posthog_client=mock_client)
2166+
client.chat.completions.create(
2167+
model="gpt-4o-mini",
2168+
messages=[{"role": "user", "content": "Say hi"}],
2169+
max_tokens=5,
2170+
posthog_distinct_id="test-id",
2171+
)
2172+
2173+
props = mock_client.capture.call_args[1]["properties"]
2174+
assert props["$ai_stop_reason"] in ("stop", "length")
2175+
assert props["$ai_provider"] == "openai"
2176+
assert props["$ai_input_tokens"] > 0

0 commit comments

Comments
 (0)