Structured output: gpt-6-luna (reasoning none) adds text after a valid object — allow recovering it, not failing JSON.parse
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- typescript
- Domain
- api, backend-api-design
Research direction
Start with adapters/text.js and adapters/responses-text.js, tracing structuredOutputStream and structuredOutput parsing, then read harvestCombinedStructuredOutput in activities/chat/index.js. Compare streaming and non-streaming error paths and the existing schema validation behavior. Done means the selected recovery or raw-error approach works consistently across the named paths without losing valid structured output.
Written by the indexing model from the issue text.
Description
Versions: @tanstack/ai 0.58.0, @tanstack/ai-openrouter 0.19.17
Model and frequency
- Model:
openai/gpt-6-lunathrough OpenRouter (@tanstack/ai-openrouter0.19.17,@tanstack/ai0.58.0), withoutputSchemaset (strictjson_schema) and reasoning effort"none". - Frequency: on a long (~65k-token) system prompt with a four-field schema, about 1–2% of calls stream one complete, schema-valid object and then keep writing. Most often the extra text is a second, near-identical copy of the same object. Sometimes it is stray text, such as
*****/or a sentence like "Need valid JSON…". In our logs the second object repeated the first object's field values in almost every case, so the first object is the model's real answer. - Reasoning effort matters: the same model with a little reasoning (effort
"minimal") produced 0 such replies over a full run of ~1,400 calls. - Not a provider issue: restricting OpenRouter routing to endpoints that support structured outputs (
provider.require_parameters) did not change the rate (12 vs 13 on the same 100 cases), so it is not one provider ignoringresponse_format. - Recoverable: extracting the first balanced top-level object recovered every one of these replies in our evals (0 needed a retry).
What happens
We use outputSchema on the OpenRouter adapter with reasoning turned off (effort "none") on a small OpenAI model. About 1–2% of calls stream a reply that holds a complete JSON object that passes the schema, followed by more text. Usually the extra text is a near-copy of the object. Sometimes it is a stray remark. Example (anonymized):
{"query":"t:creature ...","preciseScore":9}
{"query":"t:creature ..."}
Strict JSON.parse rejects the whole reply, so the run fails with code "parse-error". The first object is the model's real answer, and it is lost.
The same thing happens when string values hold regex-like content. They can include backslashes that JSON does not allow (e.g. \{), so parsing fails even when the reply is a single object.
Where this happens:
@tanstack/ai-openrouteradapters/text.js:structuredOutputStreambuilds up the streamed text, callsJSON.parse(accumulatedContent), and on failure yields aRUN_ERROR(code: "parse-error"). The message holds onlyaccumulatedContent.slice(0, 200). The non-streamingstructuredOutputpath in the same file does the same thing.adapters/responses-text.jsdoes the same, in both the streaming and non-streaming paths.@tanstack/aiactivities/chat/index.js:harvestCombinedStructuredOutputalso callsJSON.parsestrictly and keeps a 200-character preview (code: "structured-output-parse-failed"). Thenormalizehook runs only afterJSON.parsesucceeds, so there is no hook that can fix the text before it is parsed.
Why it's hard to work around
The error carries only a 200-character preview, and normalize runs after parsing. To recover, a consumer has to collect the raw text itself from TEXT_MESSAGE_CONTENT chunks in a middleware onChunk, then parse it again inside a catch around chat(). This works: it recovered all 21 of these failures in our eval suite. But it is fragile, depends on the adapter's chunk shape, and has to be duplicated for each adapter path.
Proposal
Any one of these would solve it:
- An option on
chat()/outputSchemathat runs beforeJSON.parse, such asparseStructuredOutput?: (raw: string) => unknownorrepair?: (raw: string) => string. - Built-in lenient extraction: before giving up, take the first balanced top-level object and parse that.
- At the very least, attach the full raw text to the error (e.g.
error.raw) instead of a 200-character preview, so consumers can recover without capturing chunks.
For reference, our repair is about 30 lines:
- Take the first balanced
{...}, tracking strings and escapes. - If that still fails to parse, double every backslash that is not followed by a valid JSON escape character.
- Validate the result against the schema.
Glad to share the code if it helps.
- Dominant language
- TypeScript
- Stars
- 3.1k
- Forks
- 340
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 173
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TanStack/ai
-
Does a hosted provider package have to carry a key?Possibly taken @AlemTuzlak claimed this 1 day ago. Openwaiting-on: author
TanStack/ai#1506 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
-
Client tools never execute against spec-compliant AG-UI servers (plain `success` RUN_FINISHED)Possibly taken @jherr claimed this 3 days ago. Openwaiting-on: maintainer
TanStack/ai#1464 · 1 reaction · 1 assignee ·
Maintainers usually reply within 1 day
-
Add ai to awesome-ai-plugins?Possibly taken @tombeckenham claimed this 9 days ago. Openwaiting-on: maintainer
TanStack/ai#1382 · 1 assignee ·
Maintainers usually reply within 1 day
-
AG-UI activity support is non-functional: events are never processed, and `role: 'activity'` messages are destroyedPossibly taken @jherr claimed this 9 days ago. Openhas-pr waiting-on: maintainer
TanStack/ai#1286 · 1 assignee ·
Maintainers usually reply within 1 day
-
ai-sandbox-daytona: SDK functions that the adapter does not use — log stream, session stdin, PTY kill, native fs/git, network policyMay be free again @jherr claimed this 37 days ago, and no pull request is open. Openneeds-repro waiting-on: maintainer
TanStack/ai#1085 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
Similar issues
-
module-request
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
appandflow/stim#1604 · 1 comment ·
Maintainers usually reply within 1 day
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 92/100
lingdojo/kana-dojo#31060 · 1 comment · 5 reactions ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
paperclipai/paperclip#14173 ·
Maintainers usually reply within 1 day
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day