Skip to content

Commit 2468618

Browse files
committed
fix(llma): fix test
1 parent 4d34ffd commit 2468618

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

posthog/ai/anthropic/anthropic_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async def _create_streaming(
136136
content_blocks: List[StreamingContentBlock] = []
137137
tools_in_progress: Dict[str, ToolInProgress] = {}
138138
current_text_block: Optional[StreamingContentBlock] = None
139-
response = super().create(**kwargs)
139+
response = await super().create(**kwargs)
140140

141141
async def generator():
142142
nonlocal usage_stats

posthog/ai/gemini/gemini_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ def extract_gemini_usage_from_chunk(chunk: Any) -> StreamingUsageStats:
303303
usage["input_tokens"] = getattr(chunk.usage_metadata, "prompt_token_count", 0)
304304
usage["output_tokens"] = getattr(chunk.usage_metadata, "candidates_token_count", 0)
305305

306-
# Calculate total if both are present
307-
if usage["input_tokens"] and usage["output_tokens"]:
306+
# Calculate total if both values are defined (including 0)
307+
if "input_tokens" in usage and "output_tokens" in usage:
308308
usage["total_tokens"] = usage["input_tokens"] + usage["output_tokens"]
309309

310310
return usage

posthog/ai/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,6 @@ def capture_streaming_event(
503503
ph_client: PostHog client instance
504504
event_data: Standardized streaming event data containing all necessary information
505505
"""
506-
import uuid
507-
508506
trace_id = event_data.get("trace_id") or str(uuid.uuid4())
509507

510508
# Build base event properties

posthog/test/ai/anthropic/test_anthropic.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,14 @@ async def mock_async_stream():
480480
)
481481
yield final_msg
482482

483-
# Mock create to return the async generator directly (not wrapped in a coroutine)
484-
# This matches the actual behavior when stream=True
483+
# Mock create to return a coroutine that yields the async generator
484+
# This matches the actual behavior when stream=True with await
485+
async def async_create_wrapper(**kwargs):
486+
return mock_async_stream()
487+
485488
with patch(
486489
"anthropic.resources.messages.AsyncMessages.create",
487-
return_value=mock_async_stream(),
490+
side_effect=async_create_wrapper,
488491
):
489492
client = AsyncAnthropic(posthog_client=mock_client)
490493
response = await client.messages.create(
@@ -926,14 +929,18 @@ def test_async_streaming_with_tool_calls(mock_client, mock_anthropic_stream_with
926929
"""Test that tool calls are properly captured in async streaming mode."""
927930
import asyncio
928931

929-
async def mock_async_create(**kwargs):
932+
async def mock_async_generator():
930933
# Convert regular generator to async generator
931934
for event in mock_anthropic_stream_with_tools:
932935
yield event
936+
937+
async def mock_async_create(**kwargs):
938+
# Return the async generator (to be awaited by the implementation)
939+
return mock_async_generator()
933940

934941
with patch(
935942
"anthropic.resources.AsyncMessages.create",
936-
side_effect=lambda **kwargs: mock_async_create(),
943+
side_effect=mock_async_create,
937944
):
938945
async_client = AsyncAnthropic(api_key="test-key", posthog_client=mock_client)
939946

0 commit comments

Comments
 (0)