Skip to content

Examples for image inputs #553

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
merged 1 commit into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/basic/local_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import asyncio
import base64
import os

from agents import Agent, Runner

FILEPATH = os.path.join(os.path.dirname(__file__), "media/image_bison.jpg")


def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
return encoded_string


async def main():
# Print base64-encoded image
b64_image = image_to_base64(FILEPATH)

agent = Agent(
name="Assistant",
instructions="You are a helpful assistant.",
)

result = await Runner.run(
agent,
[
{
"role": "user",
"content": [
{
"type": "input_image",
"detail": "auto",
"image_url": f"data:image/jpeg;base64,{b64_image}",
}
],
},
{
"role": "user",
"content": "What do you see in this image?",
},
],
)
print(result.final_output)


if __name__ == "__main__":
asyncio.run(main())
Binary file added examples/basic/media/image_bison.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions examples/basic/remote_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import asyncio

from agents import Agent, Runner

URL = "https://upload.wikimedia.org/wikipedia/commons/0/0c/GoldenGateBridge-001.jpg"


async def main():
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant.",
)

result = await Runner.run(
agent,
[
{
"role": "user",
"content": [{"type": "input_image", "detail": "auto", "image_url": URL}],
},
{
"role": "user",
"content": "What do you see in this image?",
},
],
)
print(result.final_output)


if __name__ == "__main__":
asyncio.run(main())