diff --git a/run_agent.py b/run_agent.py index 40c3eae12..6cfcb1b8f 100644 --- a/run_agent.py +++ b/run_agent.py @@ -2126,7 +2126,8 @@ class AIAgent: _is_openrouter = "openrouter" in self.base_url.lower() _is_nous = "nousresearch" in self.base_url.lower() - if _is_openrouter or _is_nous: + _is_mistral = "api.mistral.ai" in self.base_url.lower() + if (_is_openrouter or _is_nous) and not _is_mistral: if self.reasoning_config is not None: extra_body["reasoning"] = self.reasoning_config else: @@ -2271,6 +2272,7 @@ class AIAgent: if reasoning: api_msg["reasoning_content"] = reasoning api_msg.pop("reasoning", None) + api_msg.pop("finish_reason", None) api_messages.append(api_msg) if self._cached_system_prompt: @@ -2912,6 +2914,9 @@ class AIAgent: # We've copied it to 'reasoning_content' for the API above if "reasoning" in api_msg: api_msg.pop("reasoning") + # Remove finish_reason - not accepted by strict APIs (e.g. Mistral) + if "finish_reason" in api_msg: + api_msg.pop("finish_reason") # Keep 'reasoning_details' - OpenRouter uses this for multi-turn reasoning context # The signature field helps maintain reasoning continuity api_messages.append(api_msg)