Skip to content

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

fix(openai): accept empty tool arguments as an object

Aditya Dattaf32eee44e6d2
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index 3d949151f6..3fb7ddb5e1 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -368,6 +368,9 @@ describe("createOpenAIChatRuntime tool approvals", () => {
+ 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n';
const answer = 'data: {"choices":[{"index":0,"delta":{"content":"Done."}}]}\n\n'
+ 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n';
+ const emptyArgumentsToolCall = (id: string, name: string, argumentsText: string) =>
+ `data: ${JSON.stringify({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id, type: "function", function: { name, arguments: argumentsText } }] } }] })}\n\n`
+ + 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n';
const run = async (replies: string[], rounds?: number, integrations?: SendTurnInput["integrations"]) => {
const bodies: any[] = [];
vi.stubGlobal("fetch", vi.fn(async (input: unknown, init?: RequestInit) => {
@@ -399,6 +402,13 @@ describe("createOpenAIChatRuntime tool approvals", () => {
.toEqual({ requests: 4, corrective: 1, ok: false, stopReason: "tool_error" });
}, 20_000);
+ it.each(["", " \t\n "])("treats whitespace-only tool arguments as an empty object (%j)", async (argumentsText) => {
+ expect(await run([
+ emptyArgumentsToolCall("c1", "fx_write", argumentsText),
+ answer,
+ ], undefined, { custom: { fx: toolServer() } })).toMatchObject({ requests: 2, ok: true });
+ }, 20_000);
+
it("keeps the failure when the bot answers the round without running a tool", async () => {
expect(await run([toolCall("c1", "nope"), answer, answer], 1))
.toEqual({ requests: 3, corrective: 1, ok: false, stopReason: "tool_error" });
diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.ts
index 22f440a48e..d8ee3d2938 100644
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@ -799,7 +799,8 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
let fatal: Error | undefined, title = call.function.name, skipRepeatCount = false;
try {
let args: unknown;
- try { args = JSON.parse(call.function.arguments); }
+ const rawArguments = call.function.arguments.trim() ? call.function.arguments : "{}";
+ try { args = JSON.parse(rawArguments); }
catch { throw new ChatProtocolError("tool arguments are not complete JSON"); }
if (!object(args)) throw new ChatProtocolError("tool arguments must be a JSON object");
const inputPreview = preview(args);