litellm/tests/unit/proxy/google_endpoints
zoveki bc4ca0b218
fix(guardrails): check one-object parts and tool turns on generateContent (#54359)
* fix(google_genai): scan one-object parts in guardrails and skip null-text system parts that carry only metadata

* fix(google_endpoints): keep countTokens fail-closed on unknown Part fields and accept metadata-only system parts

* fix(google_genai): scan string parts, every system instruction, and tool turns in guardrails

The generateContent guardrail now scans plain-string parts, both system
instruction spellings at the top level and inside config, every string in a
functionResponse.response (skipped when the guardrail skips tool messages), and
functionCall args passed as chat-shaped tool_calls. Masked text is written back
in place so the provider gets the caller's wire shape. A functionResponse nested
past DEFAULT_MAX_RECURSE_DEPTH is blocked with a 400 instead of going out unscanned

The countTokens test now covers all eight metadata spellings

* fix(google_genai): scan tool results and code parts as one JSON text, and give each tool call its own id

Scanning each string of a functionResponse on its own fanned one tool result
out into hundreds of guardrail texts, needed a depth cap that turned a clean
deep result into a 400, and missed keys and numbers. Each structured value is
now one JSON text, the way chat scans a tool message, and a change is decoded
back into the part. executableCode, codeExecutionResult, toolCall and
toolResponse parts are scanned too. Function calls reach guardrails under the
call's own id, and their args are written back only when every call comes back

* fix(guardrails): scan generateContent tool results with real line starts and maskable numbers

Tool results keep raw control characters in their scan text so line-start
patterns match, numbers are scanned as strings so a mask replaces them, and a
guardrail that turns functionCall args into a non-object blocks the request

* fix(guardrails): scan generateContent functionCall args as JSON text and check tool calls in model replies

* fix(google_endpoints): keep metadata-only countTokens system parts answering 400, as Gemini does

* fix(guardrails): check tool calls in streamed genai replies and reject unwritable rewrites as unappliable

A post_call guardrail now sees functionCall args in streamGenerateContent chunks, and a rewrite that no longer reads back as JSON raises UnappliableRequestRewrite so a policy pipeline blocks instead of falling through

* fix(guardrails): match chat's scope flags and count checks on generateContent

scan_only_tool_results and skip_system_message_in_guardrail now narrow what a generateContent guardrail sees, as on chat. A request rewrite that changes the text or tool call count, or touches a string in a tuple, is rejected as unappliable instead of sent unmasked. A reply whose tool calls come back in a different count keeps the model's calls, as on chat. Masked numbers in a tool result keep their type when the guardrail reorders fields

* fix(guardrails): keep generateContent tool calls and reply texts the way chat does

A request guardrail that returns a different tool call count, such as an
empty list, now keeps the original calls, as chat does. Reply texts are
written back by index, so a guardrail that answers one text for a reply
with several parts still replaces the first one, as on chat and
/v1/messages. Stream text gets a line break between parts that are not
both text, so two parts cannot spell a blocked word together. Numbers
nested past the scan depth cap keep their type when another value is
masked. The module docstring now states only the rules.

* fix(google-genai): write guardrail tool calls back by position with their names, and keep list numbers typed

A guardrail that returns fewer tool calls now has them applied by position, as on chat,
so a mask on a kept call is no longer lost. A renamed call goes out under its new name,
and numbers in a tool result list a guardrail shortens keep their type.

* fix(google-genai): match kept tool result list items in order so numeric strings stay strings

A guardrail that drops a list item no longer looks originals up by their scan text,
where "1" and 1 share one key. Kept items are matched in order against the
originals instead, so each keeps its own type

* fix(google-genai): reject tool call count changes and keep chat scope flags from narrowing text

A guardrail that drops some but not all tool calls now answers the unappliable error instead of writing calls back by position, on requests and replies. skip_system_message_in_guardrail and scan_only_tool_results no longer skip system or user text, matching chat. List items keep their original values when a mask leaves them unchanged, aligned by content and then by shape

* fix(guardrails): keep streamed text whole around code parts, write a one-text reply verdict to the first text, bound list alignment

* fix(guardrails): keep the last text a reply guardrail sees on the reply's text, so last-text guardrails scan the answer

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-25 12:32:47 +00:00
..
__init__.py
test_endpoints.py fix(guardrails): check one-object parts and tool turns on generateContent (#54359) 2026-11-25 12:32:47 +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