Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(openai-chat): resend when oMLX reports an unparseable tool call

RGB-loop13c43d481ad6
diff --git a/server/drivers/openai-chat-refusals.ts b/server/drivers/openai-chat-refusals.ts
index 92836e2a8f..bc0593fcdc 100644
--- a/server/drivers/openai-chat-refusals.ts
+++ b/server/drivers/openai-chat-refusals.ts
@@ -64,7 +64,9 @@ export function rejectsReasoningReplay(status: number, body: string): boolean {
}
/** Groq fails a completion with `tool_use_failed` when a generated tool call is invalid: usually a
- * tool it was not offered (gpt-oss inventing a "json" tool), sometimes arguments missing the schema. */
+ * tool it was not offered (gpt-oss inventing a "json" tool), sometimes arguments missing the schema.
+ * oMLX ends the stream with `invalid_tool_call` or `incomplete_tool_call` when it cannot parse the
+ * model's tool call. */
export class RejectedToolCallError extends ChatProtocolError {
readonly refusal: Record<string, unknown>;
constructor(message: string, refusal: Record<string, unknown>) {
@@ -73,9 +75,11 @@ export class RejectedToolCallError extends ChatProtocolError {
}
}
+const TOOL_CALL_FAILURES = new Set(["tool_use_failed", "invalid_tool_call", "incomplete_tool_call"]);
+
export const toolCallRefusal = (error: unknown) => {
const body = object(error);
- return body?.code === "tool_use_failed" ? body : undefined;
+ return typeof body?.code === "string" && TOOL_CALL_FAILURES.has(body.code) ? body : undefined;
};
export function refusedToolCall(status: number, body: string) {
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index 3fb7ddb5e1..d9083749e3 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -865,6 +865,28 @@ describe("createOpenAIChatRuntime refused tool calls", () => {
expect(events.find((event) => event.type === "runtime.error")?.message).toMatch(/^The model tried to use a tool it was not given \("JSON"\)/);
});
+ // oMLX's in-stream frame when a local model writes a tool call it cannot parse.
+ const omlxFailure = (code: string) => sse(`data: ${JSON.stringify({ error: {
+ message: "Model output contains an unrecoverable tool call. Previously delivered tool calls must not be executed again on retry.",
+ type: "server_error", param: null, code,
+ } })}\n\ndata: [DONE]\n\n`);
+
+ it.each(["invalid_tool_call", "incomplete_tool_call"])("sends the request again when oMLX reports %s mid-stream", async (code) => {
+ let requests = 0;
+ const events = await runTurn(() => ++requests === 1 ? omlxFailure(code) : answer());
+ expect(requests).toBe(2);
+ expect(events.find((event) => event.type === "turn.retrying")).toMatchObject({ attempt: 1, delayMs: 0, reason: "tool_use_failed" });
+ expect(events.some((event) => event.type === "runtime.error")).toBe(false);
+ expect(events.at(-1)).toMatchObject({ type: "turn.completed", ok: true });
+ });
+
+ it("stops after three oMLX tool call failures and says nothing ran", async () => {
+ let requests = 0;
+ const events = await runTurn(() => (requests++, omlxFailure("invalid_tool_call")));
+ expect(requests).toBe(3);
+ expect(events.find((event) => event.type === "runtime.error")?.message).toMatch(/^The model made a tool call the provider rejected\. Nothing ran\./);
+ });
+
it("does not resend other provider errors", async () => {
let requests = 0;
const events = await runTurn(() => (requests++, sse(`data: ${JSON.stringify({ error: { ...refusal, code: "invalid_request_error" } })}\n\n`)));