Skip to content

Commit 2bed2d4

Browse files
committed
fix(ollama): preserve tool_calls arguments as JSON strings in OpenAI-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 #96441
1 parent c5f10b5 commit 2bed2d4

2 files changed

Lines changed: 139 additions & 1 deletion

File tree

‎extensions/ollama/src/stream.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ export function wrapOllamaCompatNumCtx(baseFn: StreamFn | undefined, numCtx: num
247247
payloadRecord.options = {};
248248
}
249249
(payloadRecord.options as Record<string, unknown>).num_ctx = numCtx;
250-
normalizeOllamaCompatMessageToolArgs(payloadRecord);
250+
// FIX #96441: Do not normalize tool call arguments here — the upstream
251+
// OpenAI-compatible converter (openai-completions.ts) already serializes
252+
// arguments as JSON strings per the OpenAI API spec. Re-normalizing them
253+
// back to objects breaks Ollama Cloud's Go server which enforces the spec
254+
// strictly. The native Ollama path has its own normalization elsewhere.
251255
});
252256
}
253257

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { createAssistantMessageEventStream } from "openclaw/plugin-sdk/llm";
2+
// Tests for wrapOllamaCompatNumCtx — verifies FIX #96441:
3+
// OpenAI-compatible tool_calls.function.arguments must remain as JSON strings
4+
// (not be normalized back to objects).
5+
import { beforeEach, describe, expect, it, vi } from "vitest";
6+
7+
// Track every payload patcher that wrapOllamaCompatNumCtx registers.
8+
const capturedPatchers: Array<(payload: Record<string, unknown>) => void> = [];
9+
10+
vi.mock("openclaw/plugin-sdk/provider-stream-shared", () => ({
11+
streamWithPayloadPatch: vi.fn(
12+
(
13+
_streamFn: unknown,
14+
_model: unknown,
15+
_context: unknown,
16+
_options: unknown,
17+
patcher: (payload: Record<string, unknown>) => void,
18+
) => {
19+
capturedPatchers.push(patcher);
20+
return createAssistantMessageEventStream();
21+
},
22+
),
23+
}));
24+
25+
import { wrapOllamaCompatNumCtx } from "./stream.js";
26+
27+
describe("wrapOllamaCompatNumCtx — FIX #96441", () => {
28+
beforeEach(() => {
29+
capturedPatchers.length = 0;
30+
});
31+
32+
it("injects num_ctx into payload options", async () => {
33+
const wrapped = wrapOllamaCompatNumCtx(undefined, 4096);
34+
await wrapped({} as any, { messages: [], systemPrompt: "" } as any);
35+
36+
expect(capturedPatchers.length).toBe(1);
37+
const patcher = capturedPatchers[0];
38+
39+
const payload: Record<string, unknown> = { options: {}, messages: [] };
40+
patcher(payload);
41+
42+
expect((payload.options as Record<string, unknown>).num_ctx).toBe(4096);
43+
});
44+
45+
it("preserves tool_calls[*].function.arguments as strings (FIX #96441)", async () => {
46+
const wrapped = wrapOllamaCompatNumCtx(undefined, 4096);
47+
await wrapped({} as any, { messages: [], systemPrompt: "" } as any);
48+
49+
expect(capturedPatchers.length).toBe(1);
50+
const patcher = capturedPatchers[0];
51+
52+
// Simulate the payload that openai-completions.ts produces:
53+
// arguments is a JSON string per the OpenAI API spec.
54+
const payload: Record<string, unknown> = {
55+
options: {},
56+
messages: [
57+
{
58+
role: "assistant",
59+
tool_calls: [
60+
{
61+
id: "call_1",
62+
type: "function",
63+
function: {
64+
name: "config_get",
65+
arguments: '{"action":"config.get","path":"gateway.port"}',
66+
},
67+
},
68+
],
69+
},
70+
],
71+
};
72+
73+
patcher(payload);
74+
75+
// Read back the tool_calls and verify arguments stayed as a string.
76+
const messages = payload.messages as Array<Record<string, unknown>>;
77+
const toolCalls = messages[0].tool_calls as Array<Record<string, unknown>>;
78+
const fnSpec = toolCalls[0].function as Record<string, unknown>;
79+
80+
expect(typeof fnSpec.arguments).toBe("string");
81+
expect(fnSpec.arguments).toBe('{"action":"config.get","path":"gateway.port"}');
82+
});
83+
84+
it("preserves function_call.arguments as strings (legacy format)", async () => {
85+
const wrapped = wrapOllamaCompatNumCtx(undefined, 4096);
86+
await wrapped({} as any, { messages: [], systemPrompt: "" } as any);
87+
88+
expect(capturedPatchers.length).toBe(1);
89+
const patcher = capturedPatchers[0];
90+
91+
// Some older models use the legacy function_call format.
92+
const payload: Record<string, unknown> = {
93+
options: {},
94+
messages: [
95+
{
96+
role: "assistant",
97+
function_call: {
98+
name: "get_weather",
99+
arguments: '{"city":"London"}',
100+
},
101+
},
102+
],
103+
};
104+
105+
patcher(payload);
106+
107+
const messages = payload.messages as Array<Record<string, unknown>>;
108+
const fnCall = messages[0].function_call as Record<string, unknown>;
109+
110+
expect(typeof fnCall.arguments).toBe("string");
111+
expect(fnCall.arguments).toBe('{"city":"London"}');
112+
});
113+
114+
it("handles messages without tool_calls or function_call gracefully", async () => {
115+
const wrapped = wrapOllamaCompatNumCtx(undefined, 4096);
116+
await wrapped({} as any, { messages: [], systemPrompt: "" } as any);
117+
118+
expect(capturedPatchers.length).toBe(1);
119+
const patcher = capturedPatchers[0];
120+
121+
// Payload with a plain user message — no tool-related fields.
122+
const payload: Record<string, unknown> = {
123+
options: {},
124+
messages: [
125+
{ role: "user", content: "Hello" },
126+
{ role: "assistant", content: "Hi there" },
127+
],
128+
};
129+
130+
// Should not throw or modify anything.
131+
expect(() => patcher(payload)).not.toThrow();
132+
expect((payload.messages as Array<Record<string, unknown>>)[1].content).toBe("Hi there");
133+
});
134+
});

0 commit comments

Comments
 (0)