Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-convert-to-model-messages-empty-assistant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

Fix: `convertToModelMessages` no longer emits an empty assistant message when a block contains only unknown data parts (e.g. a data part before `step-start` with no `convertDataPart` provided)
105 changes: 105 additions & 0 deletions packages/ai/src/ui/convert-to-model-messages.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import { convertArrayToReadableStream } from '@ai-sdk/provider-utils/test';
import type { ModelMessage } from '@ai-sdk/provider-utils';
import { describe, expect, it } from 'vitest';
import type { UIMessageChunk } from '../ui-message-stream/ui-message-chunks';
import { consumeStream } from '../util/consume-stream';
import { convertToModelMessages } from './convert-to-model-messages';
import {
createStreamingUIMessageState,
processUIMessageStream,
} from './process-ui-message-stream';
import type { UIMessage } from './ui-messages';

async function recordAssistantMessageFromChunks<
UI_MESSAGE extends UIMessage = UIMessage,
>(chunks: UIMessageChunk[]): Promise<UI_MESSAGE> {
const state = createStreamingUIMessageState<UI_MESSAGE>({
messageId: 'msg-123',
lastMessage: undefined,
});

await consumeStream({
stream: processUIMessageStream<UI_MESSAGE>({
stream: convertArrayToReadableStream(chunks),
runUpdateMessageJob: async job => {
await job({
state,
write: () => {},
});
},
onError: error => {
throw error;
},
}),
});

return state.message;
}

describe('convertToModelMessages', () => {
describe('system message', () => {
it('should convert a simple system message', async () => {
Expand Down Expand Up @@ -2666,6 +2699,78 @@ describe('convertToModelMessages', () => {
`);
});

it('should not emit empty assistant message for persistent data written before model stream starts', async () => {
type WeatherUIMessage = UIMessage<
unknown,
{
weather: {
city: string;
status: 'loading' | 'success';
weather?: string;
};
}
>;

const recordedMessage =
await recordAssistantMessageFromChunks<WeatherUIMessage>([
{ type: 'start', messageId: 'msg-123' },
{
type: 'data-weather',
id: 'weather-1',
data: { city: 'San Francisco', status: 'loading' },
},
{ type: 'start-step' },
{ type: 'text-start', id: 'text-1' },
{ type: 'text-delta', id: 'text-1', delta: 'It is sunny.' },
{ type: 'text-end', id: 'text-1' },
{ type: 'finish-step' },
{ type: 'finish' },
]);

expect(recordedMessage).toMatchInlineSnapshot(`
{
"id": "msg-123",
"metadata": undefined,
"parts": [
{
"data": {
"city": "San Francisco",
"status": "loading",
},
"id": "weather-1",
"type": "data-weather",
},
{
"type": "step-start",
},
{
"providerMetadata": undefined,
"state": "done",
"text": "It is sunny.",
"type": "text",
},
],
"role": "assistant",
}
`);

const result = await convertToModelMessages([recordedMessage]);

expect(result).toMatchInlineSnapshot(`
[
{
"content": [
{
"text": "It is sunny.",
"type": "text",
},
],
"role": "assistant",
},
]
`);
});

it('should selectively convert data parts', async () => {
const result = await convertToModelMessages<
UIMessage<
Expand Down
10 changes: 6 additions & 4 deletions packages/ai/src/ui/convert-to-model-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,12 @@ export async function convertToModelMessages<UI_MESSAGE extends UIMessage>(
}
}

modelMessages.push({
role: 'assistant',
content,
});
if (content.length > 0) {
modelMessages.push({
role: 'assistant',
content,
});
}

// check if there are tool invocations with results in the block
// Include non-provider-executed tools, OR provider-executed tools with approval responses
Expand Down
Loading