Skip to content

fix(ollama): send tool_call arguments as strings on OpenAI-compatible path - #97220

Closed
Sanjays2402 wants to merge 2 commits into
openclaw:mainfrom
Sanjays2402:fix/ollama-cloud-tool-call-args-string
Closed

Sanjays2402 wants to merge 2 commits into
openclaw:mainfrom
Sanjays2402:fix/ollama-cloud-tool-call-args-string

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Closes #96441

What Problem This Solves

Fixes an issue where users on any Ollama Cloud model (*:cloud, e.g. ollama/glm-5.2:cloud, glm-5.1:cloud, MiniMax M3 Cloud) would have multi-turn tool use break after the first tool call. The first turn succeeds, but the second turn — where prior assistant tool-call history is replayed back to the provider — fails with an HTTP 400 from Ollama's cloud-side Go server:

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

This is a regression introduced in 2026.6.9 (2026.6.8 worked). It affects the agent loop for every Ollama Cloud user the moment a conversation contains more than one tool call. Direct (non-Ollama) providers serving the same models — e.g. zhipu-ai/glm-5.2 straight from the Z.ai API — are unaffected, which isolates the fault to OpenClaw's Ollama OpenAI-compatible request shaping.

Why This Change Was Made

Ollama exposes two wire contracts and OpenClaw talks to both:

  • Native /api/chat accepts tool_calls[].function.arguments as an object.
  • OpenAI-compatible /v1/chat/completions (what Ollama Cloud proxies through) requires arguments to be a JSON string, per the OpenAI spec, and its Go server enforces this strictly.

The OpenAI-compatible num_ctx wrapper was reusing the native normalization (normalizeOllamaCompatMessageToolArgs → ensureArgsObject), coercing string arguments back into objects right before send. This change splits the two contracts, exactly as outlined in the maintainer review on #96441:

  • Native /api/chat keeps object coercion (extractToolCalls, unchanged).
  • The OpenAI-compatible payload now serializes arguments to JSON strings via a new ensureArgsString helper, while still injecting num_ctx.

Already-stringified arguments pass through unchanged to avoid double-encoding. Non-tool messages are left untouched. No behavior change for native Ollama.

User Impact

