fix(openai): v5 - skip unpaired reasoning item_reference in Responses API #10827
+214
−21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
When using the OpenAI Responses API with
store: trueand reasoning models (e.g., o3, o4-mini), the API returns errors whenitem_referenceitems are unpaired:Item 'rs_xxx' of type 'reasoning' was provided without its required following itemItem 'fc_xxx' of type 'function_call' was provided without its required 'reasoning' itemThis occurs in multi-turn conversations when:
The typical scenario is that the agent reasons, calls a tool, the tool returns an error (e.g., validation failure), and on retry the SDK rebuilds the conversation context. The reasoning has an
itemIdfrom the previous turn, but the failed tool call does not, causing the mismatch.Summary
Added look-ahead logic when processing reasoning parts with
store: true. Before including a reasoningitem_reference, the code now scans past consecutive reasoning parts to find the first non-reasoning part. The reference is only included if that part has a valid itemId that it will then include in it's iteration. Failed tool calls do not have theitemIdand thus do not include theitem_reference, causing the issue.Manual Verification
Tested with a reasoning model that calls a custom tool with invalid input. Previously this caused the API error; now the reasoning
item_referenceis correctly skipped and the conversation continues.Checklist
pnpm changesetin the project root)Future Work
Port into v6 main branch. #10829
Related Issues
#8379