From 63a8f8cc05f5528e648f04d048048d8ee85b33ce Mon Sep 17 00:00:00 2001 From: Alex McGraw Date: Fri, 23 Jan 2026 15:25:17 -0600 Subject: [PATCH] fixed the finish_reason bug; was being determined by rawToolCalls, when it should have been determined by validToolCalls in case all of the raw tool calls were filtered out because of malformed responses --- packages/openai-adapters/src/apis/AskSage.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/openai-adapters/src/apis/AskSage.ts b/packages/openai-adapters/src/apis/AskSage.ts index 77b907629..18c4f1bd4 100644 --- a/packages/openai-adapters/src/apis/AskSage.ts +++ b/packages/openai-adapters/src/apis/AskSage.ts @@ -413,9 +413,10 @@ export class AskSageApi implements BaseLlmApi { data.choices?.[0]?.message?.content || ""; - // Extract tool calls + // Extract tool calls and filter out malformed ones const rawToolCalls = data.tool_calls || data.choices?.[0]?.message?.tool_calls; + const validToolCalls = rawToolCalls?.filter((tc) => tc.function?.name); // Yield content as a single chunk if (content) { @@ -426,9 +427,7 @@ export class AskSageApi implements BaseLlmApi { } // Yield tool calls if present - if (rawToolCalls && rawToolCalls.length > 0) { - // Filter out malformed tool calls before processing - const validToolCalls = rawToolCalls.filter((tc) => tc.function?.name); + if (validToolCalls && validToolCalls.length > 0) { for (let i = 0; i < validToolCalls.length; i++) { const tc = validToolCalls[i]; yield chatChunkFromDelta({ @@ -455,7 +454,7 @@ export class AskSageApi implements BaseLlmApi { content: null, model: body.model, finish_reason: - rawToolCalls && rawToolCalls.length > 0 ? "tool_calls" : "stop", + validToolCalls && validToolCalls.length > 0 ? "tool_calls" : "stop", }); } catch (error) { if (error instanceof Error) {