diff --git a/litellm/llms/base_llm/chat/transformation.py b/litellm/llms/base_llm/chat/transformation.py index bbe1cc85df1..64546b022cc 100644 --- a/litellm/llms/base_llm/chat/transformation.py +++ b/litellm/llms/base_llm/chat/transformation.py @@ -420,6 +420,16 @@ class BaseConfig(ABC): """ return True + @property + def reserved_request_body_keys(self) -> frozenset[str]: + """ + Request-body keys that ``extra_body`` may not override after + ``transform_request`` has sanitized them (e.g. an allowlisted ``tools`` + list). Empty by default so ``extra_body`` stays a full escape hatch for + providers that do not sanitize their body. + """ + return frozenset() + def post_stream_processing(self, stream: Any) -> Any: """Hook for providers to post-process streaming responses. Default: pass-through.""" return stream diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 71a598a6fe7..07231531f8b 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -546,7 +546,8 @@ class BaseLLMHTTPHandler: ) if extra_body is not None: - data = {**data, **extra_body} + reserved_keys = provider_config.reserved_request_body_keys + data = {**data, **{k: v for k, v in extra_body.items() if k not in reserved_keys}} headers, signed_json_body = provider_config.sign_request( headers=headers, diff --git a/litellm/llms/mistral/conversations/transformation.py b/litellm/llms/mistral/conversations/transformation.py index 1649c8ccbbf..fb7dd362d2a 100644 --- a/litellm/llms/mistral/conversations/transformation.py +++ b/litellm/llms/mistral/conversations/transformation.py @@ -74,6 +74,13 @@ def _to_annotation(chunk: MistralConversationContentChunk) -> ChatCompletionAnno class MistralConversationsConfig(MistralConfig): + @property + def reserved_request_body_keys(self) -> frozenset[str]: + """Sanitized Conversations fields extra_body may not clobber: ``tools`` is + allowlist-checked, ``store`` is pinned to False, and ``inputs`` / ``model`` + are built from the authenticated request.""" + return frozenset({"tools", "store", "inputs", "model"}) + def should_fake_stream( self, model: Optional[str], diff --git a/tests/test_litellm/llms/mistral/conversations/test_mistral_conversations_transformation.py b/tests/test_litellm/llms/mistral/conversations/test_mistral_conversations_transformation.py index 88f4f8f9613..508666d10d9 100644 --- a/tests/test_litellm/llms/mistral/conversations/test_mistral_conversations_transformation.py +++ b/tests/test_litellm/llms/mistral/conversations/test_mistral_conversations_transformation.py @@ -135,6 +135,40 @@ def test_transform_request_drops_other_builtin_connectors(): assert {"type": "code_interpreter"} not in body["tools"] +@pytest.mark.parametrize( + "request_params", + [ + {"tools": [{"type": "web_search"}]}, + {"tools": [{"type": "web_search_premium"}]}, + {"web_search_options": {}}, + {"web_search_options": {"premium": True}}, + {"web_search_options": {"premium": "yes"}}, + {"web_search_options": {}, "tools": [{"type": "code_interpreter"}]}, + {"web_search_options": {}, "tools": [{"type": "WEB_SEARCH"}]}, + {"tools": [{"type": "web_search"}, {"type": "function", "function": {"name": "f"}}]}, + {"web_search_options": {"premium": True}, "tools": [{"type": "web_search"}]}, + ], +) +def test_wire_connectors_never_exceed_allowlist_checked_names(request_params): + """Invariant behind the tool allowlist: every non-function connector the + Conversations transform puts on the wire must have been named by + extract_request_tool_names for the same request, so the key/team allowlist + always sees what will actually be sent. Drift between the extractor and the + transform reopens a bypass; this pins them together.""" + from litellm.proxy.guardrails.tool_name_extraction import extract_request_tool_names + + checked_names = set(extract_request_tool_names("/v1/chat/completions", request_params)) + wire_body = MistralConversationsConfig().transform_request( + model="mistral-medium-latest", + messages=[{"role": "user", "content": "hi"}], + optional_params=dict(request_params), + litellm_params={}, + headers={}, + ) + wire_connectors = {str(tool.get("type")) for tool in wire_body["tools"] if tool.get("type") != "function"} + assert wire_connectors <= checked_names + + def test_transform_request_maps_random_seed_from_extra_body(): cfg = MistralConversationsConfig() body = cfg.transform_request( @@ -299,6 +333,37 @@ async def test_completion_routes_web_search_to_conversations(sync_mode, web_sear assert response.choices[0].message.annotations[0]["url_citation"]["url"] == ("https://uefa.com/euro2024") +@pytest.mark.asyncio +async def test_extra_body_cannot_override_sanitized_tools(respx_mock): + """Regression: extra_body must not clobber the allowlist-checked tools list + (or store/inputs/model) after transform_request sanitized them.""" + litellm.disable_aiohttp_transport = True + + conversations_route = respx_mock.post("https://api.mistral.ai/v1/conversations").respond( + json=CONVERSATIONS_RESPONSE + ) + + litellm.completion( + model="mistral/mistral-medium-latest", + messages=[{"role": "user", "content": "Who won the last Euro?"}], + web_search_options={}, + extra_body={ + "tools": [{"type": "web_search_premium"}], + "store": True, + "model": "mistral-large-latest", + "completion_args": {"temperature": 0.9}, + }, + ) + + import json + + sent = json.loads(conversations_route.calls[0].request.content) + assert sent["tools"] == [{"type": "web_search"}] + assert sent["store"] is False + assert sent["model"] == "mistral-medium-latest" + assert sent["completion_args"] == {"temperature": 0.9} + + @pytest.mark.asyncio async def test_completion_without_web_search_uses_chat_completions(respx_mock): litellm.disable_aiohttp_transport = True