codex - ✅(Solved) Fix Responses API replay can fail on orphan reasoning item after interrupted turn [1 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#17161Fetched 2026-04-09 08:01:43
View on GitHub
Comments
2
Participants
2
Timeline
5
Reactions
0
Timeline (top)
commented ×2labeled ×2closed ×1

Codex can resend an orphan reasoning item when replaying history after an interrupted turn, which causes the Responses API request to fail instead of self-healing.

The concrete API error is:

{
  "error": {
    "message": "Item rs_... of type reasoning was provided without its required following item.",
    "type": "invalid_request_error",
    "param": "input"
  }
}

Error Message

The concrete API error is: "error": {

  • retries once for the exact missing-following-item error on both HTTP and websocket Responses transports

Root Cause

I couldn’t upstream the PR directly because this repository limits PR creation to collaborators, but the fork commit above should be enough to review or cherry-pick.

Fix Action

Fix / Workaround

I implemented a working patch on my fork here:

The patch does two things:

Validation on the patch

PR fix notes

PR #1: fix: repair orphan function_call_output to prevent Responses API 400

Description (problem / solution / changelog)

Summary

Fixes openai/codex#17630 — the recurring 400 invalid_request_error: "No tool call found for function call output with call_id …" from the Responses API. This is the mirror image of openai/codex#16255 (call without matching output) and the same class as openai/codex#17161 (orphan reasoning).

Two underlying bugs combine to produce the error:

  1. Cleanup-skipped early returns in try_run_sampling_request. Two ?-style early returns (stream-item decode error, handle_output_item_done error) bypass the shared trailer that calls drain_in_flight. Any in-flight tool future drops its FunctionCallOutput / CustomToolCallOutput, leaving the call recorded with no output.
  2. Stale retry prompt in run_sampling_request. build_prompt(...) runs once before the retry loop, so retries reuse a frozen Prompt.input even after history has been amended by drain.

Together with CodexErr::InvalidRequest being non-retryable, the turn fails hard with no localized self-heal.

Changes

  • codex-rs/core/src/codex.rs
    • try_run_sampling_request: convert mid-loop ? returns to break Err(err) so the trailer always reaches drain_in_flight.
    • run_sampling_request: rebuild the prompt from clone_history().for_prompt(...) inside the retry loop so each attempt picks up newly-drained items.
    • Add classify_replay_repair() for "No tool call found for function call output with call_id …" and "Item … of type reasoning was provided without its required following item.". On match, repair in-memory history once and retry — short-circuits before the existing non-retryable check.
  • codex-rs/core/src/context_manager/normalize.rs: drop_function_call_output_by_id covering both FunctionCallOutput and CustomToolCallOutput.
  • codex-rs/core/src/context_manager/history.rs + state/session.rs: expose the helper on ContextManager / session, bumping history_version so the websocket incremental-delta baseline (client.rs:get_incremental_items) is invalidated after repair.
  • Tests: new tests/suite/orphan_function_call_output.rs (registered in mod.rs); extended tests/suite/stream_no_completed.rs with the function-call/output early-close case; expanded context_manager/history_tests.rs.

Notes / scope

  • One-shot repair per run_sampling_request (capped to avoid loops); falls through to the original error if the server keeps rejecting.
  • No protocol error enum changes — InvalidRequest stays non-retryable; the repair path intercepts before the retryability check.
  • Out of scope: commentary-only retry accounting (openai/codex#16255 point 3) and partial-response WebSocket resume (openai/codex#13949).

Test plan

  • cargo fmt
  • cargo clippy -p codex-core -- -D warnings
  • cargo test -p codex-core stream_no_completed -- --nocapture
  • cargo test -p codex-core orphan_function_call_output -- --nocapture
  • cargo test -p codex-core client_websockets -- --nocapture
  • cargo test -p codex-core context_manager::history_tests -- --nocapture
  • Manual repro: mock Responses endpoint that returns the exact invalid_request_error body from openai/codex#17630; verify Codex repairs and the turn ultimately succeeds.

🤖 Generated with Claude Code

Changed files

  • codex-rs/core/src/codex.rs (modified, +99/-0)
  • codex-rs/core/src/context_manager/history.rs (modified, +20/-0)
  • codex-rs/core/src/context_manager/history_tests.rs (modified, +455/-0)
  • codex-rs/core/src/context_manager/normalize.rs (modified, +29/-0)
  • codex-rs/core/src/state/session.rs (modified, +8/-0)
  • codex-rs/core/tests/suite/mod.rs (modified, +1/-0)
  • codex-rs/core/tests/suite/orphan_function_call_output.rs (added, +410/-0)
  • codex-rs/core/tests/suite/stream_no_completed.rs (modified, +125/-0)

Code Example

{
  "error": {
    "message": "Item rs_... of type reasoning was provided without its required following item.",
    "type": "invalid_request_error",
    "param": "input"
  }
}
RAW_BUFFERClick to expand / collapse

Summary

Codex can resend an orphan reasoning item when replaying history after an interrupted turn, which causes the Responses API request to fail instead of self-healing.

The concrete API error is:

{
  "error": {
    "message": "Item rs_... of type reasoning was provided without its required following item.",
    "type": "invalid_request_error",
    "param": "input"
  }
}

Problem shape

A bad persisted/replayed sequence looks like:

  1. trailing reasoning item
  2. synthetic <turn_aborted> user marker
  3. next real user message

In that shape, the replayed request contains a reasoning item without the model-generated item that originally followed it, and the Responses API rejects the request.

Expected behavior

Codex should detect this narrow invalid-request case, repair the replayed input/history by dropping only the orphan reasoning item, and retry once.

Proposed fix

I implemented a working patch on my fork here:

The patch does two things:

  • normalizes replayed history so incomplete reasoning items are removed while preserving <turn_aborted>
  • retries once for the exact missing-following-item error on both HTTP and websocket Responses transports

Validation on the patch

  • just fmt
  • just fix -p codex-core
  • cargo test -p codex-core missing_reasoning -- --nocapture

I couldn’t upstream the PR directly because this repository limits PR creation to collaborators, but the fork commit above should be enough to review or cherry-pick.

extent analysis

TL;DR

Apply the proposed patch from the provided fork to fix the issue with orphan reasoning items causing the Responses API request to fail.

Guidance

  • Review the patch at the provided commit f3b9e8344c58b50bb0637f7ecdfcb9230cb27e82 to understand the changes made to normalize replayed history and retry the request.
  • Verify the fix by running the tests with cargo test -p codex-core missing_reasoning -- --nocapture to ensure the issue is resolved.
  • Cherry-pick the commit into the main repository to apply the fix, as direct PR creation is limited to collaborators.
  • Test the fix with both HTTP and websocket Responses transports to ensure it works as expected in all scenarios.

Example

No code snippet is provided as the issue already includes a proposed patch and instructions on how to apply and test it.

Notes

The provided patch seems to address the specific issue of orphan reasoning items, but it's essential to review and test the changes thoroughly to ensure they don't introduce any new issues or side effects.

Recommendation

Apply the workaround by cherry-picking the proposed patch, as it directly addresses the issue and includes retry logic for the specific error case, making it a targeted and effective solution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Codex should detect this narrow invalid-request case, repair the replayed input/history by dropping only the orphan reasoning item, and retry once.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING