From 78f2dda8bfbd047b586562d71c0473d3bb4aec38 Mon Sep 17 00:00:00 2001 From: 0x11 Date: Tue, 19 Aug 2025 13:07:27 +0000 Subject: [PATCH] fix(agent): inferring finish_reason when llm provider not set finish_reason correct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit some llm provider set finish_reason to “stop” when the model is calling a function. see also https://community.openai.com/t/function-call-with-finish-reason-of-stop/437226/21 . --- crates/language_models/src/provider/open_ai.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/language_models/src/provider/open_ai.rs b/crates/language_models/src/provider/open_ai.rs index 1a5c09cdc4..c135068ab7 100644 --- a/crates/language_models/src/provider/open_ai.rs +++ b/crates/language_models/src/provider/open_ai.rs @@ -609,7 +609,15 @@ impl OpenAiEventMapper { } } - match choice.finish_reason.as_deref() { + let mut effective_finish_reason = choice.finish_reason.as_deref(); + + if effective_finish_reason == Some("stop") && !self.tool_calls_by_index.is_empty() + { + log::warn!("finish_reason is 'stop' but tool calls are present; inferring 'tool_calls'"); + effective_finish_reason = Some("tool_calls"); + } + + match effective_finish_reason { Some("stop") => { events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn))); }