Send media to models

In shortSend images, audio, video and documents to a model from Python using ImageBlock and other content blocks, MediaRef, multimodal_config and storage backends.

  • 7 min read
  • 12 sections
  • Updated
  • v1.0.0
  • Markdown

Send images, audio, video and documents to a model as typed content blocks next to text. Each media block holds a MediaRef that says where the bytes are: an external URL, inline base64, or a reference into a media store. How blocks and stores fit together is in Media and files.

Content blocks and MediaRef

Any message is a list of content blocks. 10xGraph provides typed blocks for each medium:

Python
from tenxgraph.core.state import (
    TextBlock,
    ImageBlock,
    AudioBlock,
    VideoBlock,
    DocumentBlock,
    DataBlock,
    Message,
    MediaRef,
)

Each block except TextBlock holds a MediaRef that tells 10xGraph where the bytes are. MediaRef has three kind values:

Kind Use case
url External URL, or graph://media/{key} from a media store (use media_store.to_media_ref(key, mime_type) to build it)
data Inline base64 (data_base64) for small payloads only
file_id A provider-managed file ID, for example one returned by the OpenAI or Gemini file APIs

An Agent only keeps media blocks when you pass it a multimodal_config. Without one, image, audio, video and document blocks are stripped before the model call. The examples below all pass MultimodalConfig().

Install the provider extra for the model you use, for example pip install "10xgraph[google-genai]" for Gemini or pip install "10xgraph[openai]" for OpenAI.

Example 1: Image from a public URL

Send an image directly from the web:

Python
from tenxgraph.core.graph import Agent, StateGraph
from tenxgraph.core.state import Message, TextBlock, ImageBlock, MediaRef
from tenxgraph.storage.media import MultimodalConfig
from tenxgraph.utils import END

agent = Agent(
    model="gemini-2.5-flash",
    provider="google",
    multimodal_config=MultimodalConfig(),
)

graph = StateGraph()
graph.add_node("MAIN", agent)
graph.set_entry_point("MAIN")
graph.add_edge("MAIN", END)
app = graph.compile()

messages = [
    Message(
        role="user",
        content=[
            TextBlock(text="What is in this image?"),
            ImageBlock(
                media=MediaRef(
                    kind="url",
                    url="https://example.com/photo.png",
                    mime_type="image/png",
                )
            ),
        ],
    )
]

result = app.invoke({"messages": messages})
print(result["messages"][-1].text())

This is the simplest approach: you give the model a URL and 10xGraph resolves it for the provider. Agent is a graph node, so run it through a compiled graph (app), which later examples reuse.

Control which URLs the server downloads

A kind="url" block is untrusted input when it comes from a client. What happens to it depends on the provider path:

  • OpenAI and Anthropic fetch the URL themselves. Your server only passes it on, and the policy below does not apply.
  • Google GenAI, the Google File API and the inline-bytes transport make your server download the URL first. These downloads go through a MediaFetchPolicy.

MediaFetchPolicy is a frozen pydantic model in tenxgraph.storage.media. The defaults are safe, so you only set the fields you want to change:

Field Default Meaning
enabled True False means the server never downloads an external URL. URLs a provider fetches itself are not affected.
allowed_schemes ("https", "http") A subset of these two. No other scheme can be allowed.
allowed_hosts () Empty allows any public host. Entries are exact names or "*.example.com" (subdomains only).
allow_private_for_allowed_hosts False On-prem opt-in: hosts listed in allowed_hosts may resolve to private addresses.
allowed_content_types ("image/*", "application/pdf") Response Content-Type values accepted. "image/*" style wildcards work.
max_bytes 20 MiB Largest download.
timeout 10.0 Seconds for the whole download, redirects included.
allowed_gcs_buckets () gs:// buckets a client may reference. Empty rejects every gs:// URL.

These rules always apply and cannot be turned off:

  • Only http and https are downloaded.
  • Cloud metadata addresses are never reached, even with the private-host opt-in.
  • A host name must resolve to public addresses, and the connection goes to the address that was checked.
  • Every redirect is checked again, and at most 5 are followed.
  • The size cap and the timeout always apply.

Pass the policy to compile(). The prebuilt agents take the same argument on their own compile():

Python
from tenxgraph.storage.media import MediaFetchPolicy

policy = MediaFetchPolicy(
    allowed_hosts=("cdn.example.com", "*.example.org"),
    allowed_content_types=("image/*", "application/pdf", "audio/*"),
    max_bytes=10 * 1024 * 1024,
    timeout=5.0,
    allowed_gcs_buckets=("my-uploads",),
)
app = graph.compile(media_policy=policy)

app.media_policy.check_url("https://cdn.example.com/a.png")  # no network; raises MediaURLError if refused

check_url needs no network, so it is cheap to call on input. The API server calls it on every client media URL before the run (see Files and multimodal on the server). Host names are resolved, and their addresses checked, only at download time.

A URL the policy refuses, or that fails to download, becomes a [Failed to load image] or [Failed to load media] text part in the prompt. The warning in the log shows the URL without its query string.

The default content types cover images and PDFs only, so audio and video links on Google graphs are not downloaded until you add those types.

gs:// URLs

On Vertex AI, a gs:// URL is read with the server’s own credentials, not the client’s. A client could point it at any bucket the server can read. For that reason the API rejects a client gs:// URL unless its bucket is in allowed_gcs_buckets. The converters pass gs:// through unchanged, and your own code may still use any gs:// URL, because check_url is the only place the list is enforced.

