handle no parts returned from gemini

This commit is contained in:
Dallin Romney 2025-06-03 19:46:23 -07:00
parent bfa08bc938
commit 1a9ff7ac48
2 changed files with 8 additions and 7 deletions

View file

@ -342,13 +342,13 @@ class Gemini extends BaseLLM {
throw new Error(data.error.message);
}
// Check for existence of each level before accessing the final 'text' property
const content = data?.candidates?.[0]?.content;
if (content) {
// In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec
const contentParts = data?.candidates?.[0]?.content?.parts;
if (contentParts) {
const textParts: MessagePart[] = [];
const toolCalls: ToolCallDelta[] = [];
for (const part of content.parts) {
for (const part of contentParts) {
if ("text" in part) {
textParts.push({ type: "text", text: part.text });
} else if ("functionCall" in part) {

View file

@ -287,9 +287,10 @@ export class GeminiApi implements BaseLlmApi {
throw new Error(data.error.message);
}
const content = data?.candidates?.[0]?.content;
if (content) {
for (const part of content.parts) {
// In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec
const contentParts = data?.candidates?.[0]?.content?.parts;
if (contentParts) {
for (const part of contentParts) {
if ("text" in part) {
yield chatChunk({
content: part.text,