From 6fc56e6fe260119f9fd9d0f8dddd1915563ca63a Mon Sep 17 00:00:00 2001 From: Nikhil Maguluri <33579914+nikhilmaguluri@users.noreply.github.com> Date: Sun, 20 Sep 2026 15:46:35 -0400 Subject: [PATCH] fix: handle top-level Gemini strict constraints --- .../vertex_and_google_ai_studio_gemini.py | 10 ++++---- ...test_vertex_and_google_ai_studio_gemini.py | 24 +++++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py index e46f6b7318f..daf52daf765 100644 --- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py +++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py @@ -589,11 +589,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): @staticmethod def _tools_have_strict_constraint(tools: Sequence[Mapping[str, object]]) -> bool: for tool in tools: + if "strict" in tool: + return True function_chunk = tool.get("function") - if isinstance(function_chunk, Mapping): - if "strict" in function_chunk: - return True - elif "strict" in tool: + if isinstance(function_chunk, Mapping) and "strict" in function_chunk: return True return False @@ -629,11 +628,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): "See https://github.com/BerriAI/litellm/issues/41913" ) for tool in mapped_value: + tool.pop("strict", None) function_chunk = tool.get("function") if isinstance(function_chunk, dict): function_chunk.pop("strict", None) - else: - tool.pop("strict", None) for tool in mapped_value: openai_function_object: ChatCompletionToolParamFunctionChunk | None = None diff --git 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 index 6dd77a5f623..0781fcfec9f 100644 --- 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 @@ -6313,8 +6313,6 @@ def _strict_function_tool(): def test_map_function_drops_strict_with_warning(caplog): - """Regression test for BerriAI/litellm#41913: Gemini has no `strict` support, - so it is dropped from the declaration and the drop is logged, not silent.""" config = VertexGeminiConfig() with caplog.at_level("WARNING", logger="LiteLLM"): @@ -6325,9 +6323,21 @@ def test_map_function_drops_strict_with_warning(caplog): assert any("'strict'" in record.message for record in caplog.records) +def test_map_function_drops_top_level_strict_with_warning(caplog): + config = VertexGeminiConfig() + tools = _strict_function_tool() + del tools[0]["function"]["strict"] + tools[0]["strict"] = True + + with caplog.at_level("WARNING", logger="LiteLLM"): + mapped = config._map_function(value=tools, optional_params={}) + + declaration = mapped[0]["function_declarations"][0] + assert "strict" not in declaration + assert any("'strict'" in record.message for record in caplog.records) + + def test_map_function_drops_allowed_callers_with_warning(caplog): - """Regression test for BerriAI/litellm#41913: Gemini has no `allowed_callers` - support, so it is dropped from the declaration and the drop is logged.""" config = VertexGeminiConfig() tools = _strict_function_tool() tools[0]["function"]["allowed_callers"] = ["direct"] @@ -6344,8 +6354,6 @@ def test_map_function_drops_allowed_callers_with_warning(caplog): def test_map_function_does_not_warn_for_nested_schema_property_named_strict(caplog): - """A JSON Schema property legitimately named `strict` is unrelated to the - unsupported tool-level `strict` constraint and must not trigger the warning.""" config = VertexGeminiConfig() tools = _strict_function_tool() del tools[0]["function"]["strict"] @@ -6360,8 +6368,6 @@ def test_map_function_does_not_warn_for_nested_schema_property_named_strict(capl def test_map_function_does_not_mutate_input_tools(caplog): - """The caller's tool objects must be returned unchanged; dropped fields are - only omitted from the mapped Gemini declarations.""" config = VertexGeminiConfig() tools = _strict_function_tool() tools[0]["function"]["allowed_callers"] = ["direct"] @@ -6374,8 +6380,6 @@ def test_map_function_does_not_mutate_input_tools(caplog): def test_map_openai_params_drops_parallel_tool_calls_false_with_warning(caplog): - """Regression test for BerriAI/litellm#41913: `parallel_tool_calls=False` with - multiple tools is unsupported by Gemini, so it is dropped and the drop is logged.""" config = VertexGeminiConfig() tools = [ {