Skip to content

Commit 3d6d96d

Browse files
authored
fix (ai/core): validate that messages are not empty (#5374)
1 parent c1dd343 commit 3d6d96d

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

Diff for: .changeset/lazy-paws-divide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
fix (ai/core): validate that messages are not empty

Diff for: packages/ai/core/prompt/convert-to-language-model-prompt.ts

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
import { InvalidMessageRoleError } from './invalid-message-role-error';
1818
import { splitDataUrl } from './split-data-url';
1919
import { StandardizedPrompt } from './standardize-prompt';
20-
import { convertUint8ArrayToBase64 } from '@ai-sdk/provider-utils';
2120

2221
export async function convertToLanguageModelPrompt({
2322
prompt,

Diff for: packages/ai/core/prompt/standardize-prompt.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,15 @@ describe('message prompt', () => {
1717
});
1818
}).toThrow(InvalidPromptError);
1919
});
20+
21+
it('should throw InvalidPromptError when messages array is empty', () => {
22+
expect(() => {
23+
standardizePrompt({
24+
prompt: {
25+
messages: [],
26+
},
27+
tools: undefined,
28+
});
29+
}).toThrow(InvalidPromptError);
30+
});
2031
});

Diff for: packages/ai/core/prompt/standardize-prompt.ts

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ export function standardizePrompt<TOOLS extends ToolSet>({
9595
})
9696
: (prompt.messages as CoreMessage[]);
9797

98+
if (messages.length === 0) {
99+
throw new InvalidPromptError({
100+
prompt,
101+
message: 'messages must not be empty',
102+
});
103+
}
104+
98105
const validationResult = safeValidateTypes({
99106
value: messages,
100107
schema: z.array(coreMessageSchema),

Diff for: packages/ai/rsc/stream-ui/stream-ui.ui.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ describe('rsc - streamUI() onFinish callback', () => {
192192
beforeEach(async () => {
193193
const ui = await streamUI({
194194
model: mockToolModel,
195-
messages: [],
195+
prompt: '',
196196
tools: {
197197
tool1: {
198198
description: 'test tool 1',

0 commit comments

Comments
 (0)