media_storage_multiturn.py
"""
Multi-turn Media Storage
========================
Demonstrates a multi-turn conversation over offloaded media. Turn 1 uploads the image to S3
and keeps only a MediaReference; turn 2 asks about it without re-attaching it. The stored
reference is re-signed on read, so the model fetches the image from S3 and the bytes never
travel back through the database.
store=False keeps history client-side; OpenAIResponses would otherwise chain turns via
previous_response_id and turn 2 would send no image at all.
Requirements:
- uv pip install 'agno[s3]'
- AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
- Set MEDIA_S3_BUCKET to the destination bucket
"""
import os
import httpx
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.media import Image
from agno.media.storage import S3MediaStorage
from agno.models.openai import OpenAIResponses
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
DB_FILE = "tmp/multiturn.db"
IMAGE_URL = "https://picsum.photos/id/15/800/600.jpg"
bucket = os.getenv("MEDIA_S3_BUCKET")
if not bucket:
raise ValueError("MEDIA_S3_BUCKET must be set to the destination S3 bucket")
storage = S3MediaStorage(
bucket=bucket,
region=os.getenv("AWS_REGION"),
prefix="agno/media/",
presigned_url_expiry=3600, # 1 hour
)
# ---------------------------------------------------------------------------
# Create the Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=OpenAIResponses(
id="gpt-5.5", store=False
), # keep history client-side, see docstring
media_storage=storage,
db=SqliteDb(db_file=DB_FILE),
session_id="multiturn-session",
add_history_to_context=True,
)
# ---------------------------------------------------------------------------
# Run the Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
image_bytes = httpx.get(IMAGE_URL, follow_redirects=True).content
# Turn 1: send the image and ask about it
agent.print_response(
"What do you see in this image?",
images=[Image(content=image_bytes, format="jpeg", mime_type="image/jpeg")],
)
# Turn 2: ask again without re-attaching it — the reference is re-signed for the model
agent.print_response("What was the image about?")
Run the Example
1
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
2
Install dependencies
uv pip install -U "agno[s3]" openai httpx sqlalchemy
3
Export your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"
$Env:OPENAI_API_KEY="your_openai_api_key_here"
4
Export your AWS credentials
export AWS_ACCESS_KEY_ID="your_key_id"
export AWS_SECRET_ACCESS_KEY="your_secret"
export AWS_REGION="us-east-1"
export MEDIA_S3_BUCKET="your_bucket"
$Env:AWS_ACCESS_KEY_ID="your_key_id"
$Env:AWS_SECRET_ACCESS_KEY="your_secret"
$Env:AWS_REGION="us-east-1"
$Env:MEDIA_S3_BUCKET="your_bucket"
5
Run the example
Save the code above as
media_storage_multiturn.py, then run:python media_storage_multiturn.py