fix(responses): omit empty tool config in completion fallback for ollama

This commit is contained in:
sdharani91 2026-03-19 16:45:54 -07:00
parent 2d3cff99fe
commit 2fb9e3b9d4
3 changed files with 59 additions and 0 deletions

0
dhar_repro/repro.ipynb vendored Normal file
View file

View file

@ -233,6 +233,13 @@ class LiteLLMCompletionResponsesConfig:
litellm_completion_request = {
k: v for k, v in litellm_completion_request.items() if v is not None
}
# Do not forward empty tool configuration to chat-completion providers.
# Providers like Ollama change behavior when they see `tools=[]`, even
# though the user did not actually request tool use.
if not tools:
litellm_completion_request.pop("tools", None)
litellm_completion_request.pop("tool_choice", None)
return litellm_completion_request
@staticmethod

View file

@ -26,6 +26,58 @@ from litellm.types.utils import (
class TestLiteLLMCompletionResponsesConfig:
def test_transform_responses_request_omits_empty_tools(self):
result = (
LiteLLMCompletionResponsesConfig.transform_responses_api_request_to_chat_completion_request(
model="ollama/llama3.2:latest",
input="Reply with exactly: ok",
responses_api_request={
"temperature": 0,
"tools": [],
"tool_choice": "auto",
},
custom_llm_provider="ollama",
stream=False,
)
)
assert "tools" not in result
assert "tool_choice" not in result
assert result["messages"] == [{"role": "user", "content": "Reply with exactly: ok"}]
assert result["model"] == "ollama/llama3.2:latest"
def test_transform_responses_request_preserves_non_empty_tools(self):
result = (
LiteLLMCompletionResponsesConfig.transform_responses_api_request_to_chat_completion_request(
model="ollama/llama3.2:latest",
input="Reply with exactly: ok",
responses_api_request={
"temperature": 0,
"tools": [
{
"type": "function",
"name": "echo",
"description": "Echo text",
"parameters": {
"type": "object",
"properties": {"text": {"type": "string"}},
"required": ["text"],
},
}
],
"tool_choice": "auto",
},
custom_llm_provider="ollama",
stream=False,
)
)
assert "tools" in result
assert len(result["tools"]) == 1
assert result["tools"][0]["type"] == "function"
assert result["tools"][0]["function"]["name"] == "echo"
assert result["tool_choice"] == "auto"
def test_transform_input_file_item_to_file_item_with_file_id(self):
"""Test transformation of input_file item with file_id to Chat Completion file format"""
# Setup