From 81f1dcba2d1729bf7d954e43ffee2c7f66be74f1 Mon Sep 17 00:00:00 2001 From: PRABHU KIRAN VANDRANKI <72809214+VANDRANKI@users.noreply.github.com> Date: Thu, 21 May 2026 21:26:22 -0400 Subject: [PATCH] fix(ollama): remap tool role to tool_responses for Gemma models Gemma models served via Ollama expect the role "tool_responses" for tool result messages instead of the OpenAI-standard "tool" role. Without this mapping the model ignores the tool result and enters an infinite tool-calling loop. Fixes #28530 --- litellm/llms/ollama/chat/transformation.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.py index c990cc2e093..e6a44b11771 100644 --- a/litellm/llms/ollama/chat/transformation.py +++ b/litellm/llms/ollama/chat/transformation.py @@ -287,8 +287,14 @@ class OllamaChatConfig(BaseConfig): content_str = convert_content_list_to_str(cast(AllMessageValues, m)) images = extract_images_from_message(cast(AllMessageValues, m)) + role = cast(str, m.get("role")) + # Gemma models (via Ollama) expect "tool_responses" for tool result + # messages instead of the OpenAI-standard "tool" role. Without this, + # the model ignores the result and loops infinitely. + if role == "tool" and "gemma" in model.lower(): + role = "tool_responses" ollama_message = OllamaChatCompletionMessage( - role=cast(str, m.get("role")), + role=role, ) if reasoning_content is not None: ollama_message["thinking"] = reasoning_content