Skip to content

fix(chat-completions): drop unknown message-object keys during coercion - #637

Merged
Soju06 merged 1 commit into
mainfrom
fix/chat-completions-drop-unknown-message-fields
May 15, 2026
Merged

Soju06 merged 1 commit into
mainfrom
fix/chat-completions-drop-unknown-message-fields

Conversation

@Soju06

@Soju06 Soju06 commented May 15, 2026

Copy link
Copy Markdown
Owner

Summary

Match OpenAI's /v1/chat/completions behavior on unknown message-object fields. OpenAI parses the documented chat-message fields and silently ignores everything else on the message object. codex-lb's chat→Responses coercion was instead copying the inbound message dict wholesale, so any extra key (the standard name field, client-internal bookkeeping keys, etc.) leaked into the Responses input items and was forwarded upstream verbatim. The upstream Responses API only accepts role + content on an input message item — any other key trips an unknown_parameter hard error, which surfaces as a deterministic 502 and poisons every subsequent request that replays the same message buffer.

OpenSpec change: drop-unknown-message-fields.

Reproducer

Verified against https://codex.nekos.me/v1/chat/completions on 2026-05-15, captured from a real Hermes Agent session:

502 invalid_request_error:
[ObjectParam] [input[617]._empty_recovery_synthetic]
[unknown_parameter] Unknown parameter: 'input[617]._empty_recovery_synthetic'

The client's empty-response recovery path had appended synthetic messages tagged with _empty_recovery_synthetic — a normal client pattern (bookkeeping flags on the local message buffer). codex-lb relayed the flag into input[N], upstream returned 502, and the client's session buffer replayed the poisoned input on every subsequent turn. The standard chat name field reproduces the same 502 the same way.

Direct probe before the fix:

curl -s -X POST https://codex.nekos.me/v1/chat/completions -H 'Authorization: Bearer …' \
  -H 'Content-Type: application/json' \
  -d '{"model":"gpt-5.5","messages":[{"role":"user","content":"hi","name":"alice"}],"max_tokens":5}'
# → 502 {"error":{"message":"[ObjectParam] [input[0].name] [unknown_parameter] Unknown parameter: 'input[0].name'."}}

Same with any other extra key on a message object.

Root cause

app/core/openai/message_coercion.py::_normalize_message_content was building the output message via updated = dict(message); updated["content"] = normalized, which copies every key. Result:

# Before the fix
input items:
  [0] {"role":"user","content":[…],"name":"alice","_empty_recovery_synthetic":true}
  [1] {"role":"assistant","content":[…],"_empty_recovery_synthetic":true}
  [2] {"role":"user","content":[…],"_empty_recovery_synthetic":true}

Fix

Replace the copy-and-overwrite pattern with explicit field selection — emit {"role": role, "content": normalized_content} and nothing else. Refusal still folds into content as a refusal part (unchanged); tool_calls still flow through _decompose_assistant_tool_calls (unchanged); the message-item path now matches OpenAI's lenient-input, strict-output contract.

# After the fix
input items:
  [0] {"role":"user","content":[{"type":"input_text","text":"hi"}]}
  [1] {"role":"assistant","content":[{"type":"output_text","text":"(empty)"}]}
  [2] {"role":"user","content":[{"type":"input_text","text":"continue"}]}

Tests

tests/unit/test_chat_request_mapping.py gains three tests:

  • test_chat_unknown_message_keys_are_dropped — general unknown-key case and the _empty_recovery_synthetic reproducer
  • test_chat_message_name_field_is_dropped — standard name field
  • test_chat_assistant_tool_call_message_drops_unknown_keys — assistant + tool_calls path (the message half emitted alongside the decomposed tool call must also be clean)

Results:

  • Targeted: tests/unit/test_chat_request_mapping.py + tests/unit/test_openai_requests.py + tests/integration/test_proxy_chat_completions.py + tests/integration/test_openai_client_compat.py + tests/integration/test_openai_compat_features.py — 187 passed.
  • Broader sweep: unit + integration + e2e proxy filtered to coercion/chat/responses/proxy — 633 passed, 0 new failures.
  • openspec validate drop-unknown-message-fields → valid.

Out of scope

  • Status-code mapping for upstream unknown_parameter errors that escape via other paths. The chat-completions adapter already converts upstream invalid_request_error to 400 for non-streaming responses; this PR keeps that behavior unchanged.
  • Other compatibility gaps (e.g. logprobs handling). Tracked separately if needed.

OpenAI's /v1/chat/completions parses the documented chat-message
fields and silently ignores everything else. codex-lb's
chat-to-Responses coercion was instead copying the inbound message
dict wholesale and only overwriting 'content', so any extra key on
a message object (the standard 'name' field, client-internal
bookkeeping keys, etc.) leaked into the Responses 'input' items and
was forwarded upstream verbatim.

The upstream Responses API only accepts 'role' + 'content' on an
input message item. Any other key trips an 'unknown_parameter' hard
error, which the gateway surfaces as a deterministic 502 and which
poisons every later request that replays the same message buffer.

Concrete reproducer (Hermes Agent, 2026-05-15): the agent's
empty-response recovery appends synthetic messages tagged with an
internal '_empty_recovery_synthetic' bookkeeping flag — a normal
client pattern. codex-lb relayed the flag into 'input[N]', upstream
returned 502 Unknown parameter, and the agent's session buffer
replayed the poisoned input on every subsequent turn, producing a
retry flood.

Rewrite _normalize_message_content to emit {role, content} explicitly
instead of copying the inbound message dict. Refusal still folds into
content as a refusal part; tool_calls still flow through their
existing decomposition path (FunctionCallInputItem / message-half
split); the message-item path now matches OpenAI's lenient-input,
strict-output semantics.

Add three unit tests covering: the general unknown-key case (and the
specific '_empty_recovery_synthetic' reproducer), the standard 'name'
field, and the assistant + tool_calls path.

OpenSpec change: drop-unknown-message-fields

Targeted suites: 187 passed.
Broader sweep (unit + integration + e2e proxy): 633 passed, 0 new failures.
@Soju06
Soju06 force-pushed the fix/chat-completions-drop-unknown-message-fields branch 2 times, most recently from aeca588 to ff9e84c Compare May 15, 2026 04:34
@Soju06
Soju06 merged commit d314d00 into main May 15, 2026
34 of 35 checks passed
@Soju06
Soju06 deleted the fix/chat-completions-drop-unknown-message-fields branch May 15, 2026 04:53
Soju06 added a commit that referenced this pull request May 17, 2026
Release 1.18.0 — v1.17.0 → main, 27 PR.

## Pre-merge verification (release-gate e2e)

- ✅ CI 18/18 (mergeStateStatus CLEAN)
- ✅ baseline release-gate-smoke: 10/10 (`v1.models`, chat non-stream/stream/tool_call, responses non-stream/stream, unknown-msg-key drop, transient codes, dashboard 72-byte, alembic index)
- ✅ per-PR e2e extra: 14/14 (#658 strict tool × chat valid/invalid/type-omitted + responses invalid, #650-652 trim replay markers, #516 prev-resp miss mask, #558 prolite plan, #600 token_expired, #635 device poll, #647 frontend 72B, #594 account selection, #640 alembic head, #653/#654 conv archive, #655/#656 weekly pace)
- ✅ pytest 562 passed / 3 PG-only skip (unit + integration suite)
- ✅ SDK e2e 20/20 (`tests/e2e/test_openai_sdk_compat.py` + `tests/e2e/test_v1_responses_openai_sdk.py`)
- ✅ `scripts/verify_v1_responses_openai_sdk.py` 5/5 against live container with real account
- ✅ ruff check + ruff format --check + uv run ty check — All clean
- ✅ prod parity probe: #637 (unknown-key drop) + #658 (strict tool 400) both confirmed fixed vs prod 1.17.0

## Behavior change to note in announcements

- **#658**: `strict: true` function tool schemas missing `additionalProperties: false` (or other strict-mode requirements) now return a local **400 `invalid_function_parameters`** with `param=tools[N].function.parameters` (chat) / `tools[N].parameters` (responses). Pre-1.18.0 these were silently forwarded upstream; 1.18.0 enforces OpenAI's documented strict-mode contract locally before forwarding.

OpenSpec changes shipped in this release (to be archived after tag publish):
- deactivate-on-token-expired (#600)
- drop-unknown-message-fields (#637)
- mask-single-http-previous-response-miss (#516)
- normalize-v1-responses-openai-sdk-stream (#639)
- start-device-oauth-polling (#635)
- support-prolite-plan-capacity (#558)
- treat-upstream-overloaded-as-retryable (#601)
- validate-password-length (#598)
- validate-strict-function-tool-schema (#658)
Soju06 added a commit that referenced this pull request May 17, 2026
Archive changes shipped in v1.18.0 and v1.18.1 per AGENTS.md
"release 이후 archive" workflow. All 12 changes verified shipped in
production at 10.0.0.113 (image 1.18.1, sha256:0431a474432d).

1.18.0 (9 changes):
- deactivate-on-token-expired (#600)
- drop-unknown-message-fields (#637)
- mask-single-http-previous-response-miss (#516)
- normalize-v1-responses-openai-sdk-stream (#639)
- start-device-oauth-polling (#635) [delta header fixed: MODIFIED -> ADDED]
- support-prolite-plan-capacity (#558) [delta header fixed: MODIFIED -> ADDED]
- treat-upstream-overloaded-as-retryable (#601)
- validate-password-length (#598)
- validate-strict-function-tool-schema (#658)

1.18.1 (3 changes):
- harden-long-codex-websocket-turns (#586)
- suppress-duplicate-side-effect-tool-calls (#586)
- hotfix-db-pool-pre-ping-and-firewall-cache-ttl (#679)

Specs updated: 9 capabilities (admin-auth, api-firewall, chat-completions-compat,
chat-completions-extensions, database-backends, frontend-architecture,
responses-api-compat, usage-refresh-policy).

openspec validate --specs: 19/19 passed.

Co-authored-by: Hermes Agent <[email protected]>
vitalNohj pushed a commit to vitalNohj/codex-lb that referenced this pull request Jul 17, 2026
…on (Soju06#637)

OpenAI's /v1/chat/completions parses the documented chat-message
fields and silently ignores everything else. codex-lb's
chat-to-Responses coercion was instead copying the inbound message
dict wholesale and only overwriting 'content', so any extra key on
a message object (the standard 'name' field, client-internal
bookkeeping keys, etc.) leaked into the Responses 'input' items and
was forwarded upstream verbatim.

The upstream Responses API only accepts 'role' + 'content' on an
input message item. Any other key trips an 'unknown_parameter' hard
error, which the gateway surfaces as a deterministic 502 and which
poisons every later request that replays the same message buffer.

Concrete reproducer (Hermes Agent, 2026-05-15): the agent's
empty-response recovery appends synthetic messages tagged with an
internal '_empty_recovery_synthetic' bookkeeping flag — a normal
client pattern. codex-lb relayed the flag into 'input[N]', upstream
returned 502 Unknown parameter, and the agent's session buffer
replayed the poisoned input on every subsequent turn, producing a
retry flood.

Rewrite _normalize_message_content to emit {role, content} explicitly
instead of copying the inbound message dict. Refusal still folds into
content as a refusal part; tool_calls still flow through their
existing decomposition path (FunctionCallInputItem / message-half
split); the message-item path now matches OpenAI's lenient-input,
strict-output semantics.

Add three unit tests covering: the general unknown-key case (and the
specific '_empty_recovery_synthetic' reproducer), the standard 'name'
field, and the assistant + tool_calls path.

OpenSpec change: drop-unknown-message-fields

Targeted suites: 187 passed.
Broader sweep (unit + integration + e2e proxy): 633 passed, 0 new failures.

Co-authored-by: Soju <[email protected]>
vitalNohj pushed a commit to vitalNohj/codex-lb that referenced this pull request Jul 17, 2026
Release 1.18.0 — v1.17.0 → main, 27 PR.

## Pre-merge verification (release-gate e2e)

- ✅ CI 18/18 (mergeStateStatus CLEAN)
- ✅ baseline release-gate-smoke: 10/10 (`v1.models`, chat non-stream/stream/tool_call, responses non-stream/stream, unknown-msg-key drop, transient codes, dashboard 72-byte, alembic index)
- ✅ per-PR e2e extra: 14/14 (Soju06#658 strict tool × chat valid/invalid/type-omitted + responses invalid, Soju06#650-652 trim replay markers, Soju06#516 prev-resp miss mask, Soju06#558 prolite plan, Soju06#600 token_expired, Soju06#635 device poll, Soju06#647 frontend 72B, Soju06#594 account selection, Soju06#640 alembic head, Soju06#653/Soju06#654 conv archive, Soju06#655/Soju06#656 weekly pace)
- ✅ pytest 562 passed / 3 PG-only skip (unit + integration suite)
- ✅ SDK e2e 20/20 (`tests/e2e/test_openai_sdk_compat.py` + `tests/e2e/test_v1_responses_openai_sdk.py`)
- ✅ `scripts/verify_v1_responses_openai_sdk.py` 5/5 against live container with real account
- ✅ ruff check + ruff format --check + uv run ty check — All clean
- ✅ prod parity probe: Soju06#637 (unknown-key drop) + Soju06#658 (strict tool 400) both confirmed fixed vs prod 1.17.0

## Behavior change to note in announcements

- **Soju06#658**: `strict: true` function tool schemas missing `additionalProperties: false` (or other strict-mode requirements) now return a local **400 `invalid_function_parameters`** with `param=tools[N].function.parameters` (chat) / `tools[N].parameters` (responses). Pre-1.18.0 these were silently forwarded upstream; 1.18.0 enforces OpenAI's documented strict-mode contract locally before forwarding.

OpenSpec changes shipped in this release (to be archived after tag publish):
- deactivate-on-token-expired (Soju06#600)
- drop-unknown-message-fields (Soju06#637)
- mask-single-http-previous-response-miss (Soju06#516)
- normalize-v1-responses-openai-sdk-stream (Soju06#639)
- start-device-oauth-polling (Soju06#635)
- support-prolite-plan-capacity (Soju06#558)
- treat-upstream-overloaded-as-retryable (Soju06#601)
- validate-password-length (Soju06#598)
- validate-strict-function-tool-schema (Soju06#658)
vitalNohj pushed a commit to vitalNohj/codex-lb that referenced this pull request Jul 17, 2026
…u06#682)

Archive changes shipped in v1.18.0 and v1.18.1 per AGENTS.md
"release 이후 archive" workflow. All 12 changes verified shipped in
production at 10.0.0.113 (image 1.18.1, sha256:0431a474432d).

1.18.0 (9 changes):
- deactivate-on-token-expired (Soju06#600)
- drop-unknown-message-fields (Soju06#637)
- mask-single-http-previous-response-miss (Soju06#516)
- normalize-v1-responses-openai-sdk-stream (Soju06#639)
- start-device-oauth-polling (Soju06#635) [delta header fixed: MODIFIED -> ADDED]
- support-prolite-plan-capacity (Soju06#558) [delta header fixed: MODIFIED -> ADDED]
- treat-upstream-overloaded-as-retryable (Soju06#601)
- validate-password-length (Soju06#598)
- validate-strict-function-tool-schema (Soju06#658)

1.18.1 (3 changes):
- harden-long-codex-websocket-turns (Soju06#586)
- suppress-duplicate-side-effect-tool-calls (Soju06#586)
- hotfix-db-pool-pre-ping-and-firewall-cache-ttl (Soju06#679)

Specs updated: 9 capabilities (admin-auth, api-firewall, chat-completions-compat,
chat-completions-extensions, database-backends, frontend-architecture,
responses-api-compat, usage-refresh-policy).

openspec validate --specs: 19/19 passed.

Co-authored-by: Hermes Agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant