From 41adca4e772ca232ce3b3a38045329be47765f05 Mon Sep 17 00:00:00 2001 From: teknium1 Date: Wed, 4 Mar 2026 21:08:20 -0800 Subject: [PATCH] fix: strip internal fields from API messages in _handle_max_iterations The flush_memories() and run_conversation() code paths already stripped finish_reason and reasoning from API messages (added in 7a0b377 via PR #253), but _handle_max_iterations() was missed. It was sending raw messages.copy() which could include finish_reason, causing 422 errors on strict APIs like Mistral when the agent hit max iterations. Now strips the same internal fields consistently across all three API call sites. --- run_agent.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/run_agent.py b/run_agent.py index 8df43d750..95e8a5453 100644 --- a/run_agent.py +++ b/run_agent.py @@ -2649,7 +2649,15 @@ class AIAgent: messages.append({"role": "user", "content": summary_request}) try: - api_messages = messages.copy() + # Build API messages, stripping internal-only fields + # (finish_reason, reasoning) that strict APIs like Mistral reject with 422 + api_messages = [] + for msg in messages: + api_msg = msg.copy() + for internal_field in ("reasoning", "finish_reason"): + api_msg.pop(internal_field, None) + api_messages.append(api_msg) + effective_system = self._cached_system_prompt or "" if self.ephemeral_system_prompt: effective_system = (effective_system + "\n\n" + self.ephemeral_system_prompt).strip()