From cf6dda5e2907ef3a49a70c9494f3dbd0db8eac18 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Thu, 27 Nov 2025 22:46:28 +0530 Subject: [PATCH] block input_examples in fucntion definition for non anthropic providers --- litellm/main.py | 36 +++++++++++++++++++++++++++++++++ tests/test_litellm/test_main.py | 26 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/litellm/main.py b/litellm/main.py index 3e51785ac63..a02e465448b 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -938,6 +938,37 @@ def responses_api_bridge_check( return model_info, model +def _should_allow_input_examples(custom_llm_provider: Optional[str], model: str) -> bool: + if custom_llm_provider == "anthropic": + return True + if custom_llm_provider == "azure_ai" or custom_llm_provider == "bedrock" or custom_llm_provider == "vertex_ai": + return "claude" in model.lower() + return False + + +def _drop_input_examples_from_tool(tool: dict) -> dict: + tool_copy = tool.copy() + tool_copy.pop("input_examples", None) + function = tool_copy.get("function") + if isinstance(function, dict): + function = function.copy() + function.pop("input_examples", None) + tool_copy["function"] = function + return tool_copy + + +def _drop_input_examples_from_tools(tools: Optional[List[dict]]) -> Optional[List[dict]]: + if tools is None: + return None + cleaned_tools: List[dict] = [] + for tool in tools: + if isinstance(tool, dict): + cleaned_tools.append(_drop_input_examples_from_tool(tool)) + else: + cleaned_tools.append(tool) + return cleaned_tools + + @tracer.wrap() @client def completion( # type: ignore # noqa: PLR0915 @@ -1183,6 +1214,11 @@ def completion( # type: ignore # noqa: PLR0915 api_key=api_key, ) + if not _should_allow_input_examples( + custom_llm_provider=custom_llm_provider, model=model + ): + tools = _drop_input_examples_from_tools(tools=tools) + if provider_specific_header is not None: headers.update( ProviderSpecificHeaderUtils.get_provider_specific_headers( diff --git a/tests/test_litellm/test_main.py b/tests/test_litellm/test_main.py index ade512d6f65..71ede0958f5 100644 --- a/tests/test_litellm/test_main.py +++ b/tests/test_litellm/test_main.py @@ -16,6 +16,8 @@ from unittest.mock import MagicMock, patch import litellm +from litellm import main as litellm_main + @pytest.fixture(autouse=True) def add_api_keys_to_env(monkeypatch): @@ -293,6 +295,30 @@ def test_bedrock_latency_optimized_inference(): assert json_data["performanceConfig"]["latency"] == "optimized" +def test_strip_input_examples_for_non_anthropic_providers(): + tools = [ + { + "type": "function", + "name": "example_tool", + "input_examples": [{"foo": "bar"}], + "function": { + "name": "example_tool", + "input_examples": [{"foo": "bar"}], + }, + } + ] + + assert not litellm_main._should_allow_input_examples( + custom_llm_provider="openai", model="gpt-4o-mini" + ) + + cleaned = litellm_main._drop_input_examples_from_tools(tools=tools) + + assert isinstance(cleaned, list) + assert "input_examples" not in cleaned[0] + assert "input_examples" not in cleaned[0]["function"] + + def test_custom_provider_with_extra_headers(): from litellm.llms.custom_httpx.http_handler import HTTPHandler