From 5407d12bc61f18e2b5cd1988315448ad3ea71086 Mon Sep 17 00:00:00 2001 From: ygd58 Date: Fri, 20 Mar 2026 08:02:01 +0100 Subject: [PATCH] fix(agent): strip trailing empty assistant messages before API calls to prevent prefill rejection --- run_agent.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/run_agent.py b/run_agent.py index fcef3057..67a18758 100644 --- a/run_agent.py +++ b/run_agent.py @@ -2443,7 +2443,18 @@ class AIAgent: "Pre-call sanitizer: added %d stub tool result(s)", len(missing_results), ) - + # 3. Strip trailing empty assistant messages to prevent prefill rejection. + # These can leak from Responses API reasoning-only turns (Codex/MiniMax) + # where an empty assistant message is required by the Responses API but + # must NOT be sent to Chat Completions or Anthropic Messages API providers. + while ( + messages + and messages[-1].get("role") == "assistant" + and not (messages[-1].get("content") or "").strip() + and not messages[-1].get("tool_calls") + ): + logger.debug("Pre-call sanitizer: removed trailing empty assistant message") + messages = messages[:-1] return messages @staticmethod