Many models can read images, not just text. A customer could send a photo of a book cover, and ask, do you have this? In LangChain, a message can hold text and images together.
To keep things self contained, we draw a simple book cover with the Pillow library, and turn it into base sixty four text. The message content is now a list of blocks. A text block with the question, and an image block with the picture. Run it. The model reads the title and the price, straight from the image.
image_input.pypython
import base64, iofrom PIL import Image, ImageDraw, ImageFontfrom langchain.chat_models import init_chat_modelcover = Image.new("RGB", (300, 420), "#1d3557")draw, font = ImageDraw.Draw(cover), ImageFont.load_default(size=36)draw.text((30, 80), "THE SILENT\nRIVER", fill="white", font=font)draw.text((30, 330), "£8.99", fill="#ffd166", font=font)buffer = io.BytesIO()cover.save(buffer, format="PNG")data = base64.b64encode(buffer.getvalue()).decode()model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)message = {"role": "user", "content": [ {"type": "text", "text": "What are the title and price on this book cover? One line."}, {"type": "image", "base64": data, "mime_type": "image/png"},]}print(model.invoke([message]).text)
Output
The title is "THE SILENT RIVER" and the price is 8.99.
Reading images opens up new features. Identify a book from a photo of its cover. Read a receipt for a return. Check a photo of a damaged parcel. Or describe a picture for a customer who can't see it. Images also cost tokens, so resize big photos first.
Messages can hold text and image blocks together, so the model can answer questions about pictures. Next, summarising documents too long for one prompt.