The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(codex): send string role-message text as typed parts on the ChatGPT Codex backend
teknium13665a535bb67
diff --git a/agent/codex_responses_adapter.py b/agent/codex_responses_adapter.pyindex c8840300190..4a0156fcdf2 100644--- a/agent/codex_responses_adapter.py+++ b/agent/codex_responses_adapter.py@@ -540,6 +540,11 @@ def _chat_messages_to_responses_input(item_sources: List[Optional[Dict[str, Any]]] = []seen_item_ids: set = set()wire_ids = _WireCallIds()+ # The ChatGPT Codex backend rejects a role message whose ``content`` is a plain string with+ # ``{"detail": "Unsupported content type"}`` (400) — even a single user turn with no replay state+ # (#51512). It accepts only typed parts, so string text goes out as ``input_text``/``output_text``+ # there; other Responses routes keep the string shorthand they have always received.+ typed_text_only = current_issuer_kind == "codex_backend"def emit(new_items: List[Dict[str, Any]], msg: Dict[str, Any]) -> None:items.extend(new_items)item_sources.extend([msg] * len(new_items))@@ -559,8 +564,10 @@ def _chat_messages_to_responses_input("".join(p["text"] for p in content_parts if p["type"] == text_type)if isinstance(content, list) else _str_or_empty(content))+ def wire_content(value: Any) -> Any:+ return [{"type": text_type, "text": value}] if typed_text_only and isinstance(value, str) else valueif role == "user":- emit([{"role": role, "content": content_parts or content_text}], msg)+ emit([{"role": role, "content": wire_content(content_parts or content_text)}], msg)continuereasoning_items = [] if not replay_encrypted_reasoning else _replay_reasoning_items(msg, seen_item_ids=seen_item_ids, current_issuer_kind=current_issuer_kind,@@ -581,7 +588,7 @@ def _chat_messages_to_responses_input(# non-empty: strict Responses-compatible providers reject "" with 400.if fallback is not None and not (fallback == "" and tool_items):follower = " " if fallback == "" else fallback- emit([{"role": "assistant", "content": follower}], msg)+ emit([{"role": "assistant", "content": wire_content(follower)}], msg)emit(tool_items, msg)# The server renders nothing placed before a compaction item, so pre-checkpoint history is# dead weight and plaintext asks / merged summaries silently vanish. Keep the newest checkpointdiff --git a/tests/agent/test_native_compaction.py b/tests/agent/test_native_compaction.pyindex a530ef3c952..1328ce4578e 100644--- a/tests/agent/test_native_compaction.py+++ b/tests/agent/test_native_compaction.py@@ -668,6 +668,10 @@ class TestPrunePreCheckpointItems:assert [i.get("role") for i in items] == ["user", "assistant", "user"]+# The ChatGPT Codex backend route sends text as typed parts (#51512); the checks below only care that history survives.+_CODEX_ON_IT = [{"type": "output_text", "text": "on it"}]++class TestCheckpointGatedOnCurrentEligibility:"""A captured checkpoint must not outlive the native gate.@@ -712,11 +716,12 @@ class TestCheckpointGatedOnCurrentEligibility:{k: v for k, v in msg.items() if k != "codex_reasoning_items"}for msg in history],+ current_issuer_kind="codex_backend",)assert items == pre_feature# Specifically: no checkpoint on the wire, no deleted history.assert all(i.get("type") != "compaction" for i in items)- assert {"role": "assistant", "content": "on it"} in items+ assert {"role": "assistant", "content": _CODEX_ON_IT} in itemsdef test_eligible_request_still_restructures(self):from agent.codex_responses_adapter import _chat_messages_to_responses_input@@ -769,7 +774,7 @@ class TestCheckpointGatedOnCurrentEligibility:self._history(), is_codex_backend=True)assert all(i.get("type") != "compaction" for i in items)- assert {"role": "assistant", "content": "on it"} in items+ assert {"role": "assistant", "content": _CODEX_ON_IT} in itemsdef test_auxiliary_responses_adapter_never_prunes(self, monkeypatch):"""Auxiliary calls (compression, flush_memories, MoA) replay real@@ -807,4 +812,4 @@ class TestCheckpointGatedOnCurrentEligibility:assert seen.get("native_compaction_eligible") is Falseassert all(i.get("type") != "compaction" for i in seen["input"])- assert {"role": "assistant", "content": "on it"} in seen["input"]+ assert {"role": "assistant", "content": _CODEX_ON_IT} in seen["input"]diff --git a/tests/agent/transports/test_codex_transport.py b/tests/agent/transports/test_codex_transport.pyindex a9a5cb6bab5..6b90b7b4158 100644--- a/tests/agent/transports/test_codex_transport.py+++ b/tests/agent/transports/test_codex_transport.py@@ -258,6 +258,26 @@ class TestCodexBuildKwargs:assert "id" not in message_itemassert message_item["phase"] == "final_answer"+ @pytest.mark.parametrize(("is_codex_backend", "expected_user", "expected_assistant"), [+ (True, [{"type": "input_text", "text": "hi"}], [{"type": "output_text", "text": "pong"}]),+ (False, "hi", "pong"), # other Responses routes keep the string shorthand they always sent+ ], ids=["codex-typed-parts", "other-route-string"])+ def test_codex_backend_sends_typed_text_parts_for_string_content(+ self, transport, is_codex_backend, expected_user, expected_assistant,+ ):+ """#51512 (no-replay atom): the ChatGPT Codex backend 400s ``{"detail": "Unsupported content type"}``+ on a role message whose ``content`` is a plain string, even with no reasoning replay in the request.+ Text must go out as typed ``input_text``/``output_text`` parts; the preflight the real call runs+ through must keep them."""+ messages = [+ {"role": "user", "content": "hi"},+ {"role": "assistant", "content": "pong"},+ {"role": "user", "content": "hi"},+ ]+ kw = transport.build_kwargs(model="gpt-5.5", messages=messages, tools=[], is_codex_backend=is_codex_backend)+ kw = transport.preflight_kwargs(kw, sanitize_harmony_tokens=is_codex_backend)+ assert [item["content"] for item in kw["input"]] == [expected_user, expected_assistant, expected_user]+@pytest.mark.parametrize("model", ["gpt-5.5","gpt-5.5-pro",