Multimodal content: images and documents
Send images and documents to a model in a user message or a tool result, and know before the request goes out whether the provider can take them.
Table of contents
- Content parts
- Why inline media is bounded
- What each provider sends
- Tools that return media
- Media from the user
- Stored conversations
Content parts
A UserMessage and a ToolMessage carry parts: Seq[ContentPart]. A part is one of:
ContentPart.Text(text);- an image, built with
ContentPart.image(source, mediaType),mediaTypeanImageMediaType(Png,Jpeg,Gif,WebP,Bmp,Tiff); - a document, built with
ContentPart.document(source, mediaType),mediaTypeaDocumentMediaType(Pdf,PlainText,Markdown,Html,Csv).
Those media types, and MediaSource, are llm4s-media’s shared vocabulary, which image generation and RAG
extraction use too: Media Types describes it.
The source, an org.llm4s.media.MediaSource, says where the bytes are:
| Source | Meaning |
|---|---|
MediaSource.Url(uri) |
The provider fetches it. Must be absolute; a data: URL is refused, use Inline. |
MediaSource.Inline(bytes) |
The bytes themselves, at most ContentPart.MaxInlineBytes (5 MiB). |
MediaSource.ProviderFile(provider, id) |
A file already uploaded to provider (openai, gemini), by its id. |
1
2
3
4
5
6
7
8
9
10
import org.llm4s.llmconnect.model.*
import org.llm4s.media.{ MediaSource, MediaType }
import java.net.URI
import java.nio.file.{ Files, Path }
for {
chart <- ContentPart.image(MediaSource.Url(URI.create("https://example.com/q3.png")), MediaType.Png)
report <- ContentPart.document(MediaSource.Inline(IArray.from(Files.readAllBytes(Path.of("q3.pdf")))), MediaType.Pdf)
answer <- client.complete(Conversation(Seq(UserMessage(Vector(ContentPart.Text("Summarise these."), chart, report)))))
} yield answer.content
UserMessage(text) and ToolMessage(text, toolCallId) build the one-text-part message. content is a
message’s text view: its text parts joined by newlines, "" for a message that is all media.
Why inline media is bounded
ContentPart.image and ContentPart.document return Left(ValidationError) for inline bytes over 5 MiB, and a
part read from JSON is checked the same way. An agent thread’s every checkpoint is a full snapshot of its state, so each
retained checkpoint keeps a copy of every inline part - base64-encoded, a third larger - for as long as the message
stays in the thread. Put larger media at a URL, or upload it to the provider and send a ProviderFile; both are held
by reference.
What each provider sends
A client maps every part its API takes and fails the request, before sending anything, with an
UnsupportedContentPartError naming the provider, the part (ContentPart.kind, e.g. image (url, image/png)) and
the role. No part is dropped and none is turned into text. A ProviderFile goes only to the provider it names.
| Provider | User message | Tool result |
|---|---|---|
| OpenAI, Azure OpenAI, Requesty | text; image Url, Inline; PDF Inline, own ProviderFile |
text |
| OpenAI-compatible (generic, Z.ai, OpenRouter, Mistral, Cohere, xAI) | text; image Url, Inline |
text |
| OpenRouter, in addition | PDF Url, Inline |
|
| Mistral, in addition | PDF Url |
|
| DeepSeek | text | text |
| Anthropic | text; image (PNG, JPEG, GIF, WebP) Url, Inline; PDF Url, Inline; UTF-8 plain text Inline |
the same |
| Gemini API | text; image and document Inline, own ProviderFile (its Files API uri, or its name files/abc) |
text |
| Vertex AI | text; image and document Inline or a gs:// Url |
text |
| Ollama | text; image Inline |
text |
| Bedrock | text; image (PNG, JPEG, GIF, WebP) and document (all five) Inline or an s3:// Url |
the same |
| watsonx.ai | text; image Inline |
text |
| Perplexity | text | text |
| Jlama | text | text |
A Url sent to OpenAI, an OpenAI-compatible provider or Anthropic must be http or https; the Gemini API fetches no web
URL, so upload the file or send it inline.
“Own ProviderFile” means a file whose provider is the client’s own id (openai, azure, requesty, gemini;
Vertex AI takes none). Several text parts with no media are sent as one string, joined by newlines, by the OpenAI-compatible
providers and Ollama.
llm4s refuses before sending only what would otherwise go silently wrong: a part a provider cannot send at all, a reference it cannot resolve, and llm4s’s own bounds. A provider’s own limits - how many images, how large a request, which formats a model reads - are the provider’s to enforce: its refusal comes back as a typed error with its message.
An agent thread holds at most 20 MiB of inline media across its messages (Messages.MaxInlineBytes). When a message
would take it past that, the oldest inline media in the thread is replaced first, message by message, by a text
part saying what was removed, and the new message’s own media only if that is not enough, so a screenshot or vision
loop keeps the image it is looking at and the run goes on. Each eviction is logged at WARN with the message ids and
the bytes freed. Evicted media is gone for good: an output guardrail’s block that retracts the turn does not restore it.
Tools that return media
A tool returns parts with ToolContent.Parts:
1
2
3
4
5
6
7
8
import org.llm4s.agent.graph.tool.{ AgentTool, ToolContent, ToolOutcome }
val plot = AgentTool(plotSpec) { (args, _) =>
ContentPart.image(MediaSource.Inline(IArray.from(render(args))), MediaType.Png) match {
case Right(image) => ToolOutcome.success(ToolContent.Parts(Vector(ContentPart.Text("The plot:"), image)))
case Left(error) => ToolOutcome.Error(error.message)
}
}
ToolContent.Json(value) is a plain JSON result. The loop puts Parts in the call’s
ToolMessage unchanged. A result with no content, or with more than 5 MiB of inline bytes in total, is the call’s
error result instead, so the model is told and the run continues. The live ToolCallResult event carries the text
view only.
On a provider whose tool results take text only (OpenAI, Azure, the OpenAI-compatible providers, Gemini, Vertex AI,
Ollama and watsonx; see the table above), the provider refuses the media before anything is sent, and the model call
is made again with the tool results reduced to their text - each media part named in place - followed by one user
turn holding their media, labelled by tool call. The stored thread is unchanged, so the thread is never wedged by it
and recover works. That user turn takes what the provider’s user messages take: a Url image on Ollama, which
takes inline images only, still fails the call.
Media from the user
A turn’s user message can carry images and documents on any thread, new or existing:
1
2
3
4
5
6
7
8
9
import org.llm4s.agent.Agent
import org.llm4s.agent.graph.{ RunConfig, ThreadId }
import org.llm4s.llmconnect.model.{ ContentPart, UserMessage }
import org.llm4s.types.Result
def askAboutScreenshot(agent: Agent, screenshot: ContentPart.Image): Result[Option[String]] =
agent
.run(ThreadId("support-42"), UserMessage(Vector(ContentPart.Text("What is wrong here?"), screenshot)), RunConfig())
.map(_.answer)
run, start, stream and continueConversation each have an overload taking a UserMessage. Its text parts,
joined by newlines, are the query that admission and the input guardrails see. Its other parts are stored after
that text, in order, and are bounded with the thread’s other inline media. A message with no text (media alone, or
blank text) is a ValidationError, since the text is what the guardrails judge.
Stored conversations
A message whose JSON has a plain text content and no parts reads as one text part, so stored conversations, memory
entries and agent threads written that way read back unchanged. The semantic cache (CachingLLMClient)
embeds a prompt’s text, so it passes a conversation with media straight to the model.