diff --git a/litellm/llms/mistral/mistral_chat_transformation.py b/litellm/llms/mistral/mistral_chat_transformation.py index 9a5a94db00..110b41fee6 100644 --- a/litellm/llms/mistral/mistral_chat_transformation.py +++ b/litellm/llms/mistral/mistral_chat_transformation.py @@ -252,25 +252,18 @@ Then provide a clear, concise answer based on your reasoning.""" existing_content = msg.get("content", "") reasoning_prompt = self._get_mistral_reasoning_system_prompt() - # Handle both string and list content - convert everything to string - # since Mistral API expects string content + # Handle both string and list content, preserving original format if isinstance(existing_content, str): # String content - prepend reasoning prompt - content_str = existing_content + new_content = f"{reasoning_prompt}\n\n{existing_content}" elif isinstance(existing_content, list): - # List content - convert to string first - content_str = "" - for item in existing_content: - if isinstance(item, dict) and item.get("type") == "text": - content_str += item.get("text", "") - else: - content_str += str(item) + # List content - prepend reasoning prompt as text block + new_content = [ + {"type": "text", "text": reasoning_prompt + "\n\n"} + ] + existing_content else: # Fallback for any other type - convert to string - content_str = str(existing_content) - - # Create the final content with reasoning prompt - new_content = f"{reasoning_prompt}\n\n{content_str}" + new_content = f"{reasoning_prompt}\n\n{str(existing_content)}" messages[i] = cast(AllMessageValues, { **msg, 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 bccfd56c4c..af576c3eef 100644 --- a/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py +++ b/tests/test_litellm/llms/mistral/test_mistral_chat_transformation.py @@ -163,23 +163,56 @@ class TestMistralReasoningSupport: result = mistral_config._add_reasoning_system_prompt_if_needed(messages, optional_params) - # Should modify existing system message with list content converted to string + # Should modify existing system message preserving list format assert len(result) == 2 assert result[0]["role"] == "system" - assert isinstance(result[0]["content"], str) + assert isinstance(result[0]["content"], list) - # Should contain the reasoning prompt - assert "" in result[0]["content"] + # First item should be the reasoning prompt + assert result[0]["content"][0]["type"] == "text" + assert "" in result[0]["content"][0]["text"] - # Original content should be preserved (converted from list to string) - assert "You are a helpful assistant." in result[0]["content"] - assert "You always provide detailed explanations." in result[0]["content"] + # Original content should be preserved + assert "You are a helpful assistant." in result[0]["content"][1]["text"] + assert "You always provide detailed explanations." in result[0]["content"][2]["text"] assert result[1]["role"] == "user" # Should remove the internal flag assert "_add_reasoning_prompt" not in optional_params + def test_add_reasoning_system_prompt_preserves_content_types(self): + """Test that reasoning prompt preserves original content types (string vs list).""" + mistral_config = MistralConfig() + + # Test with string content + string_messages = [ + {"role": "system", "content": "You are helpful."}, + {"role": "user", "content": "Hello"} + ] + string_params = {"_add_reasoning_prompt": True} + + string_result = mistral_config._add_reasoning_system_prompt_if_needed(string_messages, string_params) + assert isinstance(string_result[0]["content"], str) + assert "" in string_result[0]["content"] + assert "You are helpful." in string_result[0]["content"] + + # Test with list content + list_messages = [ + { + "role": "system", + "content": [{"type": "text", "text": "You are helpful."}] + }, + {"role": "user", "content": "Hello"} + ] + list_params = {"_add_reasoning_prompt": True} + + list_result = mistral_config._add_reasoning_system_prompt_if_needed(list_messages, list_params) + assert isinstance(list_result[0]["content"], list) + assert list_result[0]["content"][0]["type"] == "text" + assert "" in list_result[0]["content"][0]["text"] + assert "You are helpful." in list_result[0]["content"][1]["text"] + def test_add_reasoning_system_prompt_no_flag(self): """Test that no modification happens when _add_reasoning_prompt flag is not set.""" mistral_config = MistralConfig()