-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(provider/google): Url support for pdfs and images in tool results #9963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
songkeys
wants to merge
2
commits into
vercel:main
Choose a base branch
from
songkeys:google--pdf-urls-v6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+336
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@ai-sdk/google': patch | ||
| --- | ||
|
|
||
| Adds url-based pdf and image support for google tool results |
50 changes: 50 additions & 0 deletions
50
examples/ai-core/src/generate-text/google-image-tool-result-url.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { google } from '@ai-sdk/google'; | ||
| import { generateText, stepCountIs, tool } from 'ai'; | ||
| import { run } from '../lib/run'; | ||
| import { z } from 'zod'; | ||
|
|
||
| run(async () => { | ||
| const readImage = tool({ | ||
| description: `Read and return an image`, | ||
| inputSchema: z.object({}), | ||
| execute: async () => { | ||
| try { | ||
| return { | ||
| success: true, | ||
| description: 'Successfully loaded image', | ||
| imageUrl: | ||
| 'https://github.com/vercel/ai/blob/main/examples/ai-core/data/comic-cat.png?raw=true', | ||
| }; | ||
| } catch (error) { | ||
| throw new Error(`Failed to analyze image: ${error}`); | ||
| } | ||
| }, | ||
| toModelOutput(result) { | ||
| return { | ||
| type: 'content', | ||
| value: [ | ||
| { | ||
| type: 'text', | ||
| text: result.description, | ||
| }, | ||
| { | ||
| type: 'image-url', | ||
| url: result.imageUrl, | ||
| }, | ||
| ], | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const result = await generateText({ | ||
| model: google('gemini-2.5-flash'), | ||
| prompt: | ||
| 'Please read the image using the tool provided and return the summary of that image', | ||
| tools: { | ||
| readImage, | ||
| }, | ||
| stopWhen: stepCountIs(4), | ||
| }); | ||
|
|
||
| console.log(`Assistant response: ${JSON.stringify(result.text, null, 2)}`); | ||
| }); |
60 changes: 60 additions & 0 deletions
60
examples/ai-core/src/generate-text/google-pdf-tool-results-base64.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { google } from '@ai-sdk/google'; | ||
| import { generateText, stepCountIs, tool } from 'ai'; | ||
| import { run } from '../lib/run'; | ||
| import { z } from 'zod'; | ||
| import path from 'path'; | ||
| import fs from 'fs/promises'; | ||
|
|
||
| run(async () => { | ||
| const readPDFDocument = tool({ | ||
| description: `Read and return a PDF document`, | ||
| inputSchema: z.object({}), | ||
| execute: async () => { | ||
| try { | ||
| const pdfPath = path.join(__dirname, '../../data/ai.pdf'); | ||
| const pdfData = await fs.readFile(pdfPath); | ||
|
|
||
| const base64Data = pdfData.toString('base64'); | ||
|
|
||
| console.log(`PDF document read successfully`); | ||
|
|
||
| return { | ||
| success: true, | ||
| description: 'Successfully loaded PDF document', | ||
| pdfData: base64Data, | ||
| }; | ||
| } catch (error) { | ||
| throw new Error(`Failed to analyze PDF: ${error}`); | ||
| } | ||
| }, | ||
| toModelOutput(result) { | ||
| return { | ||
| type: 'content', | ||
| value: [ | ||
| { | ||
| type: 'text', | ||
| text: result.description, | ||
| }, | ||
| { | ||
| type: 'file-data', | ||
| data: result.pdfData, | ||
| mediaType: 'application/pdf', | ||
| filename: 'ai.pdf', | ||
| }, | ||
| ], | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const result = await generateText({ | ||
| model: google('gemini-2.5-flash'), | ||
| prompt: | ||
| 'Please read the pdf document using the tool provided and return the summary of that pdf', | ||
| tools: { | ||
| readPDFDocument, | ||
| }, | ||
| stopWhen: stepCountIs(4), | ||
| }); | ||
|
|
||
| console.log(`Assistant response: ${JSON.stringify(result.text, null, 2)}`); | ||
| }); |
50 changes: 50 additions & 0 deletions
50
examples/ai-core/src/generate-text/google-pdf-tool-results-url.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { google } from '@ai-sdk/google'; | ||
| import { generateText, stepCountIs, tool } from 'ai'; | ||
| import { run } from '../lib/run'; | ||
| import { z } from 'zod'; | ||
|
|
||
| run(async () => { | ||
| const readPDFDocument = tool({ | ||
| description: `Read and return a PDF document`, | ||
| inputSchema: z.object({}), | ||
| execute: async () => { | ||
| try { | ||
| return { | ||
| success: true, | ||
| description: 'Successfully loaded PDF document', | ||
| pdfUrl: | ||
| 'https://github.com/vercel/ai/blob/main/examples/ai-core/data/ai.pdf?raw=true', | ||
| }; | ||
| } catch (error) { | ||
| throw new Error(`Failed to analyze PDF: ${error}`); | ||
| } | ||
| }, | ||
| toModelOutput(result) { | ||
| return { | ||
| type: 'content', | ||
| value: [ | ||
| { | ||
| type: 'text', | ||
| text: result.description, | ||
| }, | ||
| { | ||
| type: 'file-url', | ||
| url: result.pdfUrl, | ||
| }, | ||
| ], | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const result = await generateText({ | ||
| model: google('gemini-2.5-flash'), | ||
| prompt: | ||
| 'Please read the pdf document using the tool provided and return the summary of that pdf', | ||
| tools: { | ||
| readPDFDocument, | ||
| }, | ||
| stopWhen: stepCountIs(4), | ||
| }); | ||
|
|
||
| console.log(`Assistant response: ${JSON.stringify(result.text, null, 2)}`); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default MIME type for
image-urltool results is'image/*', but this is a wildcard pattern used in HTTP Accept headers, not a valid concrete MIME type. It should be'image/jpeg'to be consistent with how the same file handles generic image types elsewhere (line 95) and to match patterns in other providers.View Details
📝 Patch Details
Analysis
Invalid MIME type
image/*used as default for tool result image URLsWhat fails:
convertToGoogleGenerativeAIMessages()inpackages/google/src/convert-to-google-generative-ai-messages.tsusesimage/*as the default MIME type when a tool result image URL has an unknown file extension, causing Google's Generative AI API to reject requests with an invalid MIME type.How to reproduce:
Result: Requests with
mimeType: 'image/*'are sent to the Google Generative AI API. Sinceimage/*is a wildcard pattern for Accept headers (RFC 2045), not a valid concrete MIME type, the API rejects the request.Expected: Should default to
image/jpegto match:part.mediaType === 'image/*' ? 'image/jpeg' : part.mediaTypeimage/*toimage/jpegimage/jpeg,image/png) are required in Content-Type headers and API payloads; wildcards are only valid in Accept headersFix: Changed line 229 from
defaultMimeType: 'image/*'todefaultMimeType: 'image/jpeg'and added test case for unknown file extensions to prevent regression.