Example 2: Inline base64 for small media

Embed bytes directly in the message for small payloads:

Python
import base64
from tenxgraph.core.state import Message, TextBlock, ImageBlock, MediaRef

# Read a local image
with open("photo.jpg", "rb") as f:
    image_bytes = f.read()

b64 = base64.b64encode(image_bytes).decode()

messages = [
    Message(
        role="user",
        content=[
            TextBlock(text="Describe this photo in one sentence."),
            ImageBlock(
                media=MediaRef(
                    kind="data",
                    data_base64=b64,
                    mime_type="image/jpeg",
                )
            ),
        ],
    )
]

result = app.invoke({"messages": messages})

This works well for development and testing. For production or repeated references, use a media store instead.

Example 3: Media store for reusable references

Store media once, reference it many times:

Python
import asyncio
from tenxgraph.core.state import Message, TextBlock, ImageBlock, MediaRef
from tenxgraph.storage.media import InMemoryMediaStore

media_store = InMemoryMediaStore()

# Bind the store to the graph so graph://media/ references can be resolved
app = graph.compile(media_store=media_store)

# Upload and get a storage key
async def setup():
    with open("chart.png", "rb") as f:
        key = await media_store.store(data=f.read(), mime_type="image/png")
    return key

key = asyncio.run(setup())

# Reference by key in a message (builds a graph://media/{key} URL)
messages = [
    Message(
        role="user",
        content=[
            TextBlock(text="Analyze this chart and extract the numbers."),
            ImageBlock(media=media_store.to_media_ref(key, "image/png")),
        ],
    )
]

result = app.invoke({"messages": messages})

The store returns an opaque key that any later message can reference without uploading the file again.

Audio and documents

Audio blocks work the same way as images:

Python
import base64
from tenxgraph.core.state import AudioBlock

with open("clip.wav", "rb") as f:
    audio_bytes = f.read()

messages = [
    Message(
        role="user",
        content=[
            TextBlock(text="Transcribe this audio."),
            AudioBlock(
                media=MediaRef(
                    kind="data",
                    data_base64=base64.b64encode(audio_bytes).decode(),
                    mime_type="audio/wav",
                ),
                sample_rate=16000,
                channels=1,
            ),
        ],
    )
]

Document blocks can include both extracted text and the raw file. Most models take text, so provide the text when you have it:

Python
from tenxgraph.core.state import DocumentBlock

messages = [
    Message(
        role="user",
        content=[
            TextBlock(text="Summarize this contract."),
            DocumentBlock(
                text="Optional: pre-extracted text if you have it already.",
                media=MediaRef(
                    kind="url",
                    url="https://example.com/contract.pdf",
                    mime_type="application/pdf",
                ),
            ),
        ],
    )
]

Configure media with MultimodalConfig

Pass MultimodalConfig to Agent to enable media for that agent. Agents without it strip media blocks, which keeps text-only agents in a multi-agent graph from receiving images added by an earlier agent.

Python
from tenxgraph.core.graph import Agent
from tenxgraph.storage.media import MultimodalConfig

agent = Agent(
    model="gpt-4o",
    provider="openai",
    multimodal_config=MultimodalConfig(
        max_image_size_mb=10.0,
        max_image_dimension=2048,
        supported_image_types={"image/jpeg", "image/png", "image/webp"},
    ),
)

Fields and defaults are in MultimodalConfig.

The size, dimension and type limits are enforced by MediaProcessor (tenxgraph.storage.media), which you call yourself to validate or resize images before sending. The Agent does not apply them automatically. Document text extraction is not part of the core library; it lives in the API server, so pass pre-extracted text in DocumentBlock(text=...) when you need it.

Media store backends

Use InMemoryMediaStore in tests, LocalFileMediaStore(base_dir="./media") on one server, and CloudMediaStore when several workers must read the same files. See MediaStore. CloudMediaStore needs the cloud-storage extra:

Terminal
pip install "10xgraph[cloud-storage]"
Python
from cloud_storage_manager import CloudStorageFactory, StorageProvider, StorageConfig, AwsConfig
from tenxgraph.storage.media import CloudMediaStore

config = StorageConfig(
    aws=AwsConfig(bucket_name="my-bucket", access_key_id="...", secret_access_key="...")
)
storage = CloudStorageFactory.get_storage(StorageProvider.AWS, config)
store = CloudMediaStore(storage, prefix="10xgraph-media")

All stores expose the same async interface: store(), retrieve(), delete(), exists(), and get_metadata(), plus to_media_ref() to build a message reference.

Pitfalls

Provide MIME types. Always include mime_type in MediaRef. The provider needs it to interpret the bytes correctly.

Size limits are configurable. When running behind the API server, check MEDIA_MAX_SIZE_MB and MEDIA_ALLOWED_CONTENT_TYPES environment variables. For Python graphs, MultimodalConfig.max_image_size_mb is enforced only when you use MediaProcessor.

Run it in production

The server accepts uploads through its file API and hands your graph references. Choose the storage backend, size limits and signed URL settings there, not in the graph. See Files and multimodal and the Security model.

Frequently asked questions

Do I need a media store for production?
For single-server setups, LocalFileMediaStore works. For multi-worker or cloud deployments, use CloudMediaStore (S3/GCS) so all workers can access the same files.
Can I send media over the REST API?
Yes. Upload via POST /v1/files/upload, get a file_id, and reference it in your message. That flow is separate from Python; see /docs/server/files-and-multimodal.
Last updated for v1.0.0Edit this page on GitHubReport an issue