Repository navigation
fix(ollama): send tool_call arguments as strings on OpenAI-compatible path - #97220
Sanjays2402 wants to merge 2 commits into
Conversation
… 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 review: did not complete due to Codex infrastructure failure. Reviewed July 2, 2026, 1:43 PM ET / 17:43 UTC. Summary 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 Merge readiness This is a ClawSweeper/Codex infrastructure failure, not a PR readiness or patch-quality verdict. Risk before merge
Maintainer options:
Next step before merge
Review detailsBest 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 changesLabel changes:
Evidence reviewedPR surface: Source +31, Tests +118. Total +149 across 2 files. View PR surface stats
What I checked:
Likely related people:
How this review workflow works
|
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]>
|
This is already fixed on current 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. |
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: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.2straight 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:
/api/chatacceptstool_calls[].function.argumentsas an object./v1/chat/completions(what Ollama Cloud proxies through) requiresargumentsto be a JSON string, per the OpenAI spec, and its Go server enforces this strictly.The OpenAI-compatible
num_ctxwrapper 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:/api/chatkeeps object coercion (extractToolCalls, unchanged).argumentsto JSON strings via a newensureArgsStringhelper, while still injectingnum_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):New regression coverage:
function_callshape → normalized to a stringLint + format clean on the changed files:
Type-check:
tsgo -p extensions/ollama/tsconfig.jsonreports the same 7 pre-existing errors with and without this change (workspace-artifact module-resolution noise inindex.ts/embedding-provider.tsand an untouchedtext_deltaevent) — 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.