Skip to content

Commit 18c9aa1

Browse files
author
Zoo (VP)
committed
fix(mimo): retry once without strict tool schemas on endpoint rejection
MiMo sends tools through convertToolsForOpenAI(), which attaches a strict flag to every function tool. An OpenAI-compatible endpoint that doesn't support structured outputs rejects the request with a 400 and the turn fails outright. Mirror the existing parallel_tool_calls fallback: detect schema-rejection errors narrowly (400 status plus a mention of strict/additionalProperties in a tools context, so unrelated 400s like MiMo's missing-reasoning_content rejection are not retried) and retry once with the original schemas and no strict flag.
1 parent 3046cc8 commit 18c9aa1

2 files changed

Lines changed: 190 additions & 0 deletions

File tree

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

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,145 @@ describe("MimoHandler", () => {
518518
expect(textChunks[0].text).toBe("Retried")
519519
})
520520

521+
it("should retry without the strict flag when the endpoint rejects strict tool schemas", async () => {
522+
// First call rejects with a 400 error naming the strict field
523+
const rejectionError = Object.assign(new Error("400 - Unknown parameter: tools[0].function.strict"), {
524+
status: 400,
525+
})
526+
mockCreate.mockRejectedValueOnce(rejectionError)
527+
528+
// Second call (retry) succeeds
529+
mockCreate.mockImplementationOnce(async () => ({
530+
[Symbol.asyncIterator]: async function* () {
531+
yield {
532+
choices: [{ delta: { content: "Retried" }, index: 0 }],
533+
usage: null,
534+
}
535+
yield {
536+
choices: [{ delta: {}, index: 0, finish_reason: "stop" }],
537+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
538+
}
539+
},
540+
}))
541+
542+
const tools: OpenAI.Chat.ChatCompletionTool[] = [
543+
{
544+
type: "function",
545+
function: {
546+
name: "read_file",
547+
description: "Read a file",
548+
parameters: {
549+
type: "object",
550+
properties: { path: { type: "string" } },
551+
required: ["path"],
552+
},
553+
},
554+
},
555+
]
556+
557+
const messages: Anthropic.Messages.MessageParam[] = [
558+
{ role: "user", content: [{ type: "text", text: "Hello" }] },
559+
]
560+
561+
const chunks: ApiStreamChunk[] = []
562+
const stream = handler.createMessage("System prompt", messages, { taskId: "test-task", tools })
563+
for await (const chunk of stream) {
564+
chunks.push(chunk)
565+
}
566+
567+
expect(mockCreate).toHaveBeenCalledTimes(2)
568+
569+
// First call sent tools with the strict flag applied
570+
const firstCallParams = mockCreate.mock.calls[0][0]
571+
expect(firstCallParams.tools[0].function).toHaveProperty("strict")
572+
573+
// Retry stripped the strict flag but kept the original schema
574+
const retryCallParams = mockCreate.mock.calls[1][0]
575+
expect(retryCallParams.tools).toHaveLength(1)
576+
expect(retryCallParams.tools[0].function.name).toBe("read_file")
577+
expect(retryCallParams.tools[0].function).not.toHaveProperty("strict")
578+
expect(retryCallParams.tools[0].function.parameters).toEqual({
579+
type: "object",
580+
properties: { path: { type: "string" } },
581+
required: ["path"],
582+
})
583+
584+
const textChunks = chunks.filter((c) => c.type === "text")
585+
expect(textChunks[0].text).toBe("Retried")
586+
})
587+
588+
it("should retry without the strict flag when the endpoint rejects hardened schema fields", async () => {
589+
// 400 naming additionalProperties in a tools context
590+
const rejectionError = Object.assign(
591+
new Error("400 - Invalid tools: additionalProperties is not a supported field"),
592+
{ status: 400 },
593+
)
594+
mockCreate.mockRejectedValueOnce(rejectionError)
595+
596+
mockCreate.mockImplementationOnce(async () => ({
597+
[Symbol.asyncIterator]: async function* () {
598+
yield {
599+
choices: [{ delta: { content: "Retried" }, index: 0 }],
600+
usage: null,
601+
}
602+
yield {
603+
choices: [{ delta: {}, index: 0, finish_reason: "stop" }],
604+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
605+
}
606+
},
607+
}))
608+
609+
const tools: OpenAI.Chat.ChatCompletionTool[] = [
610+
{
611+
type: "function",
612+
function: { name: "read_file", description: "Read", parameters: {} },
613+
},
614+
]
615+
616+
const messages: Anthropic.Messages.MessageParam[] = [
617+
{ role: "user", content: [{ type: "text", text: "Hello" }] },
618+
]
619+
620+
const stream = handler.createMessage("System prompt", messages, { taskId: "test-task", tools })
621+
for await (const _chunk of stream) {
622+
// drain
623+
}
624+
625+
expect(mockCreate).toHaveBeenCalledTimes(2)
626+
const retryCallParams = mockCreate.mock.calls[1][0]
627+
expect(retryCallParams.tools[0].function).not.toHaveProperty("strict")
628+
})
629+
630+
it("should not retry schema-unrelated 400 errors", async () => {
631+
// A 400 about reasoning_content (not tool schemas) must NOT trigger
632+
// the strict-schema fallback.
633+
const rejectionError = Object.assign(
634+
new Error("400 - reasoning_content is required in multi-turn tool call conversations"),
635+
{ status: 400 },
636+
)
637+
mockCreate.mockRejectedValueOnce(rejectionError)
638+
639+
const tools: OpenAI.Chat.ChatCompletionTool[] = [
640+
{
641+
type: "function",
642+
function: { name: "read_file", description: "Read", parameters: {} },
643+
},
644+
]
645+
646+
const messages: Anthropic.Messages.MessageParam[] = [
647+
{ role: "user", content: [{ type: "text", text: "Hello" }] },
648+
]
649+
650+
await expect(async () => {
651+
const stream = handler.createMessage("System prompt", messages, { taskId: "test-task", tools })
652+
for await (const _chunk of stream) {
653+
// drain
654+
}
655+
}).rejects.toThrow()
656+
657+
expect(mockCreate).toHaveBeenCalledTimes(1)
658+
})
659+
521660
it("should send stream_options with include_usage", async () => {
522661
const messages: Anthropic.Messages.MessageParam[] = [
523662
{ role: "user", content: [{ type: "text", text: "Hello" }] },

‎src/api/providers/mimo.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,50 @@ function isParallelToolCallsRejected(error: unknown): boolean {
3434
return false
3535
}
3636

37+
/**
38+
* Detects whether an API error is specifically caused by the endpoint
39+
* rejecting the `strict` tool flag or a hardened strict-mode schema
40+
* (`additionalProperties: false`, forced `required`, ...). OpenAI-compatible
41+
* endpoints that don't support structured outputs typically return a 400
42+
* Bad Request naming the offending field.
43+
*
44+
* Detection is intentionally narrow (400 status plus a schema-specific
45+
* keyword) so unrelated 400s — e.g. MiMo's missing-reasoning_content
46+
* rejection — are NOT mistaken for schema rejections and retried pointlessly.
47+
*/
48+
function isStrictToolSchemaRejected(error: unknown): boolean {
49+
if (error instanceof Error) {
50+
const message = error.message.toLowerCase()
51+
const status = (error as { status?: number }).status
52+
if (status !== 400) {
53+
return false
54+
}
55+
if (message.includes("strict")) {
56+
return true
57+
}
58+
const mentionsTools = message.includes("tool") || message.includes("function")
59+
const mentionsSchemaField =
60+
message.includes("additionalproperties") || message.includes("additional_properties")
61+
return mentionsTools && mentionsSchemaField
62+
}
63+
return false
64+
}
65+
66+
/**
67+
* Removes the `strict` flag from function tools, keeping their original
68+
* (non-hardened) schemas. Used by the one-time retry fallback when an
69+
* endpoint rejects strict tool schemas.
70+
*/
71+
function stripStrictFromTools(tools: OpenAI.Chat.ChatCompletionTool[]): OpenAI.Chat.ChatCompletionTool[] {
72+
return tools.map((tool) => {
73+
if (tool.type !== "function") {
74+
return tool
75+
}
76+
const { strict: _omit, ...functionWithoutStrict } = tool.function
77+
return { ...tool, function: functionWithoutStrict }
78+
})
79+
}
80+
3781
/**
3882
* Filters a streamed delta so that only the FIRST tool call (index 0) survives.
3983
* MiMo v2.5 Pro ignores `parallel_tool_calls: false` and may emit multiple
@@ -205,6 +249,13 @@ export class MimoHandler extends OpenAiHandler {
205249
if (params.parallel_tool_calls !== undefined && isParallelToolCallsRejected(error)) {
206250
const { parallel_tool_calls: _omit, ...paramsWithoutParallel } = params
207251
stream = await this.client.chat.completions.create(paramsWithoutParallel as MiMoCompletionParams)
252+
} else if (params.tools !== undefined && isStrictToolSchemaRejected(error)) {
253+
// Fallback: if the endpoint rejects the strict tool flag or a
254+
// hardened strict-mode schema, retry once with the original
255+
// schemas and no strict flag. Build a new params object so the
256+
// rejected request is left untouched.
257+
const paramsWithoutStrict = { ...params, tools: stripStrictFromTools(tools ?? []) }
258+
stream = await this.client.chat.completions.create(paramsWithoutStrict)
208259
} else {
209260
throw handleProviderError(error, "MiMo")
210261
}

0 commit comments

Comments
 (0)