Sub topic 2
Mixing Text and Images in One Message
This chapter assumes you can call an LLM chat API through an SDK or raw HTTP, read JSON request bodies, and read and write files in Python. You should also know roughly what a token is. Here you will learn how a single message can carry both text and images.
From a string to a list of blocks
In a plain chat request, a message's content is a string. A multimodal request changes content into a list. Each item in the list is a content block, an object with a type field that says what the block holds.
- A
textblock holds words. - An
imageblock holds a picture, or a reference to one. - A
documentblock holds a file such as a PDF. A later chapter covers it.
The model reads the blocks in order and treats them as one user turn. Image blocks sit next to text blocks, and the model answers with both in view.
The shape of a message
The exact field names depend on the provider. The idea is the same everywhere: a role, then a list of typed blocks. The Anthropic Messages API uses this shape:
message_content = [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "<base64 text here>",
},
},
{"type": "text", "text": "Describe the trend shown in this chart."},
]
user_message = {"role": "user", "content": message_content}
print(user_message["content"][1]["text"])The source object says how the image arrives. Here it is base64, so media_type and data come with it. A later chapter compares base64, URLs and uploaded files.
Same idea, different names
Providers use different block names, so check each provider's documentation.
| Provider API | Text block type | Image block type | How the image is given |
|---|---|---|---|
| Anthropic Messages | `text` | `image` | `source` object with base64 data or a URL |
| OpenAI Responses | `input_text` | `input_image` | `image_url` string, either a URL or a data URL |
Your code can hide these differences behind one small function that builds blocks for the provider you use. Then the rest of your program does not need to know the names.
Order matters a little
You choose where each block goes in the list. A common habit is to place the image first and the question after it, so the model sees the picture before it reads what to do with it. Both orders work, but keep your choice consistent so your results are easy to compare. A later chapter covers prompt placement with several images.
Common mistakes
- Putting an image in a string. A string
contentcan only hold text. Switch to a list of blocks as soon as you add an image. - Using the wrong type name.
imageandinput_imageare not interchangeable. The API usually rejects the wrong one with a validation error. - Leaving out the text block. An image with no question gives the model nothing to do. Always include an instruction.
- Sending an image to a text-only model. The request fails or the image is ignored. Pick a vision-capable model.
Exercise
A request fails because content is a plain string that you tried to extend with an image. What change fixes the structure, and what two kinds of block should the new list hold at minimum?
Show answer
Change content from a string to a list of content blocks. The list should hold at least one image block, which carries the picture or a reference to it, and one text block that tells the model what to do with it.