Ollama Cloud users can now run multi-turn conversations with tool calls without the second turn 400-ing. The fix is provider-payload-shaping only; native Ollama (/api/chat) object-form arguments are preserved, so the previously-fixed native replay issues (#46679, #50689) stay fixed.

Evidence

Targeted tests in extensions/ollama/src/stream.test.ts (all 21 pass — 16 existing + 5 new regression):

 ✓  extension-providers  extensions/ollama/src/stream.test.ts (21 tests) 29ms
 Test Files  1 passed (1)
      Tests  21 passed (21)

New regression coverage:

  • object arguments → serialized to a JSON string
  • already-stringified arguments → passed through unchanged (no double-encoding)
  • legacy function_call shape → normalized to a string
  • the exact second-turn replay payload from the bug report
  • no-op when there are no messages / tool calls

Lint + format clean on the changed files:

oxlint:  Found 0 warnings and 0 errors.
oxfmt:   All matched files use the correct format.

Type-check: tsgo -p extensions/ollama/tsconfig.json reports the same 7 pre-existing errors with and without this change (workspace-artifact module-resolution noise in index.ts / embedding-provider.ts and an untouched text_delta event) — zero new type errors introduced by this diff.

Note: I did not have live Ollama Cloud credentials to capture an end-to-end 400→200 run; validation is source-level + unit tests against the captured failing payload shape from the issue, matching the maintainer's source-level reproduction.

… path

Ollama Cloud (*:cloud models) proxies through an OpenAI-compatible Go server
that requires tool_calls[].function.arguments to be a JSON string. The compat
num_ctx wrapper reused the native /api/chat normalization, which coerces
arguments back to objects, so replaying assistant tool-call history on the
second turn failed with:

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

Split the contracts per the maintainer review on openclaw#96441: keep object coercion
for native /api/chat (extractToolCalls), and serialize arguments to JSON
strings for the OpenAI-compatible Chat Completions payload while retaining
num_ctx injection. Already-stringified arguments pass through unchanged to
avoid double-encoding.

Adds regression coverage for object->string, string passthrough, the legacy
function_call shape, the second-turn replay payload, and the no-op cases.

Fixes openclaw#96441

Signed-off-by: Sanjay Santhanam <[email protected]>
Signed-off-by: Sanjays2402 <[email protected]>
@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

ClawSweeper review: did not complete due to Codex infrastructure failure. Reviewed July 2, 2026, 1:43 PM ET / 17:43 UTC.

Summary
Review failed before ClawSweeper could summarize the requested change.

PR surface: Source +31, Tests +118. Total +149 across 2 files.

Reproducibility: unclear. The review failed before ClawSweeper could establish a reproduction path.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: extensions/ollama/src/stream.test.ts, serialized state: extensions/ollama/src/stream.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Not assessed.
Failure reason: timeout.

This is a ClawSweeper/Codex infrastructure failure, not a PR readiness or patch-quality verdict.
Keep any merge decision on the normal maintainer review path until ClawSweeper can complete a fresh review.

Risk before merge

  • [P1] No close action taken because the review did not complete.

Maintainer options:

  1. Decide the mitigation before merge
    Retry the Codex review after fixing the execution failure.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • Review did not complete, so no work-lane recommendation was made.
Review details

Best possible solution:

Retry the Codex review after fixing the execution failure.

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

Unclear. The review failed before ClawSweeper could establish a reproduction path.

Is this the best way to solve the issue?

Unclear. Retry the review first so ClawSweeper can evaluate the actual issue and fix direction.

AGENTS.md: unclear because the file could not be read completely.

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

Label changes

Label changes:

  • remove P1: Current review triage priority is none.
  • remove rating: 🦪 silver shellfish: Current review failed before PR readiness was assessed, so no rating label should remain.
  • remove merge-risk: 🚨 compatibility: Current PR review selected no merge-risk labels.
  • remove status: 📣 needs proof: Current PR status no longer selects a status label.
Evidence reviewed

PR surface:

Source +31, Tests +118. Total +149 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 34 3 +31
Tests 1 119 1 +118
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 153 4 +149

What I checked:

  • failure reason: timeout.
  • codex failure detail: Codex review failed for this PR: Codex process timed out after 1200000ms.
  • codex stderr: No stderr captured.
  • codex stdout: {"type":"thread.started","thread_id":"019f23db-97f5-7ba1-8ab1-383026607d70"}.
  • process error code: ETIMEDOUT.

Likely related people:

  • unknown: Codex failed before it could trace repository history. (role: review did not complete; confidence: low)
How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. 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 27, 2026
The two OpenAI-compatible tool_call assertions cast the fixture arrays to
Array<Record<string, never>>. After sending arguments as JSON strings, the
concrete fixture shape no longer overlaps that empty-object type, so tsgo's
check-test-types lane failed with:

  TS2352 Conversion of type '{ id; type; function: { name; arguments } }[]'
         to type 'Record<string, never>[]' may be a mistake ...
  TS2339 Property 'arguments' does not exist on type 'never'.

Cast through the real tool_call shape ({ function: { arguments: unknown } }),
matching the existing replay-payload assertion in the same block. Assertions
and runtime behavior are unchanged; the redundant `as unknown as string` on
JSON.parse collapses to a single `as string` now that args is `unknown`.

Signed-off-by: Sanjay Santhanam <[email protected]>
@clawsweeper clawsweeper Bot removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jul 2, 2026
@vincentkoc

Copy link
Copy Markdown
Member

This is already fixed on current main by #96474, merged as 3047e6c.

That canonical fix removes the OpenAI-compatible wrapper's object coercion, while preserving native Ollama object arguments. Its focused tests cover both sides of the contract: native replay remains object-shaped and OpenAI-compatible replay remains string-shaped. This PR addresses the same #96441 failure with a second normalization path, so keeping it open would duplicate the landed behavior.

Closing as fixed/superseded. Thanks for the careful repro and tests.

@vincentkoc vincentkoc closed this Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: ollama P1 High-priority user-facing bug, regression, or broken workflow. size: S

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