diff --git a/crates/language_models/src/provider/open_ai.rs b/crates/language_models/src/provider/open_ai.rs index 4348fd4211..85ddbc8aa3 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))); }