From 1f81e69ce18a9615b2d46a28c15ae52811821ac8 Mon Sep 17 00:00:00 2001 From: alpha-nerd-nomyo Date: Mon, 9 Feb 2026 11:04:14 +0100 Subject: [PATCH] refactor(router.py): correctly implement OpenAI tool_calls to Ollama format conversion --- router.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/router.py b/router.py index fe1fd36..89f27e8 100644 --- a/router.py +++ b/router.py @@ -1029,13 +1029,32 @@ class rechunk: thinking = (getattr(with_thinking.message, "reasoning_content", None) or getattr(with_thinking.message, "reasoning", None)) if with_thinking else None role = chunk.choices[0].message.role or "assistant" content = chunk.choices[0].message.content or '' + # Convert OpenAI tool_calls to Ollama format + ollama_tool_calls = None + if stream: + raw_tool_calls = getattr(with_thinking.delta, "tool_calls", None) if with_thinking else None + else: + raw_tool_calls = getattr(with_thinking.message, "tool_calls", None) if with_thinking else None + if raw_tool_calls: + ollama_tool_calls = [] + for tc in raw_tool_calls: + try: + args = orjson.loads(tc.function.arguments) if isinstance(tc.function.arguments, str) else (tc.function.arguments or {}) + except (orjson.JSONDecodeError, TypeError): + args = {} + ollama_tool_calls.append({ + "function": { + "name": tc.function.name, + "arguments": args, + } + }) assistant_msg = ollama.Message( role=role, content=content, thinking=thinking, images=None, tool_name=None, - tool_calls=None) + tool_calls=ollama_tool_calls) rechunk = ollama.ChatResponse( model=chunk.model, created_at=iso8601_ns(),