Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_fix_gemini_web_search_unique_queries_36377

mateo-berri9003b02c3c92
diff --cc tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
index 3a04424a581,790a32506f2..bfe12498dd6
--- a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
+++ b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
@@@ -5555,23 -5563,19 +5563,41 @@@ def test_accumulated_json_skips_non_dic
assert out[0].choices[0].delta.content == "a"
+ def test_accumulated_json_async_end_of_stream_drains_buffered_value():
+ """Async twin of test_accumulated_json_end_of_stream_drains_all_buffered_values:
+ __anext__'s StopAsyncIteration branch must also parse a buffered value."""
+ import asyncio
+ from unittest.mock import AsyncMock, MagicMock
+
+ obj = '{"candidates":[{"content":{"parts":[{"text":"a"}]}}],"usageMetadata":{}}'
+ iterator = _accumulating_gemini_iterator()
+ iterator.accumulated_json = obj
+ mock_async_iterator = MagicMock()
+ mock_async_iterator.__anext__ = AsyncMock(side_effect=StopAsyncIteration)
+ iterator.async_response_iterator = mock_async_iterator
+
+ result = asyncio.run(iterator.__anext__())
+ assert result is not None
+ assert result.choices[0].delta.content == "a"
++
++
+def test_calculate_web_search_requests_counts_unique_queries():
+ """Gemini 3 per_query billing charges per unique query executed, not per emitted string.
+
+ Regression for #36377: duplicate webSearchQueries within and across grounding
+ metadata items must collapse to the distinct-query count, and empty strings must
+ be ignored, matching Google's documented Grounding-with-Search billing rule.
+ """
+ duplicates_in_one_item: Final = [
+ {"webSearchQueries": ["euro 2024 winner", "euro 2024 winner", "spain england final", ""]}
+ ]
+ assert VertexGeminiConfig._calculate_web_search_requests(duplicates_in_one_item) == 2
+
+ duplicates_across_items: Final = [
+ {"webSearchQueries": ["euro 2024 winner"]},
+ {"webSearchQueries": ["euro 2024 winner", "spain england final"]},
+ ]
+ assert VertexGeminiConfig._calculate_web_search_requests(duplicates_across_items) == 2
+
+ assert VertexGeminiConfig._calculate_web_search_requests([]) is None
+ assert VertexGeminiConfig._calculate_web_search_requests([{"webSearchQueries": ["", ""]}]) is None