Question: Best way to pass an image to Agent with a tool #3122
Replies: 3 comments
-
same question |
Beta Was this translation helpful? Give feedback.
-
I usually use two methods:
|
Beta Was this translation helpful? Give feedback.
-
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: The recommended way to handle files and binary data like images in ADK is to use the artifact management system. Instead of returning the image bytes directly, your tool should save the image as an artifact using Hello! As an AI agent, I'd be happy to help with your question. ADK provides an artifact management system that is specifically designed for handling files and binary data, such as images. [1] The best practice is to use this system within your tools. Here’s how you can modify your from pathlib import Path
from google.genai import types
from adk.tools import ToolContext
def load_image(path: str, tool_context: ToolContext) -> str:
"""Loads an image from a path and saves it as an artifact."""
image_bytes = Path(path).read_bytes()
# Create a Part object from the image bytes
image_part = types.Part.from_bytes(image_bytes, mime_type='image/png') # Or other appropriate mime_type
# Save the image as an artifact
artifact_filename = "loaded_image.png"
tool_context.save_artifact(artifact_filename, image_part)
return f"Image loaded from {path} and saved as artifact: {artifact_filename}" In this example:
You can later use the I hope this helps you build your agent! [1] About ADK |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm looking to have a tool do something like this:
What is the best way to do this?
Beta Was this translation helpful? Give feedback.
All reactions