Skip to content

fix(ollama): Ollama Cloud tool calls fail after first turn - #96473

Closed
harjothkhara wants to merge 2 commits into
openclaw:mainfrom
harjothkhara:codex/96441-ollama-compat-tool-args
Closed

harjothkhara wants to merge 2 commits into
openclaw:mainfrom
harjothkhara:codex/96441-ollama-compat-tool-args

Conversation

@harjothkhara

Copy link
Copy Markdown
Contributor

Closes #96441

What Problem This Solves

Fixes an issue where users running Ollama Cloud models with tools would complete the first tool call but fail on the next turn when OpenClaw replayed the prior assistant tool call back through the OpenAI-compatible Chat Completions path.

The failure showed up as Ollama rejecting the request with 400 json: cannot unmarshal object into Go struct field .messages.tool_calls.function.arguments of type string.

Why This Change Was Made

OpenAI-compatible Chat Completions payloads require assistant tool-call and legacy function-call arguments to remain JSON strings. The Ollama OpenAI-compatible num_ctx wrapper now only injects options.num_ctx and no longer applies the native Ollama /api/chat object-argument normalization to replayed Chat Completions messages.

Native Ollama response/request argument parsing remains separate and still handles object arguments for the native API path.

User Impact

Ollama Cloud users can continue multi-turn tool-calling conversations after the first tool call instead of hitting a provider schema error on replay. Native Ollama /api/chat behavior is unchanged.

Evidence

Duplicate/competing PR check:

Real behavior proof:

  • Behavior addressed: Ollama OpenAI-compatible second-turn tool-call replay payload shape.
  • Real environment tested: local OpenClaw provider payload wrapper path in the current source checkout; the regression test captures the outgoing payload after createConfiguredOllamaCompatStreamWrapper applies its patches.
  • Exact steps: added a regression that sends assistant tool_calls[*].function.arguments and legacy function_call.arguments as JSON strings through the Ollama OpenAI-compatible wrapper.
  • Evidence after fix: the captured patched payload keeps both argument fields as JSON strings while still injecting options.num_ctx.
  • Observed result: focused Ollama tests pass and cover the replay payload contract plus adjacent native/Ollama stream behavior.
  • What was not tested: no live Ollama Cloud credentialed request was run from this environment.

Validation commands:

  • node scripts/run-vitest.mjs extensions/ollama/src/stream-runtime.test.ts — 1 file, 101 tests passed.
  • node scripts/run-vitest.mjs extensions/ollama/src/stream.test.ts extensions/ollama/src/stream-runtime.test.ts — 2 files, 116 tests passed.
  • node scripts/run-vitest.mjs extensions/ollama/index.test.ts extensions/ollama/src/stream.test.ts extensions/ollama/src/stream-runtime.test.ts — 3 files, 173 tests passed.
  • Branch autoreview with parallel focused tests: .agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main --parallel-tests "node scripts/run-vitest.mjs extensions/ollama/index.test.ts extensions/ollama/src/stream.test.ts extensions/ollama/src/stream-runtime.test.ts" — clean, no accepted/actionable findings; tests exited 0.

AI assistance disclosure:

  • This PR was implemented with AI assistance in a local Codex session. I reviewed the issue, duplicate PR state, current source behavior, dependency contract (openai Chat Completions types), tests, and autoreview output before opening.

@clawsweeper

clawsweeper Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

This branch is a correct-looking duplicate candidate for the Ollama Cloud second-turn tool-call regression, but #96474 now owns the same production fix with positive captured HTTP-path proof and a clean live mergeability check.

Root-cause cluster
Relationship: superseded
Canonical: #96474
Summary: This PR and #96474 propose the same production deletion for the canonical Ollama Cloud second-turn tool-call regression, but the other PR is open, cleanly mergeable, and has positive captured HTTP-path proof.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Land #96474 as the proof-positive canonical fix, optionally consolidating any desired tests first, then close duplicate candidate paths for the same Ollama Cloud regression.

So I’m closing this here and keeping the remaining discussion on #96474.

Review details

Best possible solution:

Land #96474 as the proof-positive canonical fix, optionally consolidating any desired tests first, then close duplicate candidate paths for the same Ollama Cloud regression.

Do we have a high-confidence way to reproduce the issue?

Yes. The linked issue provides a captured failing Ollama Cloud payload and current source shows the OpenAI-compatible wrapper converting Chat Completions string arguments back into objects before send.

Is this the best way to solve the issue?

No as a landing branch. The code shape is the narrow fix, but #96474 is the better canonical path because it carries the same production deletion with positive captured HTTP-path proof.

Security review:

Security review cleared: The diff only removes Ollama TypeScript runtime payload mutation and adds tests; it does not touch dependencies, workflows, secrets, permissions, packaging, or code-execution infrastructure.

AGENTS.md: found and applied where relevant.

What I checked:

  • Current main still has the root payload mutation: On current main, wrapOllamaCompatNumCtx injects options.num_ctx and then calls normalizeOllamaCompatMessageToolArgs, so the OpenAI-compatible payload is mutated after the base Chat Completions converter builds it. (extensions/ollama/src/stream.ts:242, f29dbd3ebd1c)
  • Current main converts string tool arguments to objects: normalizeOllamaCompatMessageToolArgs walks legacy function_call.arguments and tool_calls[*].function.arguments and replaces each present value with ensureArgsObject(...), matching the reporter's object-on-wire failure mode. (extensions/ollama/src/stream.ts:739, f29dbd3ebd1c)
  • Base OpenAI-compatible converter emits strings: The shared Chat Completions converter serializes assistant tool-call arguments with JSON.stringify(tc.arguments) before Ollama provider wrappers run. (src/llm/providers/openai-completions.ts:1079, f29dbd3ebd1c)
  • Dependency contract supports string arguments: OpenAI Node v6.39.1 source declares both legacy function_call.arguments and ChatCompletionMessageFunctionToolCall.Function.arguments as JSON strings. (openai-node/src/resources/chat/completions/completions.ts:1210)
  • This PR removes the same production normalizer: At this PR head, wrapOllamaCompatNumCtx still injects num_ctx but no longer calls normalizeOllamaCompatMessageToolArgs; the deleted helper is the same production change proposed by the canonical candidate. (extensions/ollama/src/stream.ts:242, c5bf8e98a513)
  • This PR proof remains mock/source-level: The added tests capture the wrapper's mocked onPayload output and assert string preservation, but the PR body says no live Ollama Cloud credentialed request was run and does not include a real SDK/wire payload capture. (extensions/ollama/src/stream-runtime.test.ts:194, c5bf8e98a513)

Likely related people:

  • steipete: The behavior-specific normalizer was added in the merged replay-hardening commit for fix: deserialize string tool-call arguments before sending to Ollama #52253, and that landing comment credits the source PR while recording the merge commit. (role: introduced behavior and area contributor; confidence: high; commits: 87abcfd6a6b8; files: extensions/ollama/src/stream.ts)
  • Adam-Researchh: Opened the prior native Ollama replay PR whose discussion added cloud/OpenAI-compatible argument normalization, making them relevant context for the current inverse regression. (role: original fix proposer; confidence: medium; commits: 87abcfd6a6b8; files: extensions/ollama/src/stream.ts, src/agents/ollama-stream.ts)
  • vincentkoc: Current shallow blame points the affected wrapper lines at a later provider-parity commit that carried the existing behavior into the current file layout. (role: recent current-main carrier; confidence: medium; commits: bfffc77bfc86; files: extensions/ollama/src/stream.ts)

Codex review notes: model internal, reasoning high; reviewed against f29dbd3ebd1c.

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. labels Jun 24, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jun 24, 2026
@clawsweeper

clawsweeper Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: ollama merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Ollama Cloud tool_calls fail on 2nd turn — arguments sent as JSON object instead of string (400 from Go server) — regression in 2026.6.9

1 participant