diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index b4ace1545d..2f3ffb3b89 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -3711,7 +3711,14 @@ def function_call_prompt(messages: list, functions: list): function_added_to_prompt = False for message in messages: if "system" in message["role"]: - message["content"] += f""" {function_prompt}""" + if isinstance(message["content"], str): + message["content"] += f""" {function_prompt}""" + else: + message["content"].append({ + "type": "text", + "text": f""" {function_prompt}""", + "cache_control": {"type": "ephemeral"} + }) function_added_to_prompt = True if function_added_to_prompt is False: diff --git a/tests/test_litellm/test_system_message_format_bug.py b/tests/test_litellm/test_system_message_format_bug.py new file mode 100644 index 0000000000..a733b1be99 --- /dev/null +++ b/tests/test_litellm/test_system_message_format_bug.py @@ -0,0 +1,72 @@ +""" +Test for GitHub issue #11267 - System message format issue with Ollama + tools +""" + +from unittest.mock import patch + +@patch("litellm.add_function_to_prompt", True) +def test_system_message_format_issue_reproduction(): + """ + Reproduces the system message format bug from GitHub issue #11267. + """ + from litellm import completion + + # Define test data directly from data.jsonl content + model = "ollama/custom_model_name" # Use explicit Ollama model + messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What is the capital of France?" + } + ] + }, + { + "role": "system", + "content": [ + { + "type": "text", + "text": "You are Claude Code, Anthropic's official CLI for Claude.", + "cache_control": {"type": "ephemeral"} + } + ] + } + ] + + temperature = 1 + + # Add tools to trigger the bug - this is what causes the issue + tools = [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get weather for a location", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string"} + }, + "required": ["location"] + } + } + } + ] + + response = completion( + model=model, + messages=messages, + tools=tools, + temperature=temperature, + mock_response=True + ) + + assert len(messages[1]["content"]) == 2 + + +if __name__ == "__main__": + print("Testing system message format issue...") + test_system_message_format_issue_reproduction() + print("Tests completed!") \ No newline at end of file