-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(provider/google): Handling of Image data from the tool call results. #8357
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
Merged
+196
−9
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1d22f64
fixed handling of image data in from the tool result in google genera…
AVtheking 7ed92c2
changeset
AVtheking 3628816
comments
AVtheking c5fcd92
Merge branch 'main' into fix/google-image-tool-result
AVtheking f09802d
Merge branch 'main' into fix/google-image-tool-result
AVtheking 392f89a
use local image
AVtheking a1f974f
Merge branch 'main' into fix/google-image-tool-result
gr2m 38b93a3
fix image handling
AVtheking 0914306
fix test
AVtheking 87ce9b2
Merge branch 'main' into fix/google-image-tool-result
gr2m 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 | ||
--- | ||
|
||
Fixed handling of image response in the tool call result. |
82 changes: 82 additions & 0 deletions
82
examples/ai-core/src/generate-text/google-image-tool-results.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,82 @@ | ||
import { google } from '@ai-sdk/google'; | ||
import { generateText, stepCountIs, tool } from 'ai'; | ||
import { z } from 'zod/v4'; | ||
import 'dotenv/config'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
async function fileToBase64(filePath: string): Promise<string> { | ||
const fileBuffer = await fs.promises.readFile(filePath); | ||
return fileBuffer.toString('base64'); | ||
} | ||
|
||
const imageAnalysisTool = tool({ | ||
description: 'Give the image ', | ||
inputSchema: z.object({}), | ||
execute: async ({}) => { | ||
try { | ||
const imagePath = path.join(__dirname, '../../data/comic-cat.png'); | ||
const base64Image = await fileToBase64(imagePath); | ||
|
||
return { | ||
success: true, | ||
description: 'Image fetched successfully', | ||
base64Image, | ||
}; | ||
} catch (error) { | ||
return { | ||
success: false, | ||
error: `Failed to fetch image: ${error instanceof Error ? error.message : 'Unknown error'}`, | ||
}; | ||
} | ||
}, | ||
|
||
toModelOutput(output: { base64Image?: string }) { | ||
return { | ||
type: 'content', | ||
value: [ | ||
{ | ||
type: 'media', | ||
mediaType: 'image/png', | ||
data: output.base64Image!, | ||
}, | ||
], | ||
}; | ||
}, | ||
}); | ||
|
||
async function main() { | ||
console.log( | ||
'🔍 Testing Google model image analysis with tool-returned images...\n', | ||
); | ||
|
||
const result = await generateText({ | ||
model: google('gemini-2.5-flash'), | ||
tools: { | ||
analyzeImage: imageAnalysisTool, | ||
}, | ||
stopWhen: stepCountIs(2), | ||
prompt: `Whats in this image?`, | ||
}); | ||
|
||
console.log('📋 Analysis Result: \n'); | ||
console.log('='.repeat(60)); | ||
console.log(`${JSON.stringify(result.text, null, 2)}\n`); | ||
// console.log(JSON.stringify(result.steps, null, 2)); | ||
|
||
if (result.toolCalls && result.toolCalls.length > 0) { | ||
console.log('🔧 Tool Calls Made: \n'); | ||
result.toolCalls.forEach((call, index) => { | ||
console.log(`${index + 1}. ${call.toolName}:`); | ||
console.log(` Input: ${JSON.stringify(call.input, null, 2)}`); | ||
}); | ||
console.log(); | ||
} | ||
|
||
console.log('📊 Usage: \n'); | ||
console.log(`Input tokens: ${result.usage.inputTokens}`); | ||
console.log(`Output tokens: ${result.usage.outputTokens}`); | ||
console.log(`Total tokens: ${result.usage.totalTokens}`); | ||
} | ||
|
||
main().catch(console.error); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.