Sub topic 4
Choosing how to supply the data: base64, URL or file
This chapter assumes you can call an LLM chat API from Python and read files as bytes. You should also know what a content block is: a typed item such as text or image inside a message. Here you choose how the image or document bytes reach the model.
The three source types
Most providers accept an image or document through one of three routes.
| Source | What you send | Who fetches the bytes | Typical use |
|---|---|---|---|
| Base64 | The file encoded as text, inside the request body | Nobody. The data travels with the request. | Local files, private data, one-off calls |
| URL | A link to the file | The provider's servers | Public images already hosted online |
| Uploaded file | A file ID returned by a file upload API | The provider already holds it | The same file used many times |
Support differs by provider and by model. Some APIs accept URLs only for images, some accept file IDs only for documents, and a few lack one route entirely. Check your provider's documentation before you design around a route.
Base64
Base64 turns binary bytes into plain text so they fit in a JSON body. The text is about one third larger than the original file, so a 3 MB image adds roughly 4 MB to the request. Request size limits apply to the encoded body, not the original file.
The chapter's helper reads a file, guesses its media type from the extension, and returns the encoded text:
import base64
import mimetypes
def load_as_base64(path):
"""Return (media_type, base64_text) for a local file."""
media_type, _ = mimetypes.guess_type(path)
if media_type is None:
raise ValueError(f"Cannot determine media type for {path}")
with open(path, "rb") as f:
data = base64.standard_b64encode(f.read()).decode("utf-8")
return media_type, data
media_type, data = load_as_base64("chart.png")
print(media_type, len(data))Base64 needs no network access beyond the API call. The file never sits at a public address.
URL
With a URL, the request stays small. The provider downloads the file itself. That gives you three constraints:
- The URL must be reachable from the provider's servers. Links behind a login, a VPN or
localhostfail. - A signed link that expires can lapse before the provider fetches it.
- The file is exposed to whatever hosts it, so a URL is a poor fit for private documents.
Uploaded files
A file upload API stores the file with the provider and gives you an ID. You then reference that ID in later requests. This avoids re-encoding and re-sending the same bytes on every call. It pays off when one PDF is queried many times. Uploaded files usually have a retention period and storage limits, so read how long your provider keeps them. Later chapters cover this route in more detail.
Same image, two request shapes
Providers differ in where the source goes. The Anthropic Messages API nests it under a source object. The OpenAI Responses API takes a data URL, which is the media type and base64 text joined into one string:
media_type, data = load_as_base64("chart.png")
anthropic_block = {
"type": "image",
"source": {"type": "base64", "media_type": media_type, "data": data},
}
openai_block = {
"type": "input_image",
"image_url": f"data:{media_type};base64,{data}",
}A URL is simpler in both cases. You pass the link where the base64 data would go, using a url source type or a plain https:// value.
Choosing
Default to base64 for local or private files. Use a URL when the file is already public and you want small requests. Use an uploaded file when the same input appears in many calls.
Exercise
A 6 MB private contract PDF is stored on your laptop and you will ask five different questions about it. Which source types are reasonable, and why might you prefer one over the other?
Show answer
Base64 works because the file is local and private, but it re-sends about 8 MB of encoded data on each of the five calls. A URL is a poor fit, since the file is private and not reachable by the provider. An uploaded file ID is the better choice if your provider supports it for PDFs, because the bytes are sent once and reused.