Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 11e39aa

Browse files
committed
fix: emit tool_call_end events for any finish_reason, not just tool_calls
This fixes compatibility with OpenAI-compatible servers like ik_llama.cpp that may send finish_reason values like "stop" instead of "tool_calls" when tool calls are made. The change modifies NativeToolCallParser.processFinishReason() to emit tool_call_end events when ANY finish_reason is present and there are tracked tool calls that have started, ensuring tool calls are properly finalized regardless of the specific finish_reason value. Closes #9551
1 parent 78dc344 commit 11e39aa

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

src/core/assistant-message/NativeToolCallParser.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,27 @@ export class NativeToolCallParser {
146146

147147
/**
148148
* Process stream finish reason.
149-
* Emits end events when finish_reason is 'tool_calls'.
149+
* Emits end events when any finish_reason is present and there are tracked tool calls.
150+
*
151+
* This is important for compatibility with OpenAI-compatible servers like ik_llama.cpp
152+
* that may not send finish_reason: "tool_calls" specifically. Some servers send
153+
* "stop" or other finish reasons even when tool calls were made.
150154
*/
151155
public static processFinishReason(finishReason: string | null | undefined): ToolCallStreamEvent[] {
152156
const events: ToolCallStreamEvent[] = []
153157

154-
if (finishReason === "tool_calls" && this.rawChunkTracker.size > 0) {
158+
// Emit tool_call_end events when ANY finish_reason is present and we have tracked tool calls
159+
// This ensures compatibility with various OpenAI-compatible servers including ik_llama.cpp
160+
// that may not set finish_reason to "tool_calls" specifically
161+
if (finishReason && this.rawChunkTracker.size > 0) {
155162
for (const [, tracked] of this.rawChunkTracker.entries()) {
156-
events.push({
157-
type: "tool_call_end",
158-
id: tracked.id,
159-
})
163+
// Only emit end events for tool calls that have actually started
164+
if (tracked.hasStarted) {
165+
events.push({
166+
type: "tool_call_end",
167+
id: tracked.id,
168+
})
169+
}
160170
}
161171
}
162172

0 commit comments

Comments
 (0)