Skip to content

Commit 89d8b45

Browse files
sleitordancer
andauthored
fix(google): make urlMetadata optional in urlContextMetadata schema (#12701)
## Summary The Gemini API can return `urlContextMetadata: {}` (empty object without `urlMetadata` key) when Google Search grounding is used but no URLs were fetched via the URL context tool. The Zod schema expected `urlMetadata` to always be an array, causing an `AI_APICallError` on an otherwise valid 200 response. ## Changes - **`google-generative-ai-language-model.ts`**: Make `urlMetadata` field `.nullish()` in the `getUrlContextMetadataSchema()` schema. - **Tests**: Added test case for empty object without `urlMetadata` key. All urlContextMetadata-related tests pass. Fixes #12698 --------- Co-authored-by: Dmitrii Troitskii <jsleitor@gmail.com> Co-authored-by: dancer <josh@afterima.ge> Co-authored-by: dancer <144584931+dancer@users.noreply.github.com>
1 parent 39d0544 commit 89d8b45

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

.changeset/healthy-feet-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/google': patch
3+
---
4+
5+
fix(google): make urlMetadata optional in urlContextMetadata schema

packages/google/src/google-generative-ai-language-model.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ describe('urlContextMetadata', () => {
330330
const result = urlContextMetadataSchema.safeParse(output);
331331
expect(result.success).toBe(true);
332332
});
333+
334+
it('validates empty object without urlMetadata key', () => {
335+
const output = {};
336+
337+
const result = urlContextMetadataSchema.safeParse(output);
338+
expect(result.success).toBe(true);
339+
});
333340
});
334341

335342
describe('doGenerate', () => {

packages/google/src/google-generative-ai-language-model.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,12 +974,14 @@ const usageSchema = z.object({
974974
// https://ai.google.dev/api/generate-content#UrlRetrievalMetadata
975975
export const getUrlContextMetadataSchema = () =>
976976
z.object({
977-
urlMetadata: z.array(
978-
z.object({
979-
retrievedUrl: z.string(),
980-
urlRetrievalStatus: z.string(),
981-
}),
982-
),
977+
urlMetadata: z
978+
.array(
979+
z.object({
980+
retrievedUrl: z.string(),
981+
urlRetrievalStatus: z.string(),
982+
}),
983+
)
984+
.nullish(),
983985
});
984986

985987
const responseSchema = lazySchema(() =>

0 commit comments

Comments
 (0)