diff --git a/src/agents/ollama-stream.ts b/src/agents/ollama-stream.ts index f332ad1fd83..ccec6ea7d13 100644 --- a/src/agents/ollama-stream.ts +++ b/src/agents/ollama-stream.ts @@ -254,9 +254,21 @@ function extractToolCalls(content: unknown): OllamaToolCall[] { const result: OllamaToolCall[] = []; for (const part of parts) { if (part.type === "toolCall") { - result.push({ function: { name: part.name, arguments: part.arguments } }); + // Ensure arguments are always an object, not a string (OpenAI format passes JSON string) + const args = part.arguments; + let parsedArgs: Record; + if (typeof args === "string") { + try { + parsedArgs = JSON.parse(args); + } catch { + parsedArgs = {}; + } + } else { + parsedArgs = args ?? {}; + } + result.push({ function: { name: part.name, arguments: parsedArgs } }); } else if (part.type === "tool_use") { - result.push({ function: { name: part.name, arguments: part.input } }); + result.push({ function: { name: part.name, arguments: part.input ?? {} } }); } } return result; @@ -351,11 +363,17 @@ export function buildAssistantMessage( const toolCalls = response.message.tool_calls; if (toolCalls && toolCalls.length > 0) { for (const tc of toolCalls) { + // Normalize arguments: ensure it's always an object, not a string + const rawArgs = tc.function.arguments; + const normalizedArgs = + typeof rawArgs === "string" + ? (JSON.parse(rawArgs) as Record) + : (rawArgs ?? {}); content.push({ type: "toolCall", id: `ollama_call_${randomUUID()}`, name: tc.function.name, - arguments: tc.function.arguments, + arguments: normalizedArgs, }); } }