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/anthropic-stream-request-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

`posthog.ai.anthropic`: `messages.stream()` (sync and async) now captures an `$ai_generation` event with `$ai_is_error` when the initial request fails, for example with a 401 or a connection error. Before, only `messages.create(stream=True)` captured that failure, so errors raised on entering the `stream()` context manager were never reported.
23 changes: 22 additions & 1 deletion posthog/ai/anthropic/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,28 @@ def stream(

def tracked_request():
start_time = time.time()
response = request()
try:
response = request()
except Exception as exc:
accumulator = _AnthropicStreamAccumulator()
try:
self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
kwargs,
accumulator.usage_stats,
time.time() - start_time,
accumulator.content_blocks,
accumulator.accumulated_content,
error=exc,
)
except Exception:
# Telemetry must never replace the provider's own error.
_log.warning("[PostHog] Failed to capture streaming connect error")
raise
return self._track_streaming_response(
response,
posthog_distinct_id,
Expand Down
23 changes: 22 additions & 1 deletion posthog/ai/anthropic/anthropic_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,28 @@ def stream(

async def tracked_request():
start_time = time.time()
response = await request
try:
response = await request
except Exception as exc:
accumulator = _AnthropicStreamAccumulator()
try:
await self._capture_streaming_event(
posthog_distinct_id,
posthog_trace_id,
posthog_properties,
posthog_privacy_mode,
posthog_groups,
kwargs,
accumulator.usage_stats,
time.time() - start_time,
accumulator.content_blocks,
accumulator.accumulated_content,
error=exc,
)
except Exception:
# Telemetry must never replace the provider's own error.
_log.warning("[PostHog] Failed to capture streaming connect error")
raise
return self._track_streaming_response(
response,
posthog_distinct_id,
Expand Down
41 changes: 41 additions & 0 deletions posthog/test/ai/anthropic/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,26 @@ def test_messages_stream_preserves_native_manager_helpers_close_and_tracking(
assert "posthog_distinct_id" not in client.post.call_args.kwargs


def test_messages_stream_captures_initial_request_error(mock_client):
client = Anthropic(api_key="test-key", posthog_client=mock_client)
client.post = Mock(side_effect=RuntimeError("401 unauthorized"))

manager = client.messages.stream(
model="claude-haiku-4-5",
messages=[{"role": "user", "content": "Foo"}],
max_tokens=1,
posthog_distinct_id="test-user",
)
with pytest.raises(RuntimeError, match="401 unauthorized"):
with manager:
pass

assert mock_client.capture.call_count == 1
props = mock_client.capture.call_args.kwargs["properties"]
assert props["$ai_is_error"] is True
assert props["$ai_error"] == "401 unauthorized"


def test_messages_stream_tolerates_provider_manager_internals_changing(mock_client):
manager = object()
client = Anthropic(api_key="test-key", posthog_client=mock_client)
Expand Down Expand Up @@ -2316,6 +2336,27 @@ async def test_async_messages_stream_preserves_provider_contract_and_manager(
assert "posthog_distinct_id" not in client.post.call_args.kwargs


@pytest.mark.asyncio
async def test_async_messages_stream_captures_initial_request_error(mock_client):
client = AsyncAnthropic(api_key="test-key", posthog_client=mock_client)
client.post = AsyncMock(side_effect=RuntimeError("401 unauthorized"))

manager = client.messages.stream(
model="claude-haiku-4-5",
messages=[{"role": "user", "content": "Foo"}],
max_tokens=1,
posthog_distinct_id="test-user",
)
with pytest.raises(RuntimeError, match="401 unauthorized"):
async with manager:
pass

assert mock_client.capture.call_count == 1
props = mock_client.capture.call_args.kwargs["properties"]
assert props["$ai_is_error"] is True
assert props["$ai_error"] == "401 unauthorized"


def test_async_messages_stream_tolerates_provider_manager_internals_changing(
mock_client,
):
Expand Down
Loading