From 2b575169dcaecc51c2334acb11a4a356c453c7db Mon Sep 17 00:00:00 2001 From: ElDjeee Date: Mon, 6 Jul 2026 17:19:10 +0200 Subject: [PATCH] fix(mistral): map seed directly into optional_params so it reaches completion_args map_openai_params stuffed seed into optional_params["extra_body"], but the HTTP handler pops extra_body before transform_request runs and merges it at the top level of the final body afterwards. The chat body happened to accept top-level random_seed so the bug was invisible there, but on the Conversations route the transform never saw the seed and the merge placed random_seed at the top level of the body instead of inside completion_args. The mapping now writes optional_params["random_seed"] directly, the way watsonx and codestral map their seed equivalents, which also stops the mapping from overwriting a caller-supplied extra_body; the chat wire body is unchanged and the Conversations transform picks random_seed up as a plain completion arg The old unit test called transform_request with extra_body still present in optional_params, a state that cannot occur in the real pipeline, so it passed against the broken code. It is replaced by a respx test that drives litellm.completion(seed=...) through the full pipeline and asserts the seed lands in completion_args and never at the top level; the test fails against the previous code. Also covers the chat path (seed maps to random_seed, stays top-level in the chat body) and drops the unused MistralConversationInputMessage TypedDict --- litellm/llms/mistral/chat/transformation.py | 2 +- .../mistral/conversations/transformation.py | 8 +-- litellm/types/llms/mistral.py | 5 -- ...st_mistral_conversations_transformation.py | 29 +++++++++- .../test_mistral_chat_transformation.py | 57 +++++++++++++++++++ 5 files changed, 87 insertions(+), 14 deletions(-) diff --git a/litellm/llms/mistral/chat/transformation.py b/litellm/llms/mistral/chat/transformation.py index dda9d46683f..5460250efde 100644 --- a/litellm/llms/mistral/chat/transformation.py +++ b/litellm/llms/mistral/chat/transformation.py @@ -168,7 +168,7 @@ class MistralConfig(OpenAIGPTConfig): if param == "tool_choice" and isinstance(value, str): optional_params["tool_choice"] = self._map_tool_choice(tool_choice=value) if param == "seed": - optional_params["extra_body"] = {"random_seed": value} + optional_params["random_seed"] = value if param == "reasoning_effort" and "magistral" in model.lower(): # Flag that we need to add reasoning system prompt optional_params["_add_reasoning_prompt"] = True diff --git a/litellm/llms/mistral/conversations/transformation.py b/litellm/llms/mistral/conversations/transformation.py index ab3e9c2b215..a497e406567 100644 --- a/litellm/llms/mistral/conversations/transformation.py +++ b/litellm/llms/mistral/conversations/transformation.py @@ -44,6 +44,7 @@ _COMPLETION_ARG_KEYS: tuple[str, ...] = ( "max_tokens", "stop", "response_format", + "random_seed", ) @@ -133,12 +134,7 @@ class MistralConversationsConfig(MistralConfig): @staticmethod def _build_completion_args(optional_params: dict[str, object]) -> dict[str, object]: - base = {key: optional_params[key] for key in _COMPLETION_ARG_KEYS if optional_params.get(key) is not None} - extra_body = optional_params.get("extra_body") - random_seed = ( - STR_OBJ_DICT.validate_python(extra_body).get("random_seed") if isinstance(extra_body, dict) else None - ) - return {**base, **({"random_seed": random_seed} if random_seed is not None else {})} + return {key: optional_params[key] for key in _COMPLETION_ARG_KEYS if optional_params.get(key) is not None} @staticmethod def _input_entries_for_message(message: AllMessageValues) -> tuple[dict[str, object], ...]: diff --git a/litellm/types/llms/mistral.py b/litellm/types/llms/mistral.py index b062bf27692..fdbb9ffa3b8 100644 --- a/litellm/types/llms/mistral.py +++ b/litellm/types/llms/mistral.py @@ -25,11 +25,6 @@ class MistralThinkingBlock(TypedDict): thinking: list[MistralTextBlock] -class MistralConversationInputMessage(TypedDict): - role: str - content: str - - class MistralConversationContentChunk(BaseModel): """A single chunk of a Conversations API ``message.output`` content list. 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 a7f29ffaf72..3207a461ab9 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 @@ -201,12 +201,12 @@ def test_wire_connectors_never_exceed_allowlist_checked_names(request_params): assert wire_connectors <= checked_names -def test_transform_request_maps_random_seed_from_extra_body(): +def test_transform_request_maps_random_seed(): cfg = MistralConversationsConfig() body = cfg.transform_request( model="mistral-medium-latest", messages=[{"role": "user", "content": "hi"}], - optional_params={"web_search_options": {}, "extra_body": {"random_seed": 7}}, + optional_params={"web_search_options": {}, "random_seed": 7}, litellm_params={}, headers={}, ) @@ -421,6 +421,31 @@ async def test_extra_body_cannot_override_sanitized_tools(respx_mock): assert sent["completion_args"] == {"temperature": 0.9} +@pytest.mark.asyncio +async def test_completion_seed_lands_in_completion_args(respx_mock): + """Regression: seed must survive the real pipeline (param mapping, the + handler's extra_body pop, transform_request) and land in completion_args, + never at the top level of the Conversations body.""" + 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={}, + seed=7, + ) + + import json + + sent = json.loads(conversations_route.calls[0].request.content) + assert sent["completion_args"]["random_seed"] == 7 + assert "random_seed" not in sent + + @pytest.mark.asyncio async def test_completion_without_web_search_uses_chat_completions(respx_mock): litellm.disable_aiohttp_transport = True diff --git a/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py b/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py index bfdcb84090f..9bbba525e8d 100644 --- a/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py +++ b/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py @@ -413,6 +413,63 @@ class TestMistralNameHandling: assert result["content"] == "Hello" +class TestMistralSeedParam: + def test_completion_seed_reaches_chat_body(self, monkeypatch, respx_mock): + """seed must survive the full pipeline (supported-params gate, param + mapping, request transform) and reach the chat body as top-level + random_seed; unit tests on map_openai_params alone cannot catch a + regression in the supported-params gate.""" + import litellm + + monkeypatch.setenv("MISTRAL_API_KEY", "fake-mistral-api-key-12345") + litellm.disable_aiohttp_transport = True + + chat_route = respx_mock.post("https://api.mistral.ai/v1/chat/completions").respond( + json={ + "id": "x", + "object": "chat.completion", + "choices": [{"index": 0, "message": {"role": "assistant", "content": "hi"}, "finish_reason": "stop"}], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + } + ) + + litellm.completion( + model="mistral/mistral-large-latest", + messages=[{"role": "user", "content": "hi"}], + seed=7, + ) + + import json + + sent = json.loads(chat_route.calls[0].request.content) + assert sent["random_seed"] == 7 + + def test_map_openai_params_maps_seed_to_random_seed(self): + """seed must map straight into optional_params so it reaches the chat body + top-level and stays visible to the Conversations transform (extra_body is + popped by the HTTP handler before transform_request runs).""" + result = MistralConfig().map_openai_params( + non_default_params={"seed": 7}, + optional_params={}, + model="mistral/mistral-large-latest", + drop_params=False, + ) + + assert result.get("random_seed") == 7 + assert "extra_body" not in result + + def test_transform_request_keeps_random_seed_top_level(self): + body = MistralConfig().transform_request( + model="mistral/mistral-large-latest", + messages=[{"role": "user", "content": "hi"}], + optional_params={"random_seed": 7}, + litellm_params={}, + headers={}, + ) + + assert body["random_seed"] == 7 + + class TestMistralParallelToolCalls: """Test suite for Mistral parallel tool calls functionality."""