Skip to content

Commit

Permalink
validateChatHistory is now checking that 'parts' property is an array (
Browse files Browse the repository at this point in the history
…#62)

* validateChatHistory is now checking that 'parts' property is an array

* typo
  • Loading branch information
alx13 authored Mar 14, 2024
1 parent d46ef73 commit ccd9951
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lazy-houses-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": patch
---

validateChatHistory is now checking that 'parts' property is an array
10 changes: 10 additions & 0 deletions packages/main/src/methods/chat-session-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ describe("chat-session-helpers", () => {
],
isValid: true,
},
{
//@ts-expect-error
history: [{ role: "user", parts: "" }],
isValid: false,
},
{
//@ts-expect-error
history: [{ role: "user" }],
isValid: false,
},
{
history: [{ role: "user", parts: [] }],
isValid: false,
Expand Down
12 changes: 11 additions & 1 deletion packages/main/src/methods/chat-session-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export function validateChatHistory(history: Content[]): void {
);
}

if (!Array.isArray(parts)) {
throw new GoogleGenerativeAIError(
"Content should have 'parts' property with an array of Parts",
);
}

if (parts.length === 0) {
throw new GoogleGenerativeAIError(
"Each Content should have at least one part",
Expand Down Expand Up @@ -89,7 +95,11 @@ export function validateChatHistory(history: Content[]): void {
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
if (!validPreviousContentRoles.includes(prevContent.role)) {
throw new GoogleGenerativeAIError(
`Content with role '${role}' can't follow '${prevContent.role}'`,
`Content with role '${role}' can't follow '${
prevContent.role
}'. Valid previous roles: ${JSON.stringify(
VALID_PREVIOUS_CONTENT_ROLES,
)}`,
);
}
}
Expand Down

0 comments on commit ccd9951

Please sign in to comment.