From 46da710a1b80653b1a36e1ccbdc06faac660d180 Mon Sep 17 00:00:00 2001 From: Roberto Date: Mon, 9 Mar 2026 19:30:19 -0500 Subject: [PATCH] Fix crash when OpenRouter sends chunks with empty choices array Use optional chaining on choices[0].delta.tool_calls to guard against chunks (e.g. usage/close chunks) that have no choices, which caused "undefined is not an object" errors for all OpenRouter streaming calls. Co-Authored-By: Claude Sonnet 4.6 --- src/core/chorus/OpenAICompletionsAPIUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/chorus/OpenAICompletionsAPIUtils.ts b/src/core/chorus/OpenAICompletionsAPIUtils.ts index ec057da1..300a5e1b 100644 --- a/src/core/chorus/OpenAICompletionsAPIUtils.ts +++ b/src/core/chorus/OpenAICompletionsAPIUtils.ts @@ -199,7 +199,7 @@ function convertToolCalls( // adapted from a snippet on https://platform.openai.com/docs/guides/function-calling?api-mode=chat&lang=javascript // which appears to be severely buggy (lol) for (const chunk of chunks) { - const toolCallDeltas = chunk.choices[0].delta.tool_calls || []; + const toolCallDeltas = chunk.choices?.[0]?.delta?.tool_calls || []; for (const toolCallDelta of toolCallDeltas) { const index = toolCallDelta.index ?? 0;