fix: parse tool call arguments as dicts for Ollama API compatibility

Ollama's chat API expects tool_call arguments as JSON objects (dicts),
but messages_to_dicts serializes them as JSON strings. This caused 400
errors on multi-turn tool-calling conversations. The fix parses string
arguments back to dicts before sending to Ollama.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Tarun Suresh
2026-03-08 06:13:55 +00:00
co-authored by Claude Opus 4.6
parent 880e3f67f2
commit 45c0e44143
+12 -1
View File
@@ -41,9 +41,20 @@ class OllamaEngine(InferenceEngine):
max_tokens: int = 1024, max_tokens: int = 1024,
**kwargs: Any, **kwargs: Any,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
msg_dicts = messages_to_dicts(messages)
# Ollama expects tool_call arguments as dicts, not JSON strings
for md in msg_dicts:
for tc in md.get("tool_calls", []):
fn = tc.get("function", {})
args = fn.get("arguments")
if isinstance(args, str):
try:
fn["arguments"] = json.loads(args)
except (json.JSONDecodeError, TypeError):
pass
payload: Dict[str, Any] = { payload: Dict[str, Any] = {
"model": model, "model": model,
"messages": messages_to_dicts(messages), "messages": msg_dicts,
"stream": False, "stream": False,
"options": { "options": {
"temperature": temperature, "temperature": temperature,