Skip to content
Source
Sign inSign up
TanStack/ai

fix(openai-base, ai-openrouter): report finish_reason=length as truncation in structuredOutput #1427

Mergedchangbaebang(GitHub profile, opens in a new tab)merged 7 commits intomain fromrefs/pull/1427/headmerged by AlemTuzlak View on GitHub

Showing 11 changed files with 54 additions and 1 deletion (loading 7).

x
0 of 11 viewed, 11 left
7 additions and 0 deletions.changeset/structured-output-length-finish.mdAdded
View file
@@ -0,0 +1,7 @@
+---
+'@tanstack/ai': patch
+'@tanstack/openai-base': patch
+'@tanstack/ai-openrouter': patch
+---
+​
+Structured output now reports a response that was cut off at the output cap (`finish_reason: "length"`) as a truncation error instead of a JSON parse error or a "no content" / "missing structured result" error. This covers `chat({ outputSchema })` in native combined mode (error code `max_tokens`), `structuredOutputStream()` in `openai-base` and `ai-openrouter` (`RUN_ERROR` with code `max_tokens`), and their non-stream `structuredOutput()`. A truncated document used to read as a schema failure; the error now says the token limit was reached.
10 additions and 1 deletionpackages/ai/src/activities/chat/index.tsModified
View file
@@ -3930,7 +3930,16 @@
const yieldChunks = this.finalStructuredOutput.yieldChunks
const source = this.finalStructuredOutput.source ?? 'text'
- if (source === 'event') {
+ // A final turn cut off at the output cap holds truncated JSON, or none
+ // for a reasoning model that spent the budget. Report the token limit
+ // instead of a parse or missing-result error (#1426).
+ if (this.lastFinishReason === 'length') {
+ this.finalizationError = {
+ message:
+ 'The response was cut off because the maximum token limit was reached (finish_reason=length); raise the output token limit.',
+ code: 'max_tokens',
+ }
+ } else if (source === 'event') {