fix(ai-openai): deduplicate response.output items to prevent invalid JSON concatenation#6178
Merged
IMax153 merged 2 commits intoEffect-TS:mainfrom Apr 17, 2026
Conversation
…JSON concatenation
OpenAI's Responses API intermittently returns duplicate OutputMessage
items in response.output with identical IDs and content. When this
happens, all text parts are pushed and later concatenated, producing
invalid JSON like {"a":1}{"a":1} that fails schema parsing.
Skip duplicate output items by tracking seen IDs before processing.
This handles the known OpenAI API bug documented at:
https://community.openai.com/t/1306731
Fixes Effect-TS#6146
Made-with: Cursor
🦋 Changeset detectedLatest commit: ea6159c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
generateObjectintermittently fails with aparseJsonerror when OpenAI's Responses API returns duplicateOutputMessageitems inresponse.output.Root Cause
OpenAI's Responses API has a known bug where it intermittently returns duplicate
OutputMessageitems with identical IDs and content inresponse.output. The current code iterates over all output items and pushes everyoutput_textas a text part. When these duplicate parts are later concatenated (viatext.join("")), the result is invalid JSON:This causes schema parsing to fail with
Unexpected non-whitespace character after JSON.Fix
Track seen output item IDs in a
Setand skip duplicates before processing. This is a defensive measure against the documented OpenAI API behavior:Changes
OpenAiLanguageModel.ts: AddseenOutputIdsSet to deduplicateresponse.outputitems by ID before processingFixes #6146
Made with Cursor