-
Notifications
You must be signed in to change notification settings - Fork 388
fix: resolve #425 duplicate item error when using conversationId with tools #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
When using the OpenAI Agents SDK with conversationId and tools, users encounter a 'Duplicate item found with id rs_xxx' error. This happens because the OpenAI Responses API automatically retrieves conversation history when conversationId is provided, while the SDK also sends the full conversation history, causing duplicate items. This fix modifies the getInputItems function to: - Accept an optional conversationId parameter - Filter input to only include the current turn when conversationId is present - Maintain full backward compatibility The solution identifies the current turn by finding the last user message and only includes items from that point onwards, preventing duplicates while preserving conversation context. Resolves the BadRequestError and enables proper use of conversationId with tool-enabled agents.
🦋 Changeset detectedLatest commit: 4f00b05 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Thanks for sending this. Indeed, this could be a solution for the issue. I will look into this shortly. |
@codex review this PR; especially check if finding lastUserMessageIndex is sufficient for this bug fix |
There was a problem hiding this 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 suggestions.
Reply with @codex fix comments
to fix any unresolved comments.
About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback".
// When using conversationId, the OpenAI Responses API automatically retrieves | ||
// the conversation history. To avoid duplicate items with the same IDs, | ||
// we need to filter out items that would already be present in the conversation. | ||
// We keep only the items from the current turn (typically the last few items). | ||
let filteredInput = input; | ||
if (conversationId) { | ||
// Find the last user message to identify the start of the current turn | ||
let lastUserMessageIndex = -1; | ||
for (let i = input.length - 1; i >= 0; i--) { | ||
const item = input[i]; | ||
if (isMessageItem(item) && item.role === 'user') { | ||
lastUserMessageIndex = i; | ||
break; | ||
} | ||
} | ||
|
||
// If we found a user message, only include items from that point onwards | ||
// This represents the current turn's conversation | ||
if (lastUserMessageIndex >= 0) { | ||
filteredInput = input.slice(lastUserMessageIndex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Filtering from last user message still resends duplicated tool items
The new filtering logic slices the input array starting at the last user
message, but in a tool-driven turn the items that cause the duplicate-ID error are typically the assistant’s tool call and other model-generated items that follow that user message. During the follow‑up request that posts tool results, getTurnInput
still includes the original user message and the model’s tool call (with the same IDs as already stored in the conversation). Because those items fall after the last user message, this function keeps them and they are resent alongside the tool output, so the API still receives duplicates and fails. To avoid the error you need to send only the items generated since the last request (e.g., by tracking the last response ID or filtering by new item IDs), not everything after the last user message.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a valid point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codex can you suggest alternative approaches here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codex can you suggest alternative approaches here? (path=packages/agents-openai/src/openaiResponsesModel.ts line=459 side=RIGHT)
For now, I can only help with PRs you've created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @seratch would you review the latest commit?
• Added heuristics in getInputItems to detect items already persisted in the conversation
when conversationId is set, slicing the input to send only newly generated turn data and
avoid duplicate IDs.
• Extended unit coverage to verify that stored items are skipped while fresh inputs continue
to flow when no persisted metadata exists.
@seratch is there something we can do in userland before the fix in library? |
Having the same issue ( |
is there any update ? |
Issue Description
Resolves #425
When using the OpenAI Agents SDK with both
conversationId
and tools, users encounter aBadRequestError
:This error occurs specifically when:
conversationId
parameterRoot Cause Analysis
The issue stems from a conflict between two behaviors:
conversationId
is provided, the API automatically retrieves and includes the conversation historyinput
parameterSolution
This PR modifies the
getInputItems
function inpackages/agents-openai/src/openaiResponsesModel.ts
to intelligently filter input when aconversationId
is present:Key Changes
How It Works
The solution identifies the "current turn" by locating the last user message in the input array. When a
conversationId
is provided:This approach ensures that:
conversationId
Testing
This fix resolves the error in scenarios like:
Benefits
conversationId
with toolsImpact
This change enables developers to use the full power of the OpenAI Agents SDK with persistent conversations and tools without encountering the duplicate ID error, unlocking more sophisticated conversational AI applications.
Files Changed
packages/agents-openai/src/openaiResponsesModel.ts
: ModifiedgetInputItems
function to handle conversationId filteringRelated Issues: This addresses similar issues reported by users when combining conversationId with tool-enabled agents.