fix: handle reasoning items in CodeInterpreter response processing#3590
fix: handle reasoning items in CodeInterpreter response processing#3590Oxygen56 wants to merge 1 commit into
Conversation
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
There was a problem hiding this comment.
💡 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".
| if should_strip: | ||
| result[i] = dict(item) | ||
| result[i].pop("id", None) |
There was a problem hiding this comment.
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 👍 / 👎.
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:
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:Reasoning items preceding regular messages or function_calls are left untouched (IDs preserved).
The fix is applied in
_build_response_create_kwargswhich covers all code paths (streaming, non-streaming, regular, and websocket models).Fixes #985