Skip to content

Commit 1165aeb

Browse files
authored
fix(nanogpt): preserve optional tool parameters (#1590)
1 parent cc9c0af commit 1165aeb

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎src/api/providers/__tests__/nanogpt.spec.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { nanoGptDefaultModelId, providerIdentifiers } from "@roo-code/types"
99

1010
import { buildApiHandler } from "../../index"
1111
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
12+
import { createReadFileTool } from "../../../core/prompts/tools/native-tools/read_file"
1213
import { NanoGptHandler } from "../nanogpt"
1314
import { getModels } from "../fetchers/modelCache"
1415

@@ -171,6 +172,101 @@ describe("NanoGptHandler", () => {
171172
expect(mockCreate.mock.calls[0][0]).not.toHaveProperty("max_completion_tokens")
172173
})
173174

175+
it.each(["auto", "tools"] as const)("preserves read_file optionality with %s routing", async (routing) => {
176+
const tool = createReadFileTool()
177+
if (tool.type !== "function") throw new Error("read_file must be a function tool")
178+
const original = structuredClone(tool)
179+
const handler = new NanoGptHandler({ nanoGptModelId: "model:thinking", nanoGptRoutingPreference: routing })
180+
181+
await collectStream(
182+
handler.createMessage("sys", messages, { taskId: "task", tools: [tool], tool_choice: "auto" }),
183+
)
184+
185+
expect(mockCreate).toHaveBeenCalledWith(
186+
expect.objectContaining({
187+
tool_choice: "auto",
188+
tools: [{ ...original, function: { ...original.function, strict: false } }],
189+
}),
190+
expect.anything(),
191+
)
192+
expect(tool).toEqual(original)
193+
expect(tool.function.parameters?.required).toEqual(["path"])
194+
})
195+
196+
it.each(["custom_read", "mcp--files--read"])(
197+
"preserves nested, nullable, and required fields for %s",
198+
async (name) => {
199+
const tool: OpenAI.Chat.ChatCompletionTool = {
200+
type: "function",
201+
function: {
202+
name,
203+
strict: true,
204+
parameters: {
205+
type: "object",
206+
required: ["path"],
207+
additionalProperties: false,
208+
properties: {
209+
path: { type: "string" },
210+
label: { type: ["string", "null"] },
211+
options: {
212+
type: "object",
213+
required: ["offset"],
214+
properties: { offset: { type: "integer", minimum: 1 }, limit: { type: "integer" } },
215+
},
216+
ranges: {
217+
type: "array",
218+
items: {
219+
type: "object",
220+
required: ["start"],
221+
properties: { start: { type: "integer" }, end: { type: "integer" } },
222+
},
223+
},
224+
},
225+
},
226+
},
227+
}
228+
const original = structuredClone(tool)
229+
await collectStream(
230+
new NanoGptHandler({ nanoGptModelId: "model:thinking" }).createMessage("sys", messages, {
231+
taskId: "task",
232+
tools: [tool],
233+
tool_choice: { type: "function", function: { name } },
234+
}),
235+
)
236+
expect(mockCreate).toHaveBeenCalledWith(
237+
expect.objectContaining({
238+
tools: [{ ...original, function: { ...original.function, strict: false } }],
239+
tool_choice: { type: "function", function: { name } },
240+
}),
241+
expect.anything(),
242+
)
243+
expect(tool).toEqual(original)
244+
},
245+
)
246+
247+
it.each([undefined, []])("preserves an absent or empty tool catalog (%j)", async (tools) => {
248+
await collectStream(
249+
new NanoGptHandler({ nanoGptModelId: "model:thinking" }).createMessage("sys", messages, {
250+
taskId: "task",
251+
tools,
252+
}),
253+
)
254+
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ tools }), expect.anything())
255+
})
256+
257+
it("passes non-function tools through unchanged", async () => {
258+
const tools: OpenAI.Chat.ChatCompletionTool[] = [
259+
{ type: "custom", custom: { name: "custom_tool", format: { type: "text" } } },
260+
]
261+
await collectStream(
262+
new NanoGptHandler({ nanoGptModelId: "model:thinking" }).createMessage("sys", messages, {
263+
taskId: "task",
264+
tools,
265+
}),
266+
)
267+
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ tools }), expect.anything())
268+
})
269+
174270
it.each([
175271
["max", "max"],
176272
["none", "medium"],

‎src/api/providers/nanogpt.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ export class NanoGptHandler extends RouterProvider implements SingleCompletionHa
132132
stream: true,
133133
stream_options: { include_usage: true },
134134
max_tokens: info.maxTokens ?? undefined,
135-
tools: this.convertToolsForOpenAI(metadata?.tools),
135+
// Preserve the declared schema instead of making every optional field
136+
// required for OpenAI strict mode (e.g. read_file's indentation options).
137+
// Non-strict generation still retains every original required constraint.
138+
tools: metadata?.tools?.map((tool) =>
139+
tool.type === "function" ? { ...tool, function: { ...tool.function, strict: false } } : tool,
140+
),
136141
tool_choice: metadata?.tool_choice,
137142
parallel_tool_calls: isAstra ? false : (metadata?.parallelToolCalls ?? true),
138143
...(this.options.nanoGptRoutingPreference === "caching" ? { caching: true } : {}),

0 commit comments

Comments
 (0)