Repository navigation
Conversation
commit cfcc87a changed how response items are batched and recorded, which caused tool call objects to be recorded before the assistant message content in some turns. This could result in a { role: "assistant", content: null, tool_calls: [...] } message being sent before the actual assistant message with text content, breaking ordering expectations downstream. This change reorders the items collected for a single turn so that the last non-empty assistant message is placed immediately before the first tool call object when both occur in the same turn, without perturbing prior history. A new unit test ensures that recorded items now appear in the expected order: assistant text, then tool call object, then the corresponding tool output input item.
|
Thanks for the contribution. Is there an open bug report associated with this PR? We ask that all bug fix PRs have a linked bug report (see contribution guidelines). It's not clear to me what problem this PR is solving. You mentioned that the recent change is "breaking ordering expectations downstream". Whose expectations, and how does that manifest? |
|
It only affects chat/completions endpoint with OpenAI compatible API servers. With the current codex: Test with the following. Run a test server (needs {
cat <<'EOF'
HTTP/1.1 200 OK
Content-Type: text/event-stream
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{"content":"ASSISTANT MESSAGE HERE"},"finish_reason":null}]}
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{"content":null,"tool_calls":[{"index":0,"id":"call_testid","type":"function","function":{"name":"shell","arguments":""}}]},"finish_reason":null}]}
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"command\":[\"bash\",\"-lc\",\"uname\"],\"timeout_ms\":120000,\"workdir\":\"/tmp\"}"}}]},"finish_reason":null}]}
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}
data: [DONE]
EOF
} | nc -l 8888 | sed '/^[^{]/d' | jq '.messages[2:]'
{
cat <<'EOF'
HTTP/1.1 200 OK
Content-Type: text/event-stream
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{"content":"CHECK WHAT I RECEIVED"},"finish_reason":null}]}
data: {"id":"chatcmpl-testid","object":"chat.completion.chunk","created":1763690000,"model":"gpt-test","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
EOF
} | nc -l 8888 | sed '/^[^{]/d' | jq '.messages[2:]'From another terminal connect with codex The our You can see that the This commit moves assistant messages, if there's any, before the |
|
And I just saw a bug report. This PR should fix #7051 |
|
codex --version I'm using [email protected] version via |
|
I believe this issue is model-dependent. When the messages are arranged in the sequence: Some models return an error while others do not. For example, when I used glm-4.6 and sent requests in that order, no error occurred. When I submitted the same request to deepseek-chat, it returned an HTTP 400 response with the following error: But it indeed affect the GPT-series models. Below is the request log I captured from Codex (version: 0.63.0): {
"model": "glm-4.6",
"messages": [
{
"role": "system",
"content": "You are a coding agent running in the Codex CLI, a terminal-based coding assistant. Codex CLI is....."
},
{
"role": "user",
"content": "<environment_context>...</environment_context>"
},
{
"role": "user",
"content": "write foo into bar file and save in the current location"
},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_c64a59b760fa422e8e603a2f",
"type": "function",
"function": {
"name": "exec_command",
"arguments": "{\"cmd\":\"echo \\\"foo\\\" > bar\"}"
}
}
]
},
{
"role": "assistant",
"content": "\nI'll create a file named \"bar\" with the content \"foo\" in the current location.\n"
},
{
"role": "tool",
"tool_call_id": "call_c64a59b760fa422e8e603a2f",
"content": "Chunk ID: 987afe\nWall time: 0.0261 seconds\nProcess exited with code 0\nOriginal token count: 0\nOutput:\n"
}
],
"stream": true,
"tools": [...]
}And the Codex can finish the above task with glm only. Below is my model config: [model_providers.glm_4_6]
name = "glm_4_6"
base_url = "https://open.bigmodel.cn/api/coding/paas/v4"
env_key = "GLM_API_KEY"
wire_api = "chat"
query_params = {}
[model_providers.deepseek-chat]
name = "DeepSeek"
base_url = "https://api.deepseek.com"
env_key = "DEEPSEEK_API_KEY"
wire_api = "chat"
query_params = {} |
|
@etraut-openai any chance this could be merged to fix #7051 ? |
|
Also seeing this error, had to downgrade to |
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Also affected by #7051 at my institution, would love to see this merged in! |
|
Here's a quick update on this issue... The correct fix for this problem is more involved than the proposed fix in this PR. We're working on a separate fix. I'll leave this PR open for now, but we'll likely end up closing it once we merge the other change. |
Codex CLI interleaves function_call_output items with other items during multi-turn conversations (openai/codex#7038). After Responses→IR→Chat conversion this produces tool messages separated from their assistant message, which upstream providers reject. Adds _reorder_tool_messages() to move each tool message immediately after the assistant whose tool_calls it answers. Unmatched tool messages are appended at the end with a warning rather than dropped. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Codex CLI interleaves function_call_output items with other items during multi-turn conversations (openai/codex#7038). After Responses→IR→Chat conversion this produces tool messages separated from their assistant message, which upstream providers reject. Adds _reorder_tool_messages() to move each tool message immediately after the assistant whose tool_calls it answers. Unmatched tool messages are appended at the end with a warning rather than dropped. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Codex CLI interleaves function_call_output items with other items during multi-turn conversations (openai/codex#7038). After Responses→IR→Chat conversion this produces tool messages separated from their assistant message, which upstream providers reject. Adds _reorder_tool_messages() to move each tool message immediately after the assistant whose tool_calls it answers. Unmatched tool messages are appended at the end with a warning rather than dropped. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Codex CLI interleaves function_call_output with other items in Responses API format. After IR → Chat Completions conversion, tool response messages can end up separated from their assistant message, violating the OpenAI Chat API constraint and causing upstream 400s. Add _reorder_tool_messages() post-processing step that groups tool messages back to their corresponding assistant tool_calls. Ref: openai/codex#7038 Co-authored-by: Hong Zhang <[email protected]>
Codex CLI context compaction can remove all tool definitions while keeping tool_choice/tool_config, producing invalid requests rejected by upstream APIs. This is the third symptom of the same problem family: 1. Orphaned tool_calls in messages → fix_orphaned_tool_calls_ir (v0.2.4) 2. Tool messages in wrong position → _reorder_tool_messages (v0.2.6) 3. Orphaned tool_choice/tool_config → strip_orphaned_tool_config (this) Add strip_orphaned_tool_config() in base/tools.py and call it from all 4 converters' request_to_provider(), including Google GenAI which also gains fix_orphaned_tool_calls_ir for consistency. Ref: openai/codex#7038 Closes #87
Codex CLI interleaves function_call_output with other items in Responses API format. After IR → Chat Completions conversion, tool response messages can end up separated from their assistant message, violating the OpenAI Chat API constraint and causing upstream 400s. Add _reorder_tool_messages() post-processing step that groups tool messages back to their corresponding assistant tool_calls. Ref: openai/codex#7038 Co-authored-by: Hong Zhang <[email protected]>
Codex CLI context compaction can remove all tool definitions while keeping tool_choice/tool_config, producing invalid requests rejected by upstream APIs. This is the third symptom of the same problem family: 1. Orphaned tool_calls in messages → fix_orphaned_tool_calls_ir (v0.2.4) 2. Tool messages in wrong position → _reorder_tool_messages (v0.2.6) 3. Orphaned tool_choice/tool_config → strip_orphaned_tool_config (this) Add strip_orphaned_tool_config() in base/tools.py and call it from all 4 converters' request_to_provider(), including Google GenAI which also gains fix_orphaned_tool_calls_ir for consistency. Ref: openai/codex#7038 Closes #87 Co-authored-by: Hong Zhang <[email protected]>
commit cfcc87a changed how response items are batched and recorded, which caused tool call objects to be recorded before the assistant message content in some turns.
This could result in a { role: "assistant", content: null, tool_calls: [...] } message being sent before the actual assistant message with text content, breaking ordering expectations downstream.
This change reorders the items collected for a single turn so that the last non-empty assistant message is placed immediately before the first tool call object when both occur in the same turn, without perturbing prior history.
A new unit test ensures that recorded items now appear in the expected order: assistant text, then tool call object, then the corresponding tool output input item.