Media
Media is how you put an image, a file such as a PDF, or a recording in a prompt. Each is one more part of a user turn, next to the text, and each provider rearranges it into the shape its API expects. Media goes in only. The model returns text.
An image in a user turn
A message holds a list of parts. Message::new takes a role and the parts, and InputContent::ImageUrl carries an image as a URL.
use freyja::{Agent, InputContent, Message, Role};
let message = Message::new(
Role::User,
[
InputContent::ImageUrl("https://example.com/photo.jpg".into()),
InputContent::Text("What shape and colour is this? Answer in five words.".into()),
],
);
Put the image before the text it is asked about. Order is kept, so a question placed first refers to something the model has not seen yet.
Sending it through Agent::ask
Agent::ask takes anything that converts into a Message, so the message above goes in as it is.
let agent = Agent::new(client);
let run = agent.ask(message).await?;
println!("{}", run.answer);
This calls a real model, so it needs a key and the network, and the model must accept images. A conversation takes the same message through send, and the image is stored in the transcript with the rest of the turn. See Messages and content for the part types.
Images from bytes
InputContent::image takes the bytes of an image and its media type, and sends them inside the request.
let bytes = std::fs::read("receipt.png")?;
let image = InputContent::image(bytes, "image/png");
It builds a data: URL, data:image/png;base64,..., so InputContent::ImageUrl with a URL of that form you made yourself does the same. A malformed data URL on Anthropic fails locally with InvalidRequest. See Anthropic.
Which turns may hold images
Images belong on user turns. Everywhere else the answer depends on the provider, and Freyja checks before the network.
| Provider | Turns that may carry an image |
|---|---|
| OpenAI Chat Completions | any role |
| Gemini | user and assistant, and a tool turn is InvalidRequest |
| OpenAI | user only |
| Anthropic | user only |
Where a provider will not carry an image, Freyja returns Error::UnsupportedCapability with the capability images outside user messages, and sends no request. On the providers that lift system turns into a text-only field, an image there is refused as non-text content in system/developer messages instead. Either way the image is never dropped silently, so the answer is never one that ignored it. Keeping images on user turns keeps a transcript portable.
Files
A file the model reads, such as a contract or a report, goes in the same way. InputContent::file takes the bytes and the media type.
let bytes = std::fs::read("contract.pdf")?;
let message = Message::new(
Role::User,
[
InputContent::file(bytes, "application/pdf"),
InputContent::Text("What is the notice period?".into()),
],
);
let run = agent.ask(message).await?;
A file online goes in as a link, and the provider downloads it itself, so the request stays small.
let file = InputContent::FileUrl("https://example.com/contract.pdf".into());
PDF is the type every provider reads. The others vary, and a provider can accept a file it then ignores, so the model answers as though nothing was attached. Check a new type with one short question before relying on it.
| Provider | Types read | A link | Turns that may carry a file |
|---|---|---|---|
| OpenAI | yes | user only | |
| Anthropic | PDF and plain text | yes | any |
| Gemini | yes | user and assistant | |
| OpenAI Chat Completions | PDF on OpenAI, and most local runtimes take none | no, UnsupportedCapability | any |
Freyja sends the media type as given and leaves the answer to the provider. A type it will not take comes back as the provider's own error. A file on a turn the provider refuses is UnsupportedCapability with the capability files outside user messages, before any request is sent.
Audio
A recording goes in from its bytes and media type, and the model listens to it.
let clip = std::fs::read("question.wav")?;
let message = Message::new(
Role::User,
[
InputContent::audio(clip, "audio/wav"),
InputContent::Text("Answer the question in this recording.".into()),
],
);
WAV and MP3, audio/wav and audio/mpeg, are the types both providers that listen will take. The model has to accept audio too, which on OpenAI means an audio model such as gpt-audio-mini.
| Provider | Audio | A link | Turns that may carry audio |
|---|---|---|---|
| Gemini | yes | yes | user and assistant |
| OpenAI Chat Completions | on OpenAI's audio models | no, UnsupportedCapability | user only |
| OpenAI | sent, and OpenAI answers Audio input is not available | no, UnsupportedCapability | user only |
| Anthropic | no, UnsupportedCapability with audio input | no | none |
Use Chat Completions to send audio to OpenAI. A refusal is raised before any request is sent, so a recording is never dropped silently.
Testing without a key
A scripted model shows whether a part reached the model, and Client::check shows whether a provider refuses it, both without a request. See Testing media.
What an image costs
Images, files and audio are billed as input tokens, and a large one can cost far more than the sentence beside it. Run::usage reports the input tokens after the run, and the count is the one the provider charged, so it includes the image. A data: URL adds a third to the bytes of the file, because base64 turns three bytes into four, and the whole URL travels in every request that carries the turn. A conversation resends its transcript each turn, so an image sent once is sent and billed again on each later turn, unless the memory drops it. Prefer a hosted URL for anything large or reused. See Responses for usage.
See also
- Messages and content for roles, parts and the validation done before the network
- Providers overview for what each provider carries
- Structured output for asking a model about an image and getting typed JSON back
- Conversations for sending a transcript that holds images
- Testing for checking the request an image produces without a key