Skip to content

Conversation

@test137E29B
Copy link

@test137E29B test137E29B commented Dec 3, 2025

Background

When using the OpenAI Responses API with store: true and reasoning models (e.g., o3, o4-mini), the API returns errors when item_reference items are unpaired:

  • Item 'rs_xxx' of type 'reasoning' was provided without its required following item
  • Item 'fc_xxx' of type 'function_call' was provided without its required 'reasoning' item

This occurs in multi-turn conversations when:

  • A reasoning item is followed by a tool call that lacks an itemId (i.e., the tool call wasn't stored by the API)
  • The tool call emits a full function_call object instead of an item_reference
  • The reasoning item_reference is now "unpaired" because OpenAI expects both reasoning and its following item to either both use item_reference or both use full objects

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 itemId from 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 reasoning item_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 the itemId and thus do not include the item_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_reference is correctly skipped and the conversation continues.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Future Work

Port into v6 main branch. #10829

Related Issues

#8379

When using the OpenAI Responses API with store: true and reasoning models,
the API returns an error if a reasoning item_reference is sent without its
required paired item (text or function_call with item_reference).

This fix adds look-ahead logic when processing reasoning parts to check if
the following part (after skipping consecutive reasoning parts) will also
emit an item_reference. A reasoning item_reference is only included when:

1. The following part is a text or tool-call
2. The following part has a provider-assigned itemId (meaning it will emit
   an item_reference, not a full message/function_call object)

This prevents errors like:
- "Item 'rs_xxx' of type 'reasoning' was provided without its required
  following item"
- "Item 'fc_xxx' of type 'function_call' was provided without its required
  'reasoning' item"
@test137E29B
Copy link
Author

tagging @aayush-kapoor for visibility since I note you are assigned to the issue that this resolves 😄

@aayush-kapoor
Copy link
Contributor

cc @lgrammel

@lgrammel
Copy link
Collaborator

lgrammel commented Dec 4, 2025

can you provide an example under examples/ai-core with a tool that throws an error which leads to this problem?

i want to make sure we fix the root cause and i am not convinced yet that this isn't patching the symptom

@lgrammel
Copy link
Collaborator

lgrammel commented Dec 4, 2025

also please do all work in a pr against main; we have tooling for backports

@test137E29B
Copy link
Author

@lgrammel This one got a lot harder to reproduce, however, eventually I managed to track down the root cause of the issue itself - which was resolved a couple of days ago in #10733

So we're good to close this, and related issues, as resolved!

The chain was:
InvalidToolInputError -> tool-call with no providerMetadata -> no providerMetadata.openai.itemId (even though one existed on the response from openai).

@test137E29B test137E29B closed this Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants