Skip to content

Commit b983b51

Browse files
authored
feat (ai): support model message array in prompt (#6189)
## Background Limiting the `prompt` property to `string` inputs makes it harder to change from text prompts to message prompts. There is no reason why `prompt` cannot accept `ModelMessage[]` inputs as well. ## Summary Allow `ModelMessage[]` inputs on `prompt`.
1 parent 13fef90 commit b983b51

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

‎.changeset/two-otters-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': major
3+
---
4+
5+
feat (ai): support model message array in prompt

‎packages/ai/core/prompt/prompt.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ System message to include in the prompt. Can be used with `prompt` or `messages`
1111
system?: string;
1212

1313
/**
14-
A simple text prompt. You can either use `prompt` or `messages` but not both.
15-
*/
16-
prompt?: string;
14+
A prompt. It can be either a text prompt or a list of messages.
15+
16+
You can either use `prompt` or `messages` but not both.
17+
*/
18+
prompt?: string | Array<ModelMessage>;
1719

1820
/**
19-
A list of messages. You can either use `prompt` or `messages` but not both.
21+
A list of messages.
22+
23+
You can either use `prompt` or `messages` but not both.
2024
*/
2125
messages?: Array<ModelMessage>;
2226
};

‎packages/ai/core/prompt/standardize-prompt.ts

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,54 +41,43 @@ export async function standardizePrompt(
4141
});
4242
}
4343

44-
// type: prompt
45-
if (prompt.prompt != null) {
46-
// validate that prompt is a string
47-
if (typeof prompt.prompt !== 'string') {
48-
throw new InvalidPromptError({
49-
prompt,
50-
message: 'prompt must be a string',
51-
});
52-
}
44+
let messages: ModelMessage[];
5345

54-
return {
55-
system: prompt.system,
56-
messages: [
57-
{
58-
role: 'user',
59-
content: prompt.prompt,
60-
},
61-
],
62-
};
46+
if (prompt.prompt != null && typeof prompt.prompt === 'string') {
47+
messages = [{ role: 'user', content: prompt.prompt }];
48+
} else if (prompt.prompt != null && Array.isArray(prompt.prompt)) {
49+
messages = prompt.prompt;
50+
} else if (prompt.messages != null) {
51+
messages = prompt.messages;
52+
} else {
53+
throw new InvalidPromptError({
54+
prompt,
55+
message: 'prompt or messages must be defined',
56+
});
6357
}
6458

65-
// type: messages
66-
if (prompt.messages != null) {
67-
if (prompt.messages.length === 0) {
68-
throw new InvalidPromptError({
69-
prompt,
70-
message: 'messages must not be empty',
71-
});
72-
}
73-
74-
const validationResult = await safeValidateTypes({
75-
value: prompt.messages,
76-
schema: z.array(modelMessageSchema),
59+
if (messages.length === 0) {
60+
throw new InvalidPromptError({
61+
prompt,
62+
message: 'messages must not be empty',
7763
});
64+
}
7865

79-
if (!validationResult.success) {
80-
throw new InvalidPromptError({
81-
prompt,
82-
message: 'messages must be an array of ModelMessage or UIMessage',
83-
cause: validationResult.error,
84-
});
85-
}
66+
const validationResult = await safeValidateTypes({
67+
value: messages,
68+
schema: z.array(modelMessageSchema),
69+
});
8670

87-
return {
88-
messages: prompt.messages,
89-
system: prompt.system,
90-
};
71+
if (!validationResult.success) {
72+
throw new InvalidPromptError({
73+
prompt,
74+
message: 'messages must be an array of ModelMessage',
75+
cause: validationResult.error,
76+
});
9177
}
9278

93-
throw new Error('unreachable');
79+
return {
80+
messages,
81+
system: prompt.system,
82+
};
9483
}

0 commit comments

Comments
 (0)