@@ -971,151 +971,6 @@ def mock_streaming_response():
971971 assert props ["$ai_output_tokens" ] == 15
972972
973973
974- @pytest .mark .asyncio
975- async def test_async_with_web_search (mock_client , mock_google_genai_client ):
976- """Test that web search count is properly tracked in async non-streaming mode."""
977-
978- # Create mock response with grounding metadata
979- mock_response = MagicMock ()
980-
981- # Mock usage metadata
982- mock_usage = MagicMock ()
983- mock_usage .prompt_token_count = 60
984- mock_usage .candidates_token_count = 40
985- mock_usage .cached_content_token_count = 0
986- mock_usage .thoughts_token_count = 0
987- mock_response .usage_metadata = mock_usage
988-
989- # Mock grounding metadata
990- mock_grounding_chunk = MagicMock ()
991- mock_grounding_chunk .uri = "https://example.com"
992-
993- mock_grounding_metadata = MagicMock ()
994- mock_grounding_metadata .grounding_chunks = [mock_grounding_chunk ]
995-
996- # Mock text part
997- mock_text_part = MagicMock ()
998- mock_text_part .text = "According to search results..."
999- type(mock_text_part ).text = mock_text_part .text
1000-
1001- # Mock content with parts
1002- mock_content = MagicMock ()
1003- mock_content .parts = [mock_text_part ]
1004-
1005- # Mock candidate with grounding metadata
1006- mock_candidate = MagicMock ()
1007- mock_candidate .content = mock_content
1008- mock_candidate .grounding_metadata = mock_grounding_metadata
1009- type(mock_candidate ).grounding_metadata = mock_candidate .grounding_metadata
1010-
1011- mock_response .candidates = [mock_candidate ]
1012- mock_response .text = "According to search results..."
1013-
1014- # Mock the async generate_content method
1015- async def mock_async_generate_content (* args , ** kwargs ):
1016- return mock_response
1017-
1018- mock_google_genai_client .models .generate_content_async = mock_async_generate_content
1019-
1020- client = Client (api_key = "test-key" , posthog_client = mock_client )
1021-
1022- async def run_test ():
1023- response = await client .models .generate_content_async (
1024- model = "gemini-2.5-flash" ,
1025- contents = "What's the latest news?" ,
1026- posthog_distinct_id = "test-id" ,
1027- )
1028- return response
1029-
1030- response = await run_test ()
1031-
1032- assert response == mock_response
1033- assert mock_client .capture .call_count == 1
1034-
1035- call_args = mock_client .capture .call_args [1 ]
1036- props = call_args ["properties" ]
1037-
1038- # Verify web search count is detected (binary for grounding)
1039- assert props ["$ai_web_search_count" ] == 1
1040- assert props ["$ai_input_tokens" ] == 60
1041- assert props ["$ai_output_tokens" ] == 40
1042-
1043-
1044- @pytest .mark .asyncio
1045- async def test_async_streaming_with_web_search (mock_client , mock_google_genai_client ):
1046- """Test that web search count is properly captured in async streaming mode."""
1047-
1048- async def mock_async_streaming_response ():
1049- # Create chunk 1 with grounding metadata
1050- mock_chunk1 = MagicMock ()
1051- mock_chunk1 .text = "According to "
1052-
1053- mock_usage1 = MagicMock ()
1054- mock_usage1 .prompt_token_count = 30
1055- mock_usage1 .candidates_token_count = 5
1056- mock_usage1 .cached_content_token_count = 0
1057- mock_usage1 .thoughts_token_count = 0
1058- mock_chunk1 .usage_metadata = mock_usage1
1059-
1060- # Add grounding metadata to first chunk
1061- mock_grounding_chunk = MagicMock ()
1062- mock_grounding_chunk .uri = "https://example.com"
1063-
1064- mock_grounding_metadata = MagicMock ()
1065- mock_grounding_metadata .grounding_chunks = [mock_grounding_chunk ]
1066-
1067- mock_candidate1 = MagicMock ()
1068- mock_candidate1 .grounding_metadata = mock_grounding_metadata
1069- type(mock_candidate1 ).grounding_metadata = mock_candidate1 .grounding_metadata
1070-
1071- mock_chunk1 .candidates = [mock_candidate1 ]
1072-
1073- # Create chunk 2
1074- mock_chunk2 = MagicMock ()
1075- mock_chunk2 .text = "search results..."
1076-
1077- mock_usage2 = MagicMock ()
1078- mock_usage2 .prompt_token_count = 30
1079- mock_usage2 .candidates_token_count = 15
1080- mock_usage2 .cached_content_token_count = 0
1081- mock_usage2 .thoughts_token_count = 0
1082- mock_chunk2 .usage_metadata = mock_usage2
1083-
1084- mock_candidate2 = MagicMock ()
1085- mock_chunk2 .candidates = [mock_candidate2 ]
1086-
1087- yield mock_chunk1
1088- yield mock_chunk2
1089-
1090- # Mock the async generate_content_stream method
1091- mock_google_genai_client .models .generate_content_stream_async = (
1092- mock_async_streaming_response
1093- )
1094-
1095- client = Client (api_key = "test-key" , posthog_client = mock_client )
1096-
1097- response = await client .models .generate_content_stream_async (
1098- model = "gemini-2.5-flash" ,
1099- contents = "What's the latest news?" ,
1100- posthog_distinct_id = "test-id" ,
1101- )
1102-
1103- chunks = []
1104- async for chunk in response :
1105- chunks .append (chunk )
1106-
1107- assert len (chunks ) == 2
1108- assert mock_client .capture .call_count == 1
1109-
1110- call_args = mock_client .capture .call_args [1 ]
1111- props = call_args ["properties" ]
1112-
1113- # Verify web search count is detected (binary for grounding)
1114- assert props ["$ai_web_search_count" ] == 1
1115- assert props ["$ai_input_tokens" ] == 30
1116- assert props ["$ai_output_tokens" ] == 15
1117-
1118-
1119974def test_empty_grounding_metadata_no_web_search (mock_client , mock_google_genai_client ):
1120975 """Test that empty grounding_metadata (all null fields) does not count as web search."""
1121976
0 commit comments