What version of Effect is running?
effect: 3.21.0 @effect/ai: 0.35.0 @effect/ai-openai: 0.39.0
What steps can reproduce the bug?
Call LanguageModel.generateObject() with any structured schema via the OpenAI Responses API (/v1/responses). The bug is intermittent — it depends on OpenAI returning duplicate output messages.
const MySchema = Schema.Struct({
name: Schema.String,
amount: Schema.Number,
})
const result = yield* LanguageModel.generateObject({
prompt: [{ role: "user", content: "Extract: Invoice from Acme Corp for €123.45" }],
schema: MySchema,
})
This fails intermittently with a parseJson error when OpenAI's Responses API returns multiple identical OutputMessage items in the response.output array.
Affected models: observed with gpt-5.4-mini, also widely reported with gpt-4.1-mini, gpt-4o, and gpt-4.1 on the OpenAI community forums.
What is the expected behavior?
generateObject returns a parsed object matching the schema.
What do you see instead?
AiError: Generated object failed to conform to provided schema
└─ Encoded side transformation failure
└─ parseJson
└─ Transformation process failure
└─ Unexpected non-whitespace character after JSON at position [N]
Using a custom Telemetry.CurrentSpanTransformer, we confirmed that the response contains duplicate text parts:
[response-metadata] { id: "resp_...", model: "gpt-5.4-mini-..." }
[text] {"name":"Acme Corp","amount":123.45}
[text] {"name":"Acme Corp","amount":123.45} ← identical duplicate
[finish] { reason: "stop" }
resolveStructuredOutput in LanguageModel.ts (~L1059-1077) concatenates all text parts via text.join(""), producing:
{"name":"Acme Corp","amount":123.45}{"name":"Acme Corp","amount":123.45}
This is invalid JSON — two objects concatenated without a separator.
Additional information
This is a confirmed OpenAI API bug where the Responses API (/v1/responses) intermittently returns multiple OutputMessage items in response.output, each containing identical output_text content. OpenAI Support has acknowledged it.
References:
Where the issue is in @effect/ai-openai:
OpenAiLanguageModel.ts iterates over all items in response.output and pushes every output_text as a text part — no deduplication.
The OpenAI response object includes an output_text convenience property that returns just the text as a single string. The API docs recommend: "Rather than accessing the first item in the output array and assuming it's an assistant message...you might consider using the output_text property."
Suggested fixes (any of these would resolve it):
- (provider-level, recommended) Use
response.output_text in @effect/ai-openai when available for structured output responses
- (provider-level) Deduplicate or take only the first
message from response.output
- (consumer-level) In
resolveStructuredOutput, use only the first text part instead of concatenating all
What version of Effect is running?
effect: 3.21.0 @effect/ai: 0.35.0 @effect/ai-openai: 0.39.0
What steps can reproduce the bug?
Call
LanguageModel.generateObject()with any structured schema via the OpenAI Responses API (/v1/responses). The bug is intermittent — it depends on OpenAI returning duplicate output messages.This fails intermittently with a
parseJsonerror when OpenAI's Responses API returns multiple identicalOutputMessageitems in theresponse.outputarray.Affected models: observed with
gpt-5.4-mini, also widely reported withgpt-4.1-mini,gpt-4o, andgpt-4.1on the OpenAI community forums.What is the expected behavior?
generateObjectreturns a parsed object matching the schema.What do you see instead?
Using a custom
Telemetry.CurrentSpanTransformer, we confirmed that the response contains duplicate text parts:resolveStructuredOutputinLanguageModel.ts(~L1059-1077) concatenates all text parts viatext.join(""), producing:This is invalid JSON — two objects concatenated without a separator.
Additional information
This is a confirmed OpenAI API bug where the Responses API (
/v1/responses) intermittently returns multipleOutputMessageitems inresponse.output, each containing identicaloutput_textcontent. OpenAI Support has acknowledged it.References:
Where the issue is in
@effect/ai-openai:OpenAiLanguageModel.tsiterates over all items inresponse.outputand pushes everyoutput_textas a text part — no deduplication.The OpenAI response object includes an
output_textconvenience property that returns just the text as a single string. The API docs recommend: "Rather than accessing the first item in the output array and assuming it's an assistant message...you might consider using the output_text property."Suggested fixes (any of these would resolve it):
response.output_textin@effect/ai-openaiwhen available for structured output responsesmessagefromresponse.outputresolveStructuredOutput, use only the first text part instead of concatenating all