Skip to content

fix(ai-openai): deduplicate response.output items to prevent invalid JSON concatenation#6178

Merged
IMax153 merged 2 commits intoEffect-TS:mainfrom
aayushbaluni:fix/6146-deduplicate-response-output
Apr 17, 2026
Merged

fix(ai-openai): deduplicate response.output items to prevent invalid JSON concatenation#6178
IMax153 merged 2 commits intoEffect-TS:mainfrom
aayushbaluni:fix/6146-deduplicate-response-output

Conversation

@aayushbaluni
Copy link
Copy Markdown
Contributor

Summary

generateObject intermittently fails with a parseJson error when OpenAI's Responses API returns duplicate OutputMessage items in response.output.

Root Cause

OpenAI's Responses API has a known bug where it intermittently returns duplicate OutputMessage items with identical IDs and content in response.output. The current code iterates over all output items and pushes every output_text as a text part. When these duplicate parts are later concatenated (via text.join("")), the result is invalid JSON:

{"name":"Acme Corp","amount":123.45}{"name":"Acme Corp","amount":123.45}

This causes schema parsing to fail with Unexpected non-whitespace character after JSON.

Fix

Track seen output item IDs in a Set and skip duplicates before processing. This is a defensive measure against the documented OpenAI API behavior:

Changes

  • OpenAiLanguageModel.ts: Add seenOutputIds Set to deduplicate response.output items by ID before processing

Fixes #6146

Made with Cursor

…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
@aayushbaluni aayushbaluni requested a review from IMax153 as a code owner April 15, 2026 10:01
@github-project-automation github-project-automation bot moved this to Discussion Ongoing in PR Backlog Apr 15, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 15, 2026

🦋 Changeset detected

Latest commit: ea6159c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@effect/ai-openai Patch

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

@IMax153 IMax153 merged commit 47f0439 into Effect-TS:main Apr 17, 2026
11 checks passed
@github-project-automation github-project-automation bot moved this from Discussion Ongoing to Done in PR Backlog Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

@effect/ai-openai: generateObject fails when OpenAI Responses API returns duplicate output_text parts

2 participants