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.
fix/verification-admin-route-recovery
teknium1 2026-03-04 21:08:20 -08:00
parent 8e901b31c1
commit 41adca4e77
1 changed files with 9 additions and 1 deletions

View File

@ -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()