Multimodal Input
🎯 Introduction
So far, we have sent text to models through messages. In an application, users may also want AI to describe a product image or read an after-sales policy PDF and extract its key information.
These are examples of multimodal input: the model receives images, documents, or other data alongside text. Continuing with AIHubMix + the OpenAI SDK + Chat Completions, we will work through image understanding and PDF analysis, focusing on request structure and verifying that answers reflect the supplied material. Image generation, audio, and knowledge base retrieval are outside this article's scope.
🧱 How Do Multimodal Messages Differ from Plain Text?
In earlier text requests, content was a string:
{
role: "user",
content: "Describe this product."
}
To answer using an image, the model also needs the image data. Chat Completions lets you write content as an array of content blocks:
{
role: "user",
content: [
{ type: "text", text: "Describe this product." },
{ type: "image_url", image_url: { url: "image URL or data URL" } }
]
}
Both blocks belong to one user message: the text specifies the task, and the image supplies the subject. The program still makes a single model request; there is no need to call an "image API" followed by a "text API".
This article uses three content block types:
type | Purpose | Example in this article |
|---|---|---|
text | Specify the task | Describe an image or extract document highlights |
image_url | Provide an image URL or image data | Local product.jpg |
file | Provide supported file data | Local notice.pdf |
The SDK's ability to construct a request does not mean every model can process all content types. Both the model and AIHubMix's endpoint must support the content. OpenAI's Chat Completions file blocks currently support PDF; do not assume this file pattern works for other ordinary files.
Use a Model That Supports Images and PDFs
Continue with the first article's ai-sdk-course project using Node.js 24.x and openai@7.62.0. You do not need to reinstall the SDK.
Your .env may still specify gpt-4o-mini. AIHubMix's current model information explicitly lists image input for it, but PDF support cannot be inferred from its name. To use one model for both examples below, first change the model setting in .env to:
AI_MODEL=gpt-4.1-mini
Keep AI_API_KEY and AI_BASE_URL unchanged. AIHubMix's model directory lists image and PDF input for gpt-4.1-mini. However, a PDF support label does not verify that a Chat Completions file request will succeed for your account. If the platform reports unsupported input, consult the actual model details, API documentation, and response rather than consuming credit through repeated retries.
🛠 Ask the Model to Describe a Product Image
Place product.jpg in the project root. Choose a clear product photo without private information, such as an insulated bottle on a white background, so you can compare the answer with the image and check for invented details.
Create 07.image.mjs:
import OpenAI from "openai";
import { readFile } from "node:fs/promises";
const client = new OpenAI({
apiKey: process.env.AI_API_KEY,
baseURL: process.env.AI_BASE_URL
});
const image = await readFile("./product.jpg");
const dataUrl = `data:image/jpeg;base64,${image.toString("base64")}`;
const response = await client.chat.completions.create({
model: process.env.AI_MODEL,
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Objectively describe what is visible in this product image. Do not guess the price, materials, or details that are not clearly visible."
},
{
type: "image_url",
image_url: { url: dataUrl }
}
]
}
]
});
console.log("Image description:", response.choices[0].message.content);
Run it using the proxy command verified earlier:
HTTPS_PROXY=http://127.0.0.1:7890 NODE_USE_ENV_PROXY=1 node --env-file=.env 07.image.mjs
For a photo of a silver insulated bottle, the terminal might display:
Image description: The image shows a silver bottle with a lid against a light background. No price information is clearly visible.
This is an illustration written for a hypothetical image, not a real API result. Your answer depends on your image and model.
Why Convert a Local Image to a Data URL?
The model service cannot read ./product.jpg on your computer. The example reads the file contents and encodes them as a data URL:
data:image/jpeg;base64,<Base64-encoded image contents>
image/jpeg specifies the image format, and the Base64 portion contains its data. Despite the name image_url.url, this example passes a data URL containing the image itself, rather than a local path or public web link.
This example uses JPEG. For PNG, update both the filename and prefix to data:image/png;base64,. Changing the extension alone does not change the actual file format.
The key addition to the earlier text requests is placing both text and image_url blocks in content, allowing the model to analyze an image in light of the question. It can recognize visible content, but may still misread small text, miss obscured details, or infer product properties incorrectly.
🛠 Extract Information from a PDF
Image understanding answers questions about pictures. PDF input lets the model read user-supplied documents. We continue using Chat Completions here without switching to the Responses API.
Prepare a PDF You Can Check Against
Place notice.pdf in the project root. Use the sample supplied with the course, or paste the following into a document editor and export it as a PDF:
Product After-Sales Policy (Teaching Example)
- Eligible products may be returned within 7 days of receipt.
- Quality defects in the main product are covered by a 12-month warranty.
- An order number or proof of purchase is required for after-sales service.
This is fictional exercise material, not a real merchant's policy. Fixed content lets you check each extracted point against the source.
Create 07.file.mjs:
import OpenAI from "openai";
import { readFile } from "node:fs/promises";
const client = new OpenAI({
apiKey: process.env.AI_API_KEY,
baseURL: process.env.AI_BASE_URL
});
const pdf = await readFile("./notice.pdf");
const fileData = `data:application/pdf;base64,${pdf.toString("base64")}`;
const response = await client.chat.completions.create({
model: process.env.AI_MODEL,
messages: [
{
role: "user",
content: [
{
type: "text",
text: "List three key points from this after-sales policy. Extract only information explicitly stated in the document."
},
{
type: "file",
file: {
filename: "notice.pdf",
file_data: fileData
}
}
]
}
]
});
console.log("Document highlights:", response.choices[0].message.content);
Run:
HTTPS_PROXY=http://127.0.0.1:7890 NODE_USE_ENV_PROXY=1 node --env-file=.env 07.file.mjs
If the selected model and AIHubMix endpoint support this PDF input format, the model might return:
Document highlights:
1. Eligible products may be returned within 7 days of receipt.
2. Quality defects in the main product are covered by a 12-month warranty.
3. An order number or proof of purchase is required for after-sales service.
This is illustrative expected output for the sample PDF, not an actual request result. Compare each point with the PDF's three rules and check for omissions or added conditions.
What Does a file Content Block Contain?
The main difference from the image example is the file block:
{
type: "file",
file: {
filename: "notice.pdf",
file_data: fileData
}
}
filename supplies the file's name, while file_data carries its actual contents. data:application/pdf;base64, identifies the data as a PDF.
Sending a filename does not send the file: the model analyzes file_data. Supported models and endpoints may use both extracted text and page images to understand PDFs, especially documents with layouts or tables, which can also increase input usage.
type: "text". You do not need to generate or upload a PDF first.⚠️ Limitations of Multimodal Input
| What to check | Why it matters |
|---|---|
| Model and endpoint | Image support does not imply PDF support; confirm that the aggregation platform forwards file blocks |
| Input format and size | The MIME type must match the file contents; follow the endpoint's format, size, and page limits |
| Result reliability | Models may miss image details, misread tables, or extract information absent from the source; verify original materials for important business uses |
| Privacy and costs | Files are sent to a third-party API; image and PDF costs cannot be estimated solely from visible text length |
If a PDF request reports unsupported file input, check the model details and API error first. For endpoints without direct PDF support, Node.js can extract the PDF text and send it as a normal text message. This does not automatically preserve images or complex layouts. It is a different approach, and PDF parsing code is outside this article's scope.
This article only covers using an image or document as input to the current request. Later RAG lessons cover bulk document ingestion, chunking, and vector retrieval; there is no need to implement a knowledge base here.
❓ Knowledge Check
Q1: Why can't I send ./product.jpg or ./notice.pdf directly to the model?
These are local paths on your computer, inaccessible to a remote API. Here we include file contents in the request through data URLs. When supported, you can also use publicly accessible file URLs or a provider's upload capabilities, but support differs between content types.
Q2: Why might type: "file" fail even with a PDF-capable model?
Support also depends on the API protocol and AIHubMix's handling of that block type. PDF support in a model does not mean every compatible endpoint accepts the same fields. Use the actual error to identify the supported integration method.
🧪 Exercise
- Image understanding: Choose a product image without private information. Ask "What objects are visible?" and "Does the image explicitly show a price?" Check whether the model distinguishes visible facts from guesses.
- PDF analysis: Change the warranty period in
notice.pdf, for example from 12 months to 6, and export it again. Rerun07.file.mjsand check whether the model reads the new period accurately instead of repeating the old one.
Success criteria: Construct text + image_url and text + file messages independently, make requests with real files, and verify answers against the originals. If PDF blocks are unsupported, diagnose model and endpoint compatibility rather than mistaking an API error for incorrect PDF content.
🎉 You now know the basics of sending images and PDFs through the OpenAI SDK. Next, you will build an AI service API with Express.
