Repository navigation
fix: Ollama Cloud tool calls fail on second turn - #96474
Conversation
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
|
Codex review: needs maintainer review before merge. Reviewed June 24, 2026, 2:50 PM ET / 18:50 UTC. Summary PR surface: Source -41, Tests +81. Total +40 across 2 files. Reproducibility: yes. The current-main source path shows Chat Completions arguments are stringified and then the Ollama compat wrapper converts them back to objects, matching the canonical issue's captured 400 response. Review metrics: 2 noteworthy metrics.
Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Risk before merge
Maintainer options:
Next step before merge
Security Review detailsBest possible solution: Land one narrow candidate that keeps native Ollama object coercion separate from OpenAI-compatible string replay, then close the duplicate PR path. Do we have a high-confidence way to reproduce the issue? Yes. The current-main source path shows Chat Completions arguments are stringified and then the Ollama compat wrapper converts them back to objects, matching the canonical issue's captured 400 response. Is this the best way to solve the issue? Yes for code shape. Removing only the compat wrapper's object coercion is the narrowest maintainable fix because native AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against f2bc159b79c3. Label changesLabel justifications:
Evidence reviewedPR surface: Source -41, Tests +81. Total +40 across 2 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
|
@clawsweeper re-review |
|
ClawSweeper marked this "the canonical fix" and superseded my equivalent #96473: identical production deletion, yours carries the captured HTTP-path proof and clean mergeability. It also suggested "consolidating any desired tests first," so here are the two regression tests from mine that this PR doesn't yet have, in case they're useful to fold into 1. Cross-path asymmetry guard — the same replayed tool call must become object args on the native it("replays the same tool call as object args natively but string args for OpenAI-compat", async () => {
// #96441 invariant: identical replayed tool calls must diverge by API. Native
// /api/chat expects object arguments; OpenAI-compatible Chat Completions requires
// JSON-string arguments. This guards against re-unifying the two normalization paths.
const stringArgs = '{"action":"config.get","path":"gateway.port"}';
const nativeMessages = convertToOllamaMessages([
{
role: "assistant",
content: [{ type: "toolCall", id: "call_gateway", name: "gateway", arguments: stringArgs }],
},
]);
expect(nativeMessages[0].tool_calls).toEqual([
{
id: "call_gateway",
function: { name: "gateway", arguments: { action: "config.get", path: "gateway.port" } },
},
]);
let patchedPayload: Record<string, unknown> | undefined;
const baseStreamFn = vi.fn((_model, _context, options) => {
options?.onPayload?.({
messages: [
{
role: "assistant",
tool_calls: [
{ id: "call_gateway", type: "function", function: { name: "gateway", arguments: stringArgs } },
],
},
],
});
return (async function* () {})();
});
const model = {
api: "openai-completions",
provider: "ollama",
id: "glm-5.2:cloud",
contextWindow: 131072,
params: { num_ctx: 32768 },
};
const wrapped = createConfiguredOllamaCompatStreamWrapper({
provider: "ollama",
modelId: "glm-5.2:cloud",
model,
streamFn: baseStreamFn,
} as never);
await wrapped?.(
model as never,
{ messages: [] } as never,
{
onPayload: (payload: unknown) => {
patchedPayload = payload as Record<string, unknown>;
},
} as never,
);
const payload = requireRecord(patchedPayload, "patched payload");
const compatToolCalls = (payload.messages as Array<Record<string, unknown>>)[0].tool_calls as Array<{
function: { arguments: unknown };
}>;
expect(compatToolCalls[0].function.arguments).toBe(stringArgs);
});2. Options-merge edge — it("injects num_ctx without clobbering existing OpenAI-compat options", async () => {
let patchedPayload: Record<string, unknown> | undefined;
const baseStreamFn = vi.fn((_model, _context, options) => {
options?.onPayload?.({ options: { temperature: 0.5 } });
return (async function* () {})();
});
const model = {
api: "openai-completions",
provider: "ollama",
id: "glm-5.2:cloud",
contextWindow: 131072,
params: { num_ctx: 32768 },
};
const wrapped = createConfiguredOllamaCompatStreamWrapper({
provider: "ollama",
modelId: "glm-5.2:cloud",
model,
streamFn: baseStreamFn,
} as never);
await wrapped?.(
model as never,
{ messages: [] } as never,
{
onPayload: (payload: unknown) => {
patchedPayload = payload as Record<string, unknown>;
},
} as never,
);
const payload = requireRecord(patchedPayload, "patched payload");
const options = requireRecord(payload.options, "patched options");
expect(options.temperature).toBe(0.5);
expect(options.num_ctx).toBe(32768);
});You can use the tests or not. I am closing mine in favor of this one. Nice work on the proof. |
|
Confirmed and tested in the release/2026.6.11 branch with the extensions\ollama\src\stream.ts changes recommended in this PR and this resolved an issue that I had experienced since v2026.6.9 where local ollama model calls would fail after tool calls with "cannot unmarshal object into Go struct field .messages.tool_calls.function.arguments of type string". For me, it was happening on all ollama cloud models (glm-5.2, minimax-m3 and kimi-k2.7-code). All models responded |
Tested and it works.
|
Verified the merge is on main, but the published Evidence (live test against
Source-level confirmation: Branch state:
Suggestion: @aketchel — your confirmation comment says you tested in the |
I applied the PR changes on top of the 2026.6.11 branch and confirmed compatability and issue resolution: the changes were not yet in the branch as the release branch was cut before the changes were validated. The PR has since been merged to main and will be released when the next release branch is created, unless it is cherry picked into 2026.6.11 as suggested. |
|
Thanks for confirming, @aketchel — that matches what I found. Just to close the loop publicly for anyone landing here from the issue:
Thanks @ly-wang19 for the fix and @849261680 for the PR with proof-positive evidence. And @aketchel for the real-world confirmation. |
Tested and it works.
Tested and it works. (cherry picked from commit 3047e6c)
Tested and it works. (cherry picked from commit 3047e6c)
Closes #96441
What Problem This Solves
Fixes an issue where users running Ollama Cloud models through the OpenAI-compatible Chat Completions route would hit a 400 error on the second agent turn after a tool call. The failing replay sent previous
tool_calls.function.argumentsas a JSON object even though the OpenAI-compatible request contract expects JSON strings.Why This Change Was Made
The Ollama native
/api/chatroute still needs object arguments, but the OpenAI-compatible/v1/chat/completionsroute must preserve string arguments already produced by the shared OpenAI Chat Completions converter. This change removes the extra compat payload mutation that was converting replayed function arguments back into objects while keeping native Ollama argument normalization covered.User Impact
Ollama Cloud users can continue multi-turn tool-calling conversations instead of failing immediately when OpenClaw replays the previous assistant tool call. Native Ollama users keep the existing object-argument behavior for
/api/chat.Evidence
Dependency contract checked:
node_modules/openai/resources/chat/completions/completions.d.tsdefines both legacyfunction_call.argumentsandChatCompletionMessageFunctionToolCall.Function.argumentsasstringJSON.Captured after-fix request proof:
streamOpenAICompletionsOpenAI SDK HTTP path, Ollama compat wrapper, local OpenAI-compatible/v1/chat/completionsendpoint capturing the wire request body.toolCall, matching the reporter's failing replay shape./v1/chat/completions.tool_calls[0].function.argumentstype:string.tool_calls[0].function.argumentsvalue:{"action":"config.get","path":"gateway.port"}.options.num_ctx:262144, proving the Ollama compat wrapper still patched the same request.convertToOllamaMessagesstill returns object arguments for native Ollama replay:{ "action": "config.get", "path": "gateway.port" }.Captured proof output:
{ "capturedRequests": 1, "requestUrl": "/v1/chat/completions", "compatToolArgumentsType": "string", "compatToolArgumentsValue": "{\"action\":\"config.get\",\"path\":\"gateway.port\"}", "compatOptionsNumCtx": 262144, "nativeToolArgumentsType": "object", "nativeToolArgumentsValue": { "action": "config.get", "path": "gateway.port" } }Commands run:
pnpm install --frozen-lockfilenode scripts/run-vitest.mjs extensions/ollama/src/stream-runtime.test.tsnode scripts/run-vitest.mjs extensions/ollama/index.test.tsnode scripts/run-vitest.mjs extensions/ollama/src/stream.test.ts extensions/ollama/src/stream-runtime.test.tspnpm exec oxfmt --write --threads=1 extensions/ollama/src/stream.ts extensions/ollama/src/stream-runtime.test.tspnpm exec oxfmt --check --threads=1 extensions/ollama/src/stream.ts extensions/ollama/src/stream-runtime.test.tspnpm exec oxlint --config .oxlintrc.json --quiet extensions/ollama/src/stream.ts extensions/ollama/src/stream-runtime.test.tspnpm exec tsx -with a stdin harness that starts a local OpenAI-compatible endpoint, callsstreamOpenAICompletionsthroughcreateConfiguredOllamaCompatStreamWrapper, captures the HTTP request body, and checks the native conversion sibling..agents/skills/autoreview/scripts/autoreview --mode local --prompt 'Review the narrow fix for issue #96441: Ollama OpenAI-compatible Chat Completions payloads must preserve tool call function.arguments as JSON strings, while native Ollama /api/chat replay keeps object arguments. Focus on changed files only and dependency contract fit.'Autoreview result:
autoreview clean: no accepted/actionable findings reportedNot tested: