From ea340d6d2f7dae2edcfd4506bc22bc40a3528b78 Mon Sep 17 00:00:00 2001 From: ElDjeee Date: Mon, 6 Jul 2026 14:00:06 +0200 Subject: [PATCH] fix(mistral): map tool-call history into Conversations inputs The Conversations transform built inputs from only role and content, so an assistant message's tool_calls and a tool-role result were silently dropped when a web-search turn landed mid function-calling conversation; the model then lost the prior call context with no error. Each assistant tool_call now becomes a function.call input entry (tool_call_id/name/arguments) and each tool message a function.result entry (tool_call_id/result), matching Mistral's Conversations inputs schema, the way other provider transforms preserve tool history instead of flattening it --- .../mistral/conversations/transformation.py | 50 ++++++++++++++++--- tests/llm_translation/test_mistral_api.py | 36 +++++++++++-- ...st_mistral_conversations_transformation.py | 32 ++++++++++++ 3 files changed, 109 insertions(+), 9 deletions(-) diff --git a/litellm/llms/mistral/conversations/transformation.py b/litellm/llms/mistral/conversations/transformation.py index fb7dd362d2a..675c3f20240 100644 --- a/litellm/llms/mistral/conversations/transformation.py +++ b/litellm/llms/mistral/conversations/transformation.py @@ -21,7 +21,6 @@ from litellm.llms.mistral.chat.transformation import MistralConfig from litellm.llms.mistral.common_utils import OBJ_LIST, STR_OBJ_DICT, WEB_SEARCH_TOOL_TYPES from litellm.types.llms.mistral import ( MistralConversationContentChunk, - MistralConversationInputMessage, MistralConversationOutput, MistralConversationsResponse, MistralConversationUsage, @@ -66,6 +65,16 @@ def _content_to_str(content: object) -> str: return "" if content is None else str(content) +def _function_call_entry(call: dict[str, object]) -> dict[str, object]: + fn = STR_OBJ_DICT.validate_python(call["function"]) if isinstance(call.get("function"), dict) else {} + return { + "type": "function.call", + "tool_call_id": str(call.get("id") or ""), + "name": str(fn.get("name") or ""), + "arguments": str(fn.get("arguments") or ""), + } + + def _to_annotation(chunk: MistralConversationContentChunk) -> ChatCompletionAnnotation: url_citation: ChatCompletionAnnotationURLCitation = ( {"url": chunk.url or "", "title": chunk.title} if chunk.title else {"url": chunk.url or ""} @@ -131,6 +140,37 @@ class MistralConversationsConfig(MistralConfig): ) return {**base, **({"random_seed": random_seed} if random_seed is not None else {})} + @staticmethod + def _input_entries_for_message(message: AllMessageValues) -> tuple[dict[str, object], ...]: + """Map one OpenAI message to its Conversations ``inputs`` entries. + + A ``tool`` message becomes a ``function.result`` entry; an assistant + message's ``tool_calls`` each become a ``function.call`` entry (preserving + the id/name/arguments binding); text content becomes a plain message + entry. Preserving this history keeps a web-search turn mid-conversation + from silently dropping prior tool calls the way a role+content flatten would. + """ + typed = STR_OBJ_DICT.validate_python(message) + role = str(typed.get("role") or "") + if role == "tool": + return ( + { + "type": "function.result", + "tool_call_id": str(typed.get("tool_call_id") or ""), + "result": _content_to_str(typed.get("content")), + }, + ) + raw_calls = typed.get("tool_calls") if role == "assistant" else None + calls = OBJ_LIST.validate_python(raw_calls) if isinstance(raw_calls, list) else [] + content = _content_to_str(typed.get("content")) + message_entry: tuple[dict[str, object], ...] = ( + ({"role": role, "content": content},) if content or not calls else () + ) + call_entries = tuple( + _function_call_entry(STR_OBJ_DICT.validate_python(call)) for call in calls if isinstance(call, dict) + ) + return message_entry + call_entries + def transform_request( self, model: str, @@ -146,12 +186,10 @@ class MistralConversationsConfig(MistralConfig): if message.get("role") == "system" and _content_to_str(message.get("content")) ) inputs = [ - MistralConversationInputMessage( - role=str(message.get("role")), - content=_content_to_str(message.get("content")), - ) + entry for message in messages - if message.get("role") != "system" + if str(message.get("role")) != "system" + for entry in self._input_entries_for_message(message) ] completion_args = self._build_completion_args(params) return { diff --git a/tests/llm_translation/test_mistral_api.py b/tests/llm_translation/test_mistral_api.py index 0866cf18ccd..888d5c9b1ff 100644 --- a/tests/llm_translation/test_mistral_api.py +++ b/tests/llm_translation/test_mistral_api.py @@ -51,11 +51,41 @@ class TestMistralCompletion(BaseLLMChatTest): response = litellm.completion( model=model, - messages=[ - {"role": "user", "content": "What's the weather like in Boston today?"} - ], + messages=[{"role": "user", "content": "What's the weather like in Boston today?"}], web_search_options={}, max_tokens=100, ) assert response is not None + + def test_web_search_with_tool_call_history(self): + """The Conversations API accepts prior function-call history mapped to + function.call / function.result input entries alongside a web search + request (a 400 here means the mapped wire schema is wrong).""" + if not os.getenv("MISTRAL_API_KEY"): + pytest.skip("MISTRAL_API_KEY not set") + + response = litellm.completion( + model="mistral/mistral-medium-latest", + messages=[ + {"role": "user", "content": "What's the weather in Paris?"}, + { + "role": "assistant", + "content": "", + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "get_weather", "arguments": '{"city": "Paris"}'}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "18C and sunny"}, + {"role": "user", "content": "Now search the web for who won Euro 2024 and cite sources."}, + ], + web_search_options={}, + max_tokens=200, + ) + + assert response is not None + assert response.choices[0].message.content 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 508666d10d9..4bdb9cd3f9a 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 @@ -113,6 +113,38 @@ def test_transform_request_premium_and_function_passthrough(): ] +def test_transform_request_preserves_tool_call_history(): + cfg = MistralConversationsConfig() + body = cfg.transform_request( + model="mistral-medium-latest", + messages=[ + {"role": "user", "content": "weather in Paris?"}, + { + "role": "assistant", + "content": "", + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "get_weather", "arguments": '{"city":"Paris"}'}, + } + ], + }, + {"role": "tool", "tool_call_id": "call_1", "content": "sunny"}, + {"role": "user", "content": "now cite sources"}, + ], + optional_params={"web_search_options": {}}, + litellm_params={}, + headers={}, + ) + assert body["inputs"] == [ + {"role": "user", "content": "weather in Paris?"}, + {"type": "function.call", "tool_call_id": "call_1", "name": "get_weather", "arguments": '{"city":"Paris"}'}, + {"type": "function.result", "tool_call_id": "call_1", "result": "sunny"}, + {"role": "user", "content": "now cite sources"}, + ] + + def test_transform_request_drops_other_builtin_connectors(): cfg = MistralConversationsConfig() body = cfg.transform_request(