Repository navigation
fix(openai-base): finish a Responses max_output_tokens cutoff with finishReason length instead of RUN_ERROR #1929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pcbeingused333
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
pcbeingused333:fix/responses-incomplete-finish-reason
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/openai-base': patch | ||
| --- | ||
|
|
||
| A Responses stream that ends with `response.incomplete` because of `max_output_tokens` or `content_filter` now finishes with `RUN_FINISHED` carrying the response's usage and `finishReason` `'length'` or `'content_filter'`, like the Chat Completions adapter, instead of `RUN_ERROR`. `response.failed` and an incomplete response with another or no reason still end in `RUN_ERROR`. |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| import { chat } from '@tanstack/ai' | ||
| import { createOpenaiChat } from '@tanstack/ai-openai' | ||
|
|
||
| const DUMMY_KEY = 'sk-e2e-test-dummy-key' | ||
|
|
||
| /** | ||
| * Streams one OpenAI Responses chat against the stub server the spec passes in | ||
| * `baseURL`. aimock reports a capped response as `response.completed`, so the | ||
| * spec serves a real `response.incomplete` event itself (issue #1916). Returns | ||
| * the terminal chunk `chat()` yields and what the middleware hooks saw. | ||
| */ | ||
| export const Route = createFileRoute('/api/responses-incomplete')({ | ||
| server: { | ||
| handlers: { | ||
| POST: async ({ request }) => { | ||
| const { baseURL } = (await request.json()) as { baseURL: string } | ||
| const adapter = createOpenaiChat('gpt-4.1-nano', DUMMY_KEY, { baseURL }) | ||
|
|
||
| const hooks: Array<string> = [] | ||
| const usage: Array<unknown> = [] | ||
| let terminal: Record<string, unknown> | undefined | ||
| try { | ||
| for await (const chunk of chat({ | ||
| threadId: 'responses-incomplete', | ||
| adapter, | ||
| modelOptions: { max_output_tokens: 16 }, | ||
| messages: [{ role: 'user', content: 'Count from 1 to 300.' }], | ||
| middleware: [ | ||
| { | ||
| name: 'probe', | ||
| onUsage: (_ctx, info) => { | ||
| usage.push(info) | ||
| }, | ||
| onFinish: (_ctx, info) => { | ||
| hooks.push(`onFinish:${info.finishReason}`) | ||
| }, | ||
| onError: () => { | ||
| hooks.push('onError') | ||
| }, | ||
| }, | ||
| ], | ||
| })) { | ||
| if (chunk.type === 'RUN_FINISHED' || chunk.type === 'RUN_ERROR') { | ||
| terminal = { type: chunk.type, usage: chunk.usage } | ||
| } | ||
| } | ||
| } catch (error) { | ||
| return Response.json({ | ||
| ok: false, | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }) | ||
| } | ||
|
|
||
| return Response.json({ ok: true, terminal, hooks, usage }) | ||
| }, | ||
| }, | ||
| }, | ||
| }) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recover terminal-only text before finishing an incomplete response.
If a capped response has text in
response.outputbut sends no text deltas, this branch emitsRUN_FINISHEDwithout the answer. Theresponse.completedbranch already recovers text from that same field. Apply that fallback before closing the text message and emittingRUN_FINISHED. Add a no-delta incomplete-response case to the tests. (raw.githubusercontent.com)