diff --git a/run_agent.py b/run_agent.py index b67b439f..19cc626e 100644 --- a/run_agent.py +++ b/run_agent.py @@ -2737,20 +2737,41 @@ class AIAgent: return kwargs - sanitized_messages = copy.deepcopy(api_messages) - for msg in sanitized_messages: + sanitized_messages = api_messages + needs_sanitization = False + for msg in api_messages: if not isinstance(msg, dict): continue - - # Codex-only replay state must not leak into strict chat-completions APIs. - msg.pop("codex_reasoning_items", None) + if "codex_reasoning_items" in msg: + needs_sanitization = True + break tool_calls = msg.get("tool_calls") if isinstance(tool_calls, list): for tool_call in tool_calls: - if isinstance(tool_call, dict): - tool_call.pop("call_id", None) - tool_call.pop("response_item_id", None) + if not isinstance(tool_call, dict): + continue + if "call_id" in tool_call or "response_item_id" in tool_call: + needs_sanitization = True + break + if needs_sanitization: + break + + if needs_sanitization: + sanitized_messages = copy.deepcopy(api_messages) + for msg in sanitized_messages: + if not isinstance(msg, dict): + continue + + # Codex-only replay state must not leak into strict chat-completions APIs. + msg.pop("codex_reasoning_items", None) + + tool_calls = msg.get("tool_calls") + if isinstance(tool_calls, list): + for tool_call in tool_calls: + if isinstance(tool_call, dict): + tool_call.pop("call_id", None) + tool_call.pop("response_item_id", None) provider_preferences = {} if self.providers_allowed: