Skip to content

fix(ollama): preserve tool_calls arguments as JSON strings in OpenAI-compatible path - #96501

Closed
mmyzwl wants to merge 1 commit into
openclaw:mainfrom
mmyzwl:fix/issue-96441-ollama-cloud-tool-calls-fail-on-2nd-turn-arguments-sent-as-j
Closed

mmyzwl wants to merge 1 commit into
openclaw:mainfrom
mmyzwl:fix/issue-96441-ollama-cloud-tool-calls-fail-on-2nd-turn-arguments-sent-as-j

Conversation

@mmyzwl

@mmyzwl mmyzwl commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Ollama Cloud models fail on the second agent turn when tool calls are involved. OpenClaw sends tool_calls.function.arguments as a JSON object instead of a JSON string, causing Ollama's Go server to reject the request with 400.

Root cause: The wrapOllamaCompatNumCtx function in the Ollama extension (extensions/ollama/src/stream.ts) called normalizeOllamaCompatMessageToolArgs, which converts tool call arguments from JSON strings back to objects. The upstream OpenAI-compatible converter (openai-completions.ts) already correctly serializes arguments as strings per the OpenAI API spec — this normalization was over-applied.

Fix: Removed the over-application of normalizeOllamaCompatMessageToolArgs from the OpenAI-compatible wrapper while keeping the native Ollama /api/chat normalization intact.

Fixes #96441

Real behavior proof (required for external PRs)

Behavior addressed: Ollama Cloud returns 400 cannot unmarshal object into Go struct field .messages.tool_calls.function.arguments of type string on second-turn tool call replay.

Evidence type: terminal (unit tests + code review)

Real setup tested:

  • Runtime: Node v24.13.1, Linux x86_64
  • Test suite: 266 tests pass (11 files), including 4 new regression tests

Exact steps or command run after fix:

pnpm test -- --project extension-providers extensions/ollama/src/

After-fix evidence:

Test Files  11 passed (11)
     Tests  266 passed (266)

Observed result after the fix: All 266 tests pass. New regression tests confirm that tool_calls[*].function.arguments remain as JSON strings through wrapOllamaCompatNumCtx. The native Ollama path (normalizeOllamaToolCallArguments) is unaffected.

What was not tested: End-to-end Ollama Cloud credential test (requires a live Ollama Cloud API key). The existing issue report already provides HTTP interceptor evidence showing the wire-format problem, and the code-level fix is directly verifiable from the payload structure.

Tests and validation

Test Type Result Details
New Regression Tests ✅ 4/4 wrap-ollama-compat.test.ts — verifies arguments stay as strings
Stream Tests ✅ 15/15 Existing stream.test.ts
Stream Runtime Tests ✅ 100/100 Existing stream-runtime.test.ts
All Ollama Tests ✅ 266/266 11 test files, 0 failures

Risk checklist

Did user-visible behavior change? (No)

Did config, environment, or migration behavior change? (No)

Did security, auth, secrets, network, or tool execution behavior change? (No)

What is the highest-risk area?

  • Native Ollama /api/chat path: not affected — it uses normalizeOllamaToolCallArguments in buildOllamaChatRequest, a completely separate code path.
  • Other OpenAI-compatible providers: not affected — they don't use Ollama's normalization function.

How is that risk mitigated?

  • The change is narrow (remove 1 line), fully unit-tested (4 new tests), and the separate native path is explicitly documented.
  • All 266 existing tests pass with 0 regressions.

…compatible path

- Remove normalizeOllamaCompatMessageToolArgs call from
  wrapOllamaCompatNumCtx — the upstream OpenAI-compatible converter
  (openai-completions.ts) already serializes arguments as JSON strings
  per the OpenAI API spec.
- The removed call was re-parsing string arguments back to objects,
  causing Ollama Cloud Go server to reject with 400 on second-turn
  tool calls.
- Native Ollama /api/chat path has its own normalization elsewhere.
- Add wrap-ollama-compat.test.ts with 4 test cases verifying that
  tool_calls[*].function.arguments remain as strings after wrapping.

Fixes openclaw#96441
@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 branch targets the right Ollama Cloud regression, but #96474 is the stronger open canonical path with the same production deletion, positive captured HTTP-path proof, clean mergeability, and no unused-helper validation failure.

Root-cause cluster
Relationship: superseded
Canonical: #96474
Summary: This PR is a duplicate candidate for the canonical Ollama Cloud/OpenAI-compatible second-turn tool-call argument regression, but the open proof-positive PR is the safer 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 proof-positive canonical fix, optionally consolidate any desired tests there, 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 consolidate any desired tests there, then close duplicate candidate paths for the same Ollama Cloud regression.

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

Yes. Source inspection reproduces the payload-shape bug: the OpenAI-compatible converter creates string arguments and current main's Ollama wrapper parses them back to objects before send; the linked issue adds captured provider-error evidence.

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 because it removes the helper entirely and includes positive captured HTTP-path proof.

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:

  • Current main root path: Current main still injects num_ctx and then calls normalizeOllamaCompatMessageToolArgs, so the Ollama OpenAI-compatible wrapper can mutate Chat Completions payloads after conversion. (extensions/ollama/src/stream.ts:242, 4ae0a5d958a0)
  • Current main coercion: The current compat normalizer replaces legacy function_call.arguments and tool_calls[*].function.arguments with ensureArgsObject(...), parsing JSON strings into objects. (extensions/ollama/src/stream.ts:739, 4ae0a5d958a0)
  • OpenAI-compatible base converter: 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:1084, 4ae0a5d958a0)
  • Dependency contract: OpenAI Node v6.39.1 source declares both legacy function_call.arguments and ChatCompletionMessageFunctionToolCall.Function.arguments as strings. (openai-node/src/resources/chat/completions/completions.ts:1210)
  • This branch leaves dead code: At this PR head, wrapOllamaCompatNumCtx no longer calls the normalizer, but normalizeOllamaCompatMessageToolArgs remains declared. (extensions/ollama/src/stream.ts:743, 2bed2d43ee23)
  • Patch-caused validation failure: Live CI reports TS6133 and oxlint no-unused-vars for the leftover normalizeOllamaCompatMessageToolArgs helper. (extensions/ollama/src/stream.ts:743, 2bed2d43ee23)

Likely related people:

  • steipete: The merged Ollama replay-hardening commit added the normalization family and the prior PR landing comment records this user as the merger. (role: introduced behavior and merger; confidence: high; commits: 87abcfd6a6b8, 53f5103723ee; files: extensions/ollama/src/stream.ts)
  • Adam-Researchh: Opened the earlier native Ollama replay PR whose discussion expanded to the cloud/OpenAI-compatible normalization hook now being narrowed. (role: original fix proposer; confidence: medium; commits: 87abcfd6a6b8; files: extensions/ollama/src/stream.ts, src/agents/ollama-stream.ts)
  • miorbnli: Current shallow blame on the affected wrapper and helper points at a later commit that carried this code into the reviewed current-main checkout; deeper feature provenance is better traced to the earlier Ollama replay PR. (role: recent current-main carrier; confidence: low; commits: 380f2749becc; files: extensions/ollama/src/stream.ts)

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

@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
@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: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. 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

1 participant