diff --git a/.sampo/changesets/anthropic-stream-request-error.md b/.sampo/changesets/anthropic-stream-request-error.md new file mode 100644 index 000000000..89d40b741 --- /dev/null +++ b/.sampo/changesets/anthropic-stream-request-error.md @@ -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. diff --git a/posthog/ai/anthropic/anthropic.py b/posthog/ai/anthropic/anthropic.py index ef4c4dacb..957cdd380 100644 --- a/posthog/ai/anthropic/anthropic.py +++ b/posthog/ai/anthropic/anthropic.py @@ -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, diff --git a/posthog/ai/anthropic/anthropic_async.py b/posthog/ai/anthropic/anthropic_async.py index 80014ce91..baf13c821 100644 --- a/posthog/ai/anthropic/anthropic_async.py +++ b/posthog/ai/anthropic/anthropic_async.py @@ -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, diff --git a/posthog/test/ai/anthropic/test_anthropic.py b/posthog/test/ai/anthropic/test_anthropic.py index d14847a94..22e8d1767 100644 --- a/posthog/test/ai/anthropic/test_anthropic.py +++ b/posthog/test/ai/anthropic/test_anthropic.py @@ -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) @@ -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, ):