Skip to content

fix(ollama): keep OpenAI-compatible tool_call arguments as strings - #97355

Closed
moguangyu5-design wants to merge 0 commit into
openclaw:mainfrom
moguangyu5-design:fix/96441-ollama-cloud-tool-args-string
Closed

moguangyu5-design wants to merge 0 commit into
openclaw:mainfrom
moguangyu5-design:fix/96441-ollama-cloud-tool-args-string

Conversation

@moguangyu5-design

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #96441. When using Ollama Cloud models via the OpenAI-compatible endpoint (e.g. ollama/glm-5.2:cloud), the second agent turn fails with:

400 json: cannot unmarshal object into Go struct field .messages.tool_calls.function.arguments of type string

OpenClaw converts assistant tool_calls.function.arguments back from a JSON string into an object when injecting the num_ctx option. The OpenAI API spec requires arguments to be a string, and Ollama Cloud's Go server enforces that strictly.

Why This Change Was Made

The generic OpenAI-completions formatter already stringifies tool_calls.function.arguments. The Ollama-compat num_ctx wrapper was the only place that re-parsed those strings into objects, which regressed in 2026.6.9. Removing that normalization restores spec-compliant behavior for OpenAI-compatible Ollama endpoints while leaving the native /api/chat path untouched (it still objectifies arguments as required by Ollama's native format).

User Impact

Users of Ollama Cloud models can now have multi-turn conversations that include tool calls without hitting a 400 error on the second turn.

Evidence

  • Added a regression test in extensions/ollama/src/stream-runtime.test.ts that passes an assistant message with stringified tool_calls.function.arguments through createConfiguredOllamaCompatStreamWrapper and asserts the arguments stay a string.
  • Ran the targeted test before the fix: it failed because arguments were parsed to an object.
  • Ran the targeted test after the fix: it passes.
  • Ran the full Ollama extension test suite (extensions/ollama/src/stream-runtime.test.ts and extensions/ollama/src/stream.test.ts): 117 tests pass.
  • Ran oxlint on the changed files: 0 warnings, 0 errors.
  • Ran oxfmt on the changed files.

@clawsweeper

clawsweeper Bot commented Jun 28, 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: this PR is a later duplicate of the same Ollama Cloud/OpenAI-compatible tool-call argument regression already owned by the older, open, cleanly mergeable, proof-positive canonical PR at #96474.

Root-cause cluster
Relationship: superseded
Canonical: #96474
Summary: This PR is a later duplicate candidate for the same Ollama Cloud/OpenAI-compatible second-turn tool-call argument regression; the canonical PR is open, cleanly mergeable, and proof-positive.

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 port any desired assertion from this PR there, then close duplicate candidate branches.

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 port any desired assertion from this PR there, then close duplicate candidate branches.

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

Yes. Source inspection shows current main stringifies Chat Completions tool-call arguments and then the Ollama compat wrapper parses them back into objects, matching the linked issue's captured second-turn 400 payload shape.

Is this the best way to solve the issue?

No as a landing branch. The code direction is right, but #96474 is the better solution path because it has the same production deletion plus captured SDK HTTP-path proof and native sibling coverage.

Security review:

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

AGENTS.md: found and applied where relevant.

What I checked:

  • Repository policy read: Root AGENTS.md and the scoped extensions policy were read; the review applied the provider-plugin boundary, dependency-contract, compatibility-risk, and duplicate-PR guidance. (AGENTS.md:1, 08d15ec32db3)
  • Current main still has the failing compat mutation: Current main still injects options.num_ctx and then calls normalizeOllamaCompatMessageToolArgs from wrapOllamaCompatNumCtx, so this is not already implemented on main. (extensions/ollama/src/stream.ts:242, 08d15ec32db3)
  • Current main coerces OpenAI-compatible arguments to objects: normalizeOllamaCompatMessageToolArgs walks function_call.arguments and tool_calls[*].function.arguments and assigns ensureArgsObject(...), matching the reported object-on-wire failure mode. (extensions/ollama/src/stream.ts:744, 08d15ec32db3)
  • Base OpenAI-compatible converter emits strings: The shared Chat Completions formatter maps internal tool calls to function.arguments: JSON.stringify(tc.arguments) before the Ollama wrapper mutates the payload. (src/llm/providers/openai-completions.ts:1079, 08d15ec32db3)
  • Upstream dependency contract checked: openai-node v6.39.1 types define both legacy function_call arguments and tool-call function arguments as strings, supporting the OpenAI-compatible string contract. (openai-node/src/resources/chat/completions/completions.ts:456, 6c11a7450314)
  • This PR removes the same production call: At this PR head, wrapOllamaCompatNumCtx still injects num_ctx but no longer calls the compat message argument normalizer. (extensions/ollama/src/stream.ts:242, 1900405e339d)

Likely related people:

  • steipete: The over-broad compat normalizer appears in the merged Ollama replay-hardening work for fix: deserialize string tool-call arguments before sending to Ollama #52253, which was merged by steipete and committed as 87abcfd. (role: introduced behavior and merger; confidence: high; commits: 87abcfd6a6b8, 53f5103723ee; files: extensions/ollama/src/stream.ts)
  • Adam-Researchh: The prior native/cloud Ollama replay PR was authored by Adam-Researchh, and its discussion scoped the local/native and cloud/OpenAI-compatible argument-normalization behavior. (role: original fix proposer; confidence: medium; commits: 87abcfd6a6b8; files: extensions/ollama/src/stream.ts)
  • Gio Della-Libera: Current blame on the affected lines points to cb4244f, which carried the Ollama stream module through later repository history after the original replay-hardening work. (role: recent area carrier; confidence: medium; commits: cb4244fe15e3; files: extensions/ollama/src/stream.ts)
  • Vincent Koc: The latest release tag that still contains the affected wrapper behavior is v2026.6.10, whose tag commit is aa69b12. (role: release-history carrier; confidence: medium; commits: aa69b12d0086; files: extensions/ollama/src/stream.ts, CHANGELOG.md)

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

@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 28, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 28, 2026
@moguangyu5-design

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@moguangyu5-design
moguangyu5-design force-pushed the fix/96441-ollama-cloud-tool-args-string branch from 1900405 to 2c7e989 Compare July 1, 2026 05:46
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: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS 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