diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py index 4fd7b7b79d4d..61de1cfe4725 100644 --- a/agent/chat_completion_helpers.py +++ b/agent/chat_completion_helpers.py @@ -1392,7 +1392,18 @@ def build_assistant_message(agent, assistant_message, finish_reason: str) -> dic # the same trick the reasoning_content pad uses above (#15250, #17400). # Tool-call turns are exempt: ``content: ""`` alongside ``tool_calls`` # is accepted everywhere and normalizing it would alter cache keys. - if not _san_content and not assistant_tool_calls: + # codex_responses is exempt too: empty assistant content is a designed + # first-class state there (commentary-phase messages persist with + # content:"" while their text is delivered via the interim callback), + # and the Responses wire has no "assistant must not be empty" + # validation. A codex session later replayed through a strict + # chat-completions provider is still repaired by the send-time pad, + # which keys on the ACTIVE api_mode at replay time. + if ( + not _san_content + and not assistant_tool_calls + and getattr(agent, "api_mode", None) != "codex_responses" + ): _san_content = " " msg = { diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py index ba0864f7c49e..0f02173ae2ad 100644 --- a/agent/conversation_loop.py +++ b/agent/conversation_loop.py @@ -1653,15 +1653,22 @@ def run_conversation( # turn), but BEFORE apply_anthropic_cache_control rewrites content # into list blocks. Tool-call turns are exempt: ``content: ''`` # alongside ``tool_calls`` is accepted everywhere and normalizing - # it would alter prompt-cache keys. - for am in api_messages: - if ( - am.get("role") == "assistant" - and not am.get("tool_calls") - and isinstance(am.get("content"), str) - and not am["content"].strip() - ): - am["content"] = " " + # it would alter prompt-cache keys. codex_responses is exempt: + # empty assistant content is a designed first-class state there + # (commentary-phase turns persist with content:'') and the + # Responses wire has no empty-content validation. Keying on the + # ACTIVE api_mode means a codex-written empty turn is still + # repaired the moment the session replays through a strict + # chat-completions provider. + if getattr(agent, "api_mode", None) != "codex_responses": + for am in api_messages: + if ( + am.get("role") == "assistant" + and not am.get("tool_calls") + and isinstance(am.get("content"), str) + and not am["content"].strip() + ): + am["content"] = " " # Apply Anthropic prompt caching for Claude models on native # Anthropic, OpenRouter, and third-party Anthropic-compatible