litellm/tests/unit/proxy/google_endpoints
zoveki 315c9d0d0e
fix(proxy): count a contents-only body on /utils/token_counter (#50710)
* fix(proxy): count a contents-only body on /utils/token_counter

The route now fills messages from Google-style contents when the caller sent none, using the same translation as the Google countTokens route, so the local count and the framed fallback can read the conversation instead of answering 500

* test(proxy): pin that a prompt sent with contents drops like one sent with messages

* test(integration): expect the Vertex partner custom_tokenizer to count a contents-only body

* fix(proxy): count Google bodies on /utils/token_counter like the countTokens route and reject unreadable contents

The route now maps systemInstruction and Google functionDeclarations tools through
the countTokens route's own translation, keeping the caller's system and OpenAI or
Anthropic tools. A contents entry without a parts list, or an unreadable
systemInstruction, answers 400 naming the field on both count routes instead of 500.
Same-name function calls in one turn get numbered ids that their responses answer,
so Claude no longer rejects them as duplicate tool results

* fix(google_genai): pair each function response with its call by id or order across turns

A function response now answers the call that carries its id, else the
oldest unanswered call of the same name, even when the responses come in a
later turn. Calls without an id get call_<name>, then numbered ids that skip
any id the conversation already uses, so search and search_1 no longer clash.
The pairing sorts once instead of scanning every earlier call.

An unreadable systemInstruction now answers 400 with the JSON path of the bad
field and the kind of value it expected.

* fix(google_genai): keep tool call ids short and stable and skip system media on /utils

Caller ids now only pair each function response with its call. They are no
longer sent upstream, so a long caller id no longer gets OpenAI's 64 character
400. A null or non-string id is read as no id instead of dropping the part.

The first call of each name keeps call_<name>, and a later call of the same
name gets call<n> from its position in the conversation. Ids no longer grow
with the name, and an earlier turn keeps its ids when later turns add calls.

/utils/token_counter counts the text of a systemInstruction and skips a part
with only media, instead of answering 400 for a body Google accepts.

* fix(google_genai): pair a tool response with the latest call before it and count entries without parts

A function response without a matching caller id now pairs with the earliest
unanswered call of the same name in the latest model turn before it, so a call
the user skipped stays unanswered and the later call keeps its result.

A contents entry without parts counts as an empty turn on both count routes,
the way Google's own countTokens and the generateContent route read it, and
/utils/token_counter names the exact field when parts or an entry has the
wrong type.

* fix(gemini): count a contents entry without parts instead of answering 500

The Gemini count handler read parts off every entry, so a contents entry
without parts, which Google counts as an empty turn, answered 500 on both
count routes. It now sends that entry to Google as the caller wrote it.

* fix(google_genai): pair reused caller ids and answer skipped calls

A function response now pairs with the latest unanswered call that has its
caller id, so ids reused across turns or restarted per turn pair with the call
they were sent for. A call a later turn left unanswered gets the same skipped
result modify_params adds, so OpenAI and Bedrock Converse accept the
conversation. /utils/token_counter keeps an OpenAI tool sent without type

* fix(google_genai): list skipped results after the answers to their turn

A model reads parallel tool results in order, so a skipped result listed before
the caller's answers made GPT give Paris the skipped result and Rome the Paris
forecast. A skipped result now follows the answers the next turn sent, which
is the order the modify_params filler uses

* fix(google_genai): list skipped results after every answer to their turn

When the caller split the answers to one model turn across two user turns, the
skipped result went between them and GPT read it as the second answer. A
skipped result now follows the last user turn that answers its model turn

* fix(google_genai): keep skipped results after answers split by empty or text turns

A skipped result now goes after the last answer before the next model turn. An empty or text-only user turn between split answers used to end the search early, so GPT read the later answer as the skipped call's result

* revert(google_genai): leave Google tool call pairing out of the token counter fix

Repeated function call pairing in GoogleGenAIAdapter is a separate translation bug that also affects
countTokens and generateContent, so it moves to its own change. This restores the adapter, the shared
skipped tool result helper, and their tests to the merge base.

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-26 01:21:21 +00:00
..
__init__.py
test_endpoints.py fix(proxy): count a contents-only body on /utils/token_counter (#50710) 2026-11-26 01:21:21 +00:00
test_gemini_agents_endpoints.py fix(gemini)!: accept Google's current Managed Agents fields on create and interactions (#48750) 2026-10-28 12:37:59 -07:00
test_google_api_endpoints.py fix(proxy): answer x-litellm-call-id on every token count response (#51037) 2026-11-11 22:26:47 +00:00
test_google_endpoint_routing.py fix(proxy): return 4xx instead of 500 for missing required params, invalid pagination and unknown ids (#43787) 2026-10-02 22:48:53 -07:00
test_google_gemini_proxy_request.py
test_interactions_agent_param.py fix(gemini)!: accept Google's current Managed Agents fields on create and interactions (#48750) 2026-10-28 12:37:59 -07:00
test_managed_agents_model_param.py test(proxy): move utils, agent_endpoints and endpoint tests into tests/unit/proxy (#44006) 2026-10-01 11:06:42 -07:00