The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
[Fix] Magistral small system prompt diverges too much from the official recommendation (#12007)
Ishaan Jaff1467a99aab22
diff --git a/litellm/llms/mistral/mistral_chat_transformation.py b/litellm/llms/mistral/mistral_chat_transformation.pyindex 871fd8c7ea1..e281e055537 100644--- a/litellm/llms/mistral/mistral_chat_transformation.py+++ b/litellm/llms/mistral/mistral_chat_transformation.py@@ -6,7 +6,7 @@ Why separate file? Make it easy to see how transformation worksDocs - https://docs.mistral.ai/api/"""-from typing import Any, Coroutine, List, Literal, Optional, Tuple, Union, overload, cast+from typing import Any, Coroutine, List, Literal, Optional, Tuple, Union, cast, overloadfrom litellm.litellm_core_utils.prompt_templates.common_utils import (handle_messages_with_content_list_to_str_conversion,@@ -107,15 +107,28 @@ class MistralConfig(OpenAIGPTConfig):def _get_mistral_reasoning_system_prompt() -> str:"""Returns the system prompt for Mistral reasoning models.- Based on Mistral's documentation: https://docs.mistral.ai/capabilities/reasoning/+ Based on Mistral's documentation: https://huggingface.co/mistralai/Magistral-Small-2506++ Mistral recommends the following system prompt for reasoning:"""- return """When solving problems, think step-by-step in <think> tags before providing your final answer. Use the following format:+ return """+ <s>[SYSTEM_PROMPT]system_prompt+ A user will ask you to solve a task. You should first draft your thinking process (inner monologue) until you have derived the final answer. Afterwards, write a self-contained summary of your thoughts (i.e. your summary should be succinct but contain all the critical steps you needed to reach the conclusion). You should use Markdown to format your response. Write both your thoughts and summary in the same language as the task posed by the user. NEVER use \boxed{} in your response.++ Your thinking process must follow the template below:+ <think>+ Your thoughts or/and draft, like working through an exercise on scratch paper. Be as casual and as long as you want until you are confident to generate a correct answer.+ </think>-<think>-Your step-by-step reasoning process. Be thorough and work through the problem carefully.-</think>+ Here, provide a concise summary that reflects your reasoning and presents a clear final answer to the user. Don't mention that this is a summary.-Then provide a clear, concise answer based on your reasoning."""+ Problem:++ [/SYSTEM_PROMPT][INST]user_message[/INST]<think>+ reasoning_traces+ </think>+ assistant_response</s>[INST]user_message[/INST]+ """def map_openai_params(self,diff --git a/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py b/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.pyindex c46413799fe..be0e73d8506 100644--- a/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py+++ b/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py@@ -95,10 +95,6 @@ class TestMistralReasoningSupport:def test_get_mistral_reasoning_system_prompt(self):"""Test that the reasoning system prompt is properly formatted."""prompt = MistralConfig._get_mistral_reasoning_system_prompt()-- assert "<think>" in prompt- assert "</think>" in prompt- assert "step-by-step" in promptassert isinstance(prompt, str)assert len(prompt) > 50 # Ensure it's not empty