From 1467a99aab22994da726f98e4adf9f07be80089e Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 24 Jun 2025 13:45:58 -0700 Subject: [PATCH] [Fix] Magistral small system prompt diverges too much from the official recommendation (#12007) * fix mistral _get_mistral_reasoning_system_prompt * fix test_get_mistral_reasoning_system_prompt --- .../mistral/mistral_chat_transformation.py | 27 ++++++++++++++----- .../test_mistral_chat_transformation.py | 4 --- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/litellm/llms/mistral/mistral_chat_transformation.py b/litellm/llms/mistral/mistral_chat_transformation.py index 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 works Docs - 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, overload from 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 tags before providing your final answer. Use the following format: + return """ + [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 step-by-step reasoning process. Be thorough and work through the problem carefully. - + Your thinking process must follow the template below: + + 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. + -Then provide a clear, concise answer based on your reasoning.""" + 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. + + Problem: + + [/SYSTEM_PROMPT][INST]user_message[/INST] + reasoning_traces + + assistant_response[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.py index 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 "" in prompt - assert "" in prompt - assert "step-by-step" in prompt assert isinstance(prompt, str) assert len(prompt) > 50 # Ensure it's not empty