litellm/tests/unit/test_system_message_format_bug.py
mgeorgaklis b3c9d4c1f7
fix(prompt_templates): do not mutate messages in-place in function_call_prompt (#41676)
* fix(prompt_templates): do not mutate messages in-place in function_call_prompt

* refactor(test): avoid local list mutation and satisfy type discipline gate

* style(prompt_templates): keep line length under 120 chars for ruff format

* style(prompt_templates): type _with_function_prompt with AllMessageValues for ANN401

* style(prompt_templates): eliminate basedpyright errors in function_call_prompt

* fix(prompt_templates): keep tuple and None system content when adding the function prompt

Missing system content becomes the function prompt alone, a tuple keeps its parts, and the old test that pinned the caller-side mutation now asserts the caller's messages stay unchanged

* fix(prompt_templates): reject unreadable system content without echoing it

Only list and tuple content is read as parts. Any other non-string content raises a TypeError that names the type, so error logs never carry the system prompt

* fix(prompt_templates): leave non-list system content failing as before

None and tuple content fail the same way they did before this change, so the only behavior change is that the caller's messages are no longer mutated

* fix(prompt_templates): answer 400 for unreadable system content, send the function prompt alone under drop_params

* test(prompt_templates): pin the function prompt on every system message

* test(integration): cover function prompt fallbacks, retries, cache and drop_params on ollama

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-15 14:37:11 -07:00

69 lines
1.9 KiB
Python

"""
Test for GitHub issue #11267 - System message format issue with Ollama + tools
"""
from copy import deepcopy
from typing import Final
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"},
}
],
},
]
messages_before_call: Final = deepcopy(messages)
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 messages == messages_before_call
if __name__ == "__main__":
print("Testing system message format issue...")
test_system_message_format_issue_reproduction()
print("Tests completed!")