fix(ollama): properly parse tool_calls with dict arguments

Fixes: https://github.com/BerriAI/litellm/issues/24091
This commit is contained in:
Jah-yee 2026-05-28 02:02:23 +08:00
parent 06f6cfc5ae
commit 058f264a16

View file

@ -377,6 +377,20 @@ class OllamaChatConfig(BaseConfig):
response_json_message["reasoning_content"] = reasoning_content
response_json_message["content"] = content
# Handle tool_calls explicitly - ensure they're properly converted
# Fixes: https://github.com/BerriAI/litellm/issues/24091
tool_calls = response_json_message.get("tool_calls") if response_json_message else None
if tool_calls and isinstance(tool_calls, list):
converted_tool_calls = []
for tc in tool_calls:
# Ensure arguments is a string (not a dict) for ChatCompletionMessageToolCall
if "function" in tc:
func = tc["function"]
if "arguments" in func and isinstance(func["arguments"], dict):
tc["function"]["arguments"] = json.dumps(func["arguments"])
converted_tool_calls.append(tc)
response_json_message["tool_calls"] = converted_tool_calls
if (
request_data.get("format", "") == "json"
and litellm_params.get("function_name") is not None