Skip to content
Draft
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)
110 changes: 110 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', () => {
Expand Down Expand Up @@ -1875,8 +1908,85 @@
`);
});

<<<<<<< HEAD

Check failure on line 1911 in packages/ai/src/ui/convert-to-model-messages.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
it('should selectively convert data parts', () => {
const result = convertToModelMessages<
=======

Check failure on line 1914 in packages/ai/src/ui/convert-to-model-messages.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
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<
>>>>>>> aa2dbe65e8 ([v6.0] fix(ai): omit empty assistant message for data-only blocks in convertToModelMessages (#16793))

Check failure on line 1989 in packages/ai/src/ui/convert-to-model-messages.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
UIMessage<
unknown,
{
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 @@ -240,10 +240,12 @@ export 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
const toolParts = block.filter(
Expand Down
Loading