🐛 fix: Handle content generation errors in GoogleClient (#5575)

This commit is contained in:
Danny Avila 2025-01-31 11:22:15 -05:00 committed by GitHub
parent 6920e23fb2
commit fdf0b41d08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -605,6 +605,7 @@ class GoogleClient extends BaseClient {
let reply = ''; let reply = '';
try {
if (!EXCLUDED_GENAI_MODELS.test(modelName) && !this.project_id) { if (!EXCLUDED_GENAI_MODELS.test(modelName) && !this.project_id) {
/** @type {GenAI} */ /** @type {GenAI} */
const client = this.client; const client = this.client;
@ -629,6 +630,7 @@ class GoogleClient extends BaseClient {
const delay = modelName.includes('flash') ? 8 : 15; const delay = modelName.includes('flash') ? 8 : 15;
/** @type {GenAIUsageMetadata} */ /** @type {GenAIUsageMetadata} */
let usageMetadata; let usageMetadata;
const result = await client.generateContentStream(requestOptions); const result = await client.generateContentStream(requestOptions);
for await (const chunk of result.stream) { for await (const chunk of result.stream) {
usageMetadata = !usageMetadata usageMetadata = !usageMetadata
@ -648,6 +650,7 @@ class GoogleClient extends BaseClient {
output_tokens: usageMetadata.candidatesTokenCount, output_tokens: usageMetadata.candidatesTokenCount,
}; };
} }
return reply; return reply;
} }
@ -691,6 +694,9 @@ class GoogleClient extends BaseClient {
if (usageMetadata) { if (usageMetadata) {
this.usage = usageMetadata; this.usage = usageMetadata;
} }
} catch (e) {
logger.error('[GoogleClient] There was an issue generating the completion', e);
}
return reply; return reply;
} }