Skip to content

The agent that grows with you

fix(codex): sanitize replayed function_call.name to Responses API pattern (#31666)

kshitijk4poor31485d50ea44
diff --git a/agent/codex_responses_adapter.py b/agent/codex_responses_adapter.py
index 1b02a626f0c..bd90e0ad919 100644
--- a/agent/codex_responses_adapter.py
+++ b/agent/codex_responses_adapter.py
@@ -288,6 +288,43 @@ def _clamp_responses_call_id(call_id: str) -> str:
return f"call_{digest}"
+# The Responses API enforces the same 64-char cap on function names as on
+# input item ids (_MAX_RESPONSES_ITEM_ID_LENGTH) — names over the cap are
+# rejected with the same non-retryable 400 as pattern violations.
+_VALID_RESPONSES_FN_NAME_RE = re.compile(r"[a-zA-Z0-9_-]{1,64}")
+
+
+def _sanitize_replayed_fn_name(name: str) -> str:
+ """Coerce a *replayed* function_call name to the Responses API contract.
+
+ The Responses API requires ``function_call.name`` to match
+ ``^[a-zA-Z0-9_-]+$`` and rejects the whole request with a non-retryable
+ HTTP 400 otherwise (issue #31666). A name with invalid characters (dots,
+ spaces, unicode — e.g. from an earlier model degeneration) stored in
+ conversation history therefore bricks every subsequent turn of the
+ session: the 400 replays forever until the user manually starts a new
+ conversation.
+
+ Invalid characters are replaced with ``_`` (runs collapsed) rather than
+ stripped, so an all-invalid name degrades to the ``"fn"`` placeholder
+ instead of an empty string — an empty name would just trade one
+ non-retryable 400 for a preflight ValueError. Valid names pass through
+ unchanged, preserving prompt-cache prefixes.
+
+ Apply this ONLY to replayed function_call input items, never to live
+ tool definitions: tool schema names must match the dispatch registry
+ exactly. Pairing with function_call_output is by call_id, so renaming
+ a replayed function_call is safe.
+ """
+ if not isinstance(name, str):
+ return "fn"
+ if _VALID_RESPONSES_FN_NAME_RE.fullmatch(name):
+ return name
+ coerced = re.sub(r"[^A-Za-z0-9_-]", "_", name.strip())
+ coerced = re.sub(r"_+", "_", coerced).strip("_")
+ return coerced[:64] or "fn"
+
+
def _split_responses_tool_id(raw_id: Any) -> tuple[Optional[str], Optional[str]]:
"""Split a stored tool id into (call_id, response_item_id)."""
if not isinstance(raw_id, str):
@@ -688,7 +725,7 @@ def _chat_messages_to_responses_input(
items.append({
"type": "function_call",
"call_id": _clamp_responses_call_id(call_id),
- "name": fn_name,
+ "name": _sanitize_replayed_fn_name(fn_name),
"arguments": arguments,
})
item_sources.append(msg)
@@ -813,7 +850,7 @@ def _preflight_codex_input_items(
{
"type": "function_call",
"call_id": call_id.strip(),
- "name": name.strip(),
+ "name": _sanitize_replayed_fn_name(name),
"arguments": arguments,
}
)
diff --git a/tests/agent/test_codex_responses_adapter.py b/tests/agent/test_codex_responses_adapter.py
index 1ee944035b9..611f51ceb7d 100644
--- a/tests/agent/test_codex_responses_adapter.py
+++ b/tests/agent/test_codex_responses_adapter.py
@@ -4,6 +4,7 @@ import pytest
from agent.codex_responses_adapter import (
_chat_messages_to_responses_input,
+ _sanitize_replayed_fn_name,
_format_responses_error,
_normalize_codex_response,
_neutralize_harmony_tokens,
@@ -227,8 +228,6 @@ def test_normalize_codex_response_treats_summary_only_reasoning_as_incomplete():
assert assistant_message.codex_reasoning_items is None
-
-
# ---------------------------------------------------------------------------
# Server-side built-in tool calls (xAI native web_search, code interpreter,
# etc.) come back as discrete ``*_call`` output items that xAI's
@@ -243,10 +242,6 @@ def test_normalize_codex_response_treats_summary_only_reasoning_as_incomplete():
# ---------------------------------------------------------------------------
-
-
-
-
# ---------------------------------------------------------------------------
# Replayed assistant message items with an oversized server-assigned ``id``
# (Codex issues 400+ char base64 blobs) must never reach the API — the
@@ -261,10 +256,6 @@ _OVERSIZED_ITEM_ID = "x" * 408
_VALID_ITEM_ID = "msg_abc123"
-
-
-
-
# The codex app-server overflows the Responses 64-char call_id limit for
# MCP-routed tools, e.g. codex_mcp__hermes-tools__web_search_exec-<uuid> (#73492).
_OVERSIZED_CALL_ID = "codex_mcp__hermes-tools__web_search_exec-" + "0" * 43
@@ -331,8 +322,96 @@ def test_chat_messages_to_responses_input_keeps_short_call_id():
assert output["call_id"] == "call_abc123"
+def test_sanitize_replayed_fn_name_valid_passthrough():
+ """Valid names pass through unchanged (identity — cache-prefix safe)."""
+ for name in ("web_search", "exec-command", "a1_B2-c3", "x" * 64):
+ assert _sanitize_replayed_fn_name(name) == name
+
+
+def test_sanitize_replayed_fn_name_coerces_invalid_chars():
+ assert _sanitize_replayed_fn_name("exec.command") == "exec_command"
+ assert _sanitize_replayed_fn_name("run shell cmd") == "run_shell_cmd"
+ assert _sanitize_replayed_fn_name("weird..__name") == "weird_name"
+ assert _sanitize_replayed_fn_name(" tool! ") == "tool"
+
+
+def test_sanitize_replayed_fn_name_degenerate_inputs():
+ """All-invalid / non-string names degrade to a placeholder, never empty —
+ an empty name would trade the API 400 for a preflight ValueError."""
+ assert _sanitize_replayed_fn_name("") == "fn"
+ assert _sanitize_replayed_fn_name("...") == "fn"
+ assert _sanitize_replayed_fn_name("日本語") == "fn"
+ assert _sanitize_replayed_fn_name(None) == "fn"
+ assert len(_sanitize_replayed_fn_name("a." * 100)) <= 64
+
+
+def test_chat_messages_to_responses_input_sanitizes_replayed_fn_name():
+ """A degenerate tool name stored in history must not brick the replay
+ with a non-retryable 400 (#31666)."""
+ messages = [
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "call_id": "call_abc123",
+ "function": {"name": "exec.command", "arguments": "{}"},
+ }
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_abc123",
+ "content": "some result",
+ },
+ ]
+
+ items = _chat_messages_to_responses_input(messages)
+
+ call = next(i for i in items if i.get("type") == "function_call")
+ output = next(i for i in items if i.get("type") == "function_call_output")
+ assert call["name"] == "exec_command"
+ # Pairing is by call_id and must survive the rename.
+ assert call["call_id"] == output["call_id"] == "call_abc123"
+def test_preflight_codex_input_items_sanitizes_replayed_fn_name():
+ """The preflight choke-point also coerces invalid replayed names
+ (covers callers that build input items without the chat converter)."""
+ normalized = _preflight_codex_input_items(
+ [
+ {
+ "type": "function_call",
+ "call_id": "call_1",
+ "name": "bad name!",
+ "arguments": "{}",
+ },
+ {"type": "function_call_output", "call_id": "call_1", "output": "ok"},
+ ]
+ )
+ call = next(i for i in normalized if i.get("type") == "function_call")
+ assert call["name"] == "bad_name"
+
+
+def test_preflight_codex_api_kwargs_leaves_tool_definition_names_alone():
+ """Live tool schema names must NOT be rewritten — they have to match the
+ dispatch registry exactly. Sanitization is replay-only."""
+ kwargs = _preflight_codex_api_kwargs(
+ {
+ "model": "gpt-5-codex",
+ "instructions": "x",
+ "input": [{"role": "user", "content": "hi"}],
+ "tools": [
+ {
+ "type": "function",
+ "name": "my_tool",
+ "description": "",
+ "parameters": {"type": "object", "properties": {}},
+ }
+ ],
+ }
+ )
+ assert kwargs["tools"][0]["name"] == "my_tool"
def test_preflight_codex_input_items_drops_short_id_for_github_responses():
@@ -408,8 +487,6 @@ def test_preflight_passes_native_web_search_tool_through():
assert any(t.get("type") == "function" and t.get("name") == "read_file" for t in tools)
-
-
# ---------------------------------------------------------------------------
# _format_responses_error — adapted from anomalyco/opencode#28757.
# Provider failures should surface BOTH the code (rate_limit_exceeded /
@@ -419,25 +496,11 @@ def test_preflight_passes_native_web_search_tool_through():
# ---------------------------------------------------------------------------
-
-
def test_format_responses_error_message_only():
err = {"message": "Upstream model unavailable"}
assert _format_responses_error(err, "failed") == "Upstream model unavailable"
-
-
-
-
-
-
-
-
-
-
-
-
def test_normalize_codex_response_failed_includes_code_in_error():
"""Regression: response_status == 'failed' should surface the error
code, not just the message. Used to leak a bare 'Slow down' string
@@ -461,8 +524,6 @@ def test_normalize_codex_response_failed_includes_code_in_error():
_normalize_codex_response(response)
-
-
# ---------------------------------------------------------------------------
# Reasoning-channel answer salvage (xAI grok) — grok-4.x on the xAI
# /v1/responses surface sometimes emits its final answer inside the