Skip to content

fix: handle reasoning items in CodeInterpreter response processing#3590

Open
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:fix/code-interpreter-reasoning-985
Open

fix: handle reasoning items in CodeInterpreter response processing#3590
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:fix/code-interpreter-reasoning-985

Conversation

@Oxygen56
Copy link
Copy Markdown

@Oxygen56 Oxygen56 commented Jun 5, 2026

Fixes CodeInterpreter crash with reasoning models (o3/o4-mini).

Problem

When using reasoning models (o3, o4-mini) with the CodeInterpreter tool, the SDK fails with:

Item 'rs_ABCD' of type 'reasoning' was provided without its required following item.

This happens because reasoning items emitted by the model are replayed as input (e.g. during agent handoffs), but their IDs reference following items that may not be recognized as valid reasoning followers by the API.

Root Cause

Models like o3/o4-mini emit reasoning items that precede hosted tool calls (code_interpreter, web_search, file_search, etc.). When these items are sent back as input, the API may reject reasoning items whose IDs point to a hosted tool call that is not treated as a valid "following item." The issue is particularly triggered during handoffs between agents.

Fix

Added _sanitize_reasoning_item_followers() which strips IDs from reasoning items that:

  1. Precede hosted tool calls (code_interpreter_call, web_search_call, file_search_call, image_generation_call, computer_call)
  2. Are the last item in the input with no follower at all

Reasoning items preceding regular messages or function_calls are left untouched (IDs preserved).

The fix is applied in _build_response_create_kwargs which covers all code paths (streaming, non-streaming, regular, and websocket models).

Fixes #985

Models like o3/o4-mini emit reasoning items that precede hosted tool
calls such as code_interpreter_call. When these items are replayed
as input (e.g. during agent handoffs), the API may reject reasoning
items whose IDs reference a following item that is not recognized
as a valid reasoning follower.

Strip the ID from reasoning items that:
- Precede hosted tool calls (code_interpreter, web_search, file_search,
  image_generation, computer)
- Are the last item in the input with no follower at all

This prevents 400 errors like:
  Item 'rs_...' of type 'reasoning' was provided without its required
  following item.

Fixes openai#985
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bd115b2b7e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +978 to +980
if should_strip:
result[i] = dict(item)
result[i].pop("id", None)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor explicit reasoning ID preservation

When callers set reasoning_item_id_policy="preserve" or pass a handcrafted Responses input, this model-layer sanitizer still removes the id for reasoning items followed by hosted tool calls or with no follower. That bypasses the existing public opt-out and silently changes the payload after hooks/input filters have seen it, so stateful/stateless replay code that intentionally preserves server reasoning IDs has no way to keep the documented behavior; gate this stripping on the existing policy or another explicit mitigation path.

Useful? React with 👍 / 👎.

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.

Error "Item ‘rs_ABCD’ of type ‘reasoning’ was provided without its required..." when using CodeInterpreter

1 participant