[v6.0] fix(ai): omit empty assistant message for data-only blocks in convertToModelMessages#16793
Merged
Merged
Conversation
…ToModelMessages (#15040) ## Background `convertToModelMessages` was emitting `{ role: 'assistant', content: [] }` when a block contained only unknown data parts (i.e. a data part appears before `step-start` with no `convertDataPart` provided, or `convertDataPart` returns `null`). LLMs reject empty assistant messages, causing downstream failures. ## Summary - Guard `content.length > 0` before pushing the assistant message in `processBlock()` in `convert-to-model-messages.ts` - Add regression test for the data-part-before-step-start scenario ## Manual Verification Verified that this can happen through the public UI stream helpers with `streamText`, not only from a hand-written message fixture. The real route pattern from `content/docs/04-ai-sdk-ui/20-streaming-data.mdx` writes a persistent data part before merging the model stream: ```ts writer.write({ type: 'data-weather', id: 'weather-1', data: { city: 'San Francisco', status: 'loading' }, }); const result = streamText({ model, prompt: 'What is the weather in San Francisco?', }); writer.merge(result.toUIMessageStream()); ``` That ordering is valid because `createUIMessageStream` enqueues `writer.write()` immediately, while `writer.merge()` forwards chunks from the merged stream later. The merged `streamText` UI stream is what emits `start-step`. I reproduced the route shape end-to-end with `streamText` and `MockLanguageModelV4`: ```ts function createWeatherModel() { return new MockLanguageModelV4({ doStream: async () => ({ stream: convertArrayToReadableStream([ { type: 'stream-start', warnings: [] }, { type: 'response-metadata', id: 'response-1', modelId: 'mock-model-id', timestamp: new Date(0), }, { type: 'text-start', id: 'text-1' }, { type: 'text-delta', id: 'text-1', delta: 'It is sunny.' }, { type: 'text-end', id: 'text-1' }, { type: 'finish', finishReason: { unified: 'stop', raw: 'stop' }, usage: { inputTokens: { total: 3, noCache: 3, cacheRead: undefined, cacheWrite: undefined, }, outputTokens: { total: 4, text: 4, reasoning: undefined, }, }, }, ]), }), }); } ``` The test route writes the data part first, then merges `streamText(...).toUIMessageStream()`: ```ts const chunks = await convertReadableStreamToArray( createUIMessageStream<WeatherUIMessage>({ generateId: () => 'msg-123', onEnd({ responseMessage }) { persistedMessage = responseMessage; }, execute({ writer }) { writer.write({ type: 'data-weather', id: 'weather-1', data: { city: 'San Francisco', status: 'loading' }, }); const result = streamText({ model: createWeatherModel(), prompt: 'What is the weather in San Francisco?', }); writer.merge(result.toUIMessageStream()); }, }), ); ``` The recorded UI chunk order was: ```ts [ 'data-weather', 'start', 'start-step', 'text-start', 'text-delta', 'text-end', 'finish-step', 'finish', ] ``` The persisted assistant message had `data-weather` before `step-start`: ```ts [ { type: 'data-weather', id: 'weather-1', data: { city: 'San Francisco', status: 'loading' }, }, { type: 'step-start' }, { type: 'text', text: 'It is sunny.', state: 'done', providerMetadata: undefined, }, ] ``` Against the test-only commit (`6fa891d8b6`), the repro failed because `convertToModelMessages([persistedMessage])` returned an extra empty assistant message: ```ts [ { role: 'assistant', content: [] }, { role: 'assistant', content: [{ type: 'text', text: 'It is sunny.' }] }, ] ``` Against the fixed commit (`872f91b540`), the same repro passed and returned only the valid assistant message: ```ts [ { role: 'assistant', content: [{ type: 'text', text: 'It is sunny.' }] }, ] ``` Focused test on the fixed head: ```sh vitest --config packages/ai/vitest.node.config.js --run packages/ai/src/ui/convert-to-model-messages.test.ts ``` Result: 64 tests passed, no type errors. ## Checklist - [x] All commits are signed - [x] Tests have been added / updated - [ ] Documentation has been added / updated - [x] A _patch_ changeset for relevant packages has been added - [x] I have reviewed this pull request ## Related Issues Fixes #13513 --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com> (cherry picked from commit d598481)
Contributor
|
|
Contributor
|
🚀 Published in:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport of #15040 to
release-v6.0(clean cherry-pick). Data-only blocks previously produced empty assistant messages.Part of the v6.0 backport tracking issue #16767.