Skip to content

Commit

Permalink
feat (ai): verify that system messages have string content (#2206)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Jul 8, 2024
1 parent d481729 commit abb2260
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-penguins-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

feat (ai): verify that system messages have string content
19 changes: 19 additions & 0 deletions packages/core/core/prompt/get-validated-prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { InvalidPromptError } from '@ai-sdk/provider';
import { getValidatedPrompt } from './get-validated-prompt';

describe('getValidatedPrompt', () => {
describe('messages', () => {
it('should throw InvalidPromptError when system message has parts', () => {
expect(() => {
getValidatedPrompt({
messages: [
{
role: 'system',
content: [{ type: 'text', text: 'test' }] as any,
},
],
});
}).toThrow(InvalidPromptError);
});
});
});
11 changes: 11 additions & 0 deletions packages/core/core/prompt/get-validated-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ export function getValidatedPrompt(prompt: Prompt): ValidatedPrompt {
});
}

if (prompt.messages != null) {
for (const message of prompt.messages) {
if (message.role === 'system' && typeof message.content !== 'string') {
throw new InvalidPromptError({
prompt,
message: 'system message content must be a string',
});
}
}
}

return prompt.prompt != null
? {
type: 'prompt',
Expand Down

0 comments on commit abb2260

Please sign in to comment.