Skip to content

fix(ollama): stop forcing tool_calls.arguments back to an object on the openai-completions path - #96498

Closed
googlerest wants to merge 3 commits into
openclaw:mainfrom
googlerest:fix/ollama-cloud-tool-call-arguments-string
Closed

googlerest wants to merge 3 commits into
openclaw:mainfrom
googlerest:fix/ollama-cloud-tool-call-arguments-string

Conversation

@googlerest

@googlerest googlerest commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #96441

What Problem This Solves

Ollama Cloud models (e.g. ollama/glm-5.2:cloud) fail every second agent turn after a tool call: OpenClaw sends tool_calls.function.arguments as a JSON object instead of a JSON string when replaying conversation history through the OpenAI-compatible endpoint, and Ollama's Go server rejects it with 400 json: cannot unmarshal object into Go struct field .messages.tool_calls.function.arguments of type string. This is a regression — the first turn works, the tool executes, then the agent is stuck.

Why This Change Was Made

src/llm/providers/openai-completions.ts already correctly JSON.stringifys tool_calls.function.arguments when building the outgoing request (this is the OpenAI Chat Completions wire format, which Ollama's Go server enforces strictly on :cloud models routed through model.api === "openai-completions").

extensions/ollama/src/stream.ts's wrapOllamaCompatNumCtx ran a payload patch (normalizeOllamaCompatMessageToolArgs) right before send that re-coerced those already-correct JSON strings back into objects, undoing the stringify. Tracing this back to its origin: it was added in the same commit (87abcfd6a6, fixing #52253) that correctly added ensureArgsObject() for the native Ollama /api/chat path (extractToolCalls/convertToOllamaMessages), where Ollama's native API does want an object. The same object-coercion was mistakenly also wired into the unrelated openai-completions payload-patch path, breaking the opposite contract there.

Fix: remove normalizeOllamaCompatMessageToolArgs (and its only call site) from wrapOllamaCompatNumCtx. The native-path fix for #52253 (ensureArgsObject inside extractToolCalls) is untouched.

User Impact

Restores multi-turn tool use for Ollama Cloud (and any other provider routed through model.api === "openai-completions" with shouldInjectOllamaCompatNumCtx enabled). No change for native Ollama (model.api === "ollama") users.

Evidence

Behavior addressed: wrapOllamaCompatNumCtx's payload patch mutated already-stringified tool_calls.function.arguments (and legacy function_call.arguments) back into an object on the openai-completions path, before the request left the process.

Real environment tested: macOS arm64 (M4), Darwin 25.5.0, Node v25.9.0, branch fix/ollama-cloud-tool-call-arguments-string, commit 0564c6d637.

Exact steps or command run after this patch:

node scripts/run-vitest.mjs extensions/ollama/src/stream.test.ts
node scripts/run-oxlint.mjs extensions/ollama/src/stream.ts extensions/ollama/src/stream.test.ts
pnpm tsgo:extensions
pnpm tsgo:extensions:test

Evidence after fix:

stream.test.ts: 17 passed (17)
oxlint: exit 0, no findings
tsgo:extensions: exit 0
tsgo:extensions:test: exit 0

Observed result after fix: added two regression tests for wrapOllamaCompatNumCtx (tool_calls and legacy function_call) asserting arguments stays a JSON string after the patch runs. Confirmed both new tests fail against the pre-fix code ({ query: 'weather' } received instead of '{"query":"weather"}') and pass after the fix, isolating exactly the regression described in #96441.

What was not tested: did not run against a live Ollama Cloud account (no credentials in this environment) — this is a payload-construction unit fix verified by reproducing the exact wire-shape assertion the Go server's 400 implies, not a live round-trip.

@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.

Close as superseded: this PR has the right production shape, but #96474 is the stronger open landing path for the same Ollama Cloud/OpenAI-compatible tool-call argument regression because it carries the same deletion with proof-positive captured HTTP-path evidence and native sibling coverage.

Root-cause cluster
Relationship: superseded
Canonical: #96474
Summary: This PR and the canonical sibling target the same Ollama Cloud OpenAI-compatible tool-call argument regression, but the sibling is the proof-positive viable landing path.

Members:

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

Canonical path: Land #96474 as the canonical fix, optionally fold any unique wrapper-level assertions from this PR, then close the duplicate candidate branches for the same regression.

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

Review details

Best possible solution:

Land #96474 as the canonical fix, optionally fold any unique wrapper-level assertions from this PR, then close the duplicate candidate branches for the same regression.

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

Yes. The linked issue supplies captured failing payload evidence, and current source inspection shows the OpenAI-compatible wrapper converting string arguments back to objects before send.

Is this the best way to solve the issue?

No as the landing branch. The production change is right, but #96474 is the better solution path because it carries the same deletion with stronger captured HTTP-path proof and native sibling coverage.

Security review:

Security review cleared: The diff only changes Ollama TypeScript payload shaping and tests, with no dependency, workflow, secret, permission, package, or supply-chain surface added.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • steipete: Merged fix: deserialize string tool-call arguments before sending to Ollama #52253, whose merge commit added the compat message argument normalizer now being narrowed. (role: introduced behavior and merger; confidence: high; commits: 87abcfd6a6b8; files: extensions/ollama/src/stream.ts)
  • Adam-Researchh: Opened the earlier native Ollama replay PR whose discussion and merged fix introduced the string-to-object replay normalization family. (role: original fix proposer; confidence: medium; commits: 87abcfd6a6b8; files: extensions/ollama/src/stream.ts)
  • kevinslin: Current blame on the affected lines points to the file-carrying commit from feat(codex): add always plugin approval mode #97123. (role: recent area carrier; confidence: medium; commits: c5d34c8376f8; files: extensions/ollama/src/stream.ts)
  • vincentkoc: The latest release train commit still contains the affected wrapper behavior, so release routing may be relevant after the canonical fix lands. (role: release carrier; confidence: medium; commits: aa69b12d0086; files: extensions/ollama/src/stream.ts, CHANGELOG.md)

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

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. 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. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 24, 2026
@googlerest
googlerest marked this pull request as ready for review June 24, 2026 16:50
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jun 24, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Closing as superseded by #96474, now merged on main as 3047e6c43dfe515059ea4e5134f4597083936709.

The landed change removes the same OpenAI-compatible Ollama argument re-coercion while preserving native /api/chat object handling, with the stronger captured request-path and sibling-contract proof. Current main no longer calls normalizeOllamaCompatMessageToolArgs from wrapOllamaCompatNumCtx.

Thanks for independently tracing the regression and adding focused wrapper coverage.

@vincentkoc vincentkoc closed this Jul 7, 2026
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: S 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

2 participants