|
| 1 | +# Multimodal Input |
| 2 | + |
| 3 | +Send images, PDFs, and audio alongside text in user messages. Each provider has a different wire format; `@threaded/ai` normalizes these into a single `ContentPart` vocabulary and translates per provider. |
| 4 | + |
| 5 | +## The `message()` Helper |
| 6 | + |
| 7 | +The easiest way to build a multimodal user message. |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { compose, model, message } from "@threaded/ai"; |
| 11 | +import { readFileSync } from "fs"; |
| 12 | + |
| 13 | +const png = readFileSync("chart.png").toString("base64"); |
| 14 | + |
| 15 | +const userMessage = message("What's in this chart?", { |
| 16 | + images: [{ kind: "base64", mediaType: "image/png", data: png }], |
| 17 | +}); |
| 18 | + |
| 19 | +const result = await compose(model({ model: "openai/gpt-4o-mini" }))({ |
| 20 | + history: [userMessage], |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +`message()` returns a `Message` whose `content` is a `ContentPart[]`. Drop it directly into `ctx.history`. |
| 25 | + |
| 26 | +### Image from URL |
| 27 | + |
| 28 | +```typescript |
| 29 | +const userMessage = message("Describe this photo.", { |
| 30 | + images: ["https://example.com/photo.jpg"], |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +A bare string in the `images` array is treated as a URL. Pass `{ kind, mediaType, data }` for base64. |
| 35 | + |
| 36 | +### Multiple Images |
| 37 | + |
| 38 | +```typescript |
| 39 | +const userMessage = message("Compare these three charts.", { |
| 40 | + images: [ |
| 41 | + { kind: "base64", mediaType: "image/png", data: pngA }, |
| 42 | + { kind: "base64", mediaType: "image/png", data: pngB }, |
| 43 | + "https://example.com/chart-c.png", |
| 44 | + ], |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +### PDF Documents |
| 49 | + |
| 50 | +```typescript |
| 51 | +const pdf = readFileSync("report.pdf").toString("base64"); |
| 52 | + |
| 53 | +const userMessage = message("Summarize this report.", { |
| 54 | + documents: [ |
| 55 | + { |
| 56 | + source: { kind: "base64", mediaType: "application/pdf", data: pdf }, |
| 57 | + filename: "report.pdf", |
| 58 | + }, |
| 59 | + ], |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +`filename` is optional but useful — OpenAI in particular uses it as a display hint when the assistant references the file. |
| 64 | + |
| 65 | +### Audio |
| 66 | + |
| 67 | +```typescript |
| 68 | +const wav = readFileSync("clip.wav").toString("base64"); |
| 69 | + |
| 70 | +const userMessage = message("Transcribe this clip.", { |
| 71 | + audio: [{ kind: "base64", mediaType: "audio/wav", data: wav }], |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +Audio input is only supported on audio-capable models (see the capability matrix below). |
| 76 | + |
| 77 | +## Content Parts Directly |
| 78 | + |
| 79 | +For finer control, build the `ContentPart[]` yourself instead of using `message()`. |
| 80 | + |
| 81 | +```typescript |
| 82 | +import { Message } from "@threaded/ai"; |
| 83 | + |
| 84 | +const userMessage: Message = { |
| 85 | + role: "user", |
| 86 | + content: [ |
| 87 | + { type: "text", text: "What's in this image?" }, |
| 88 | + { |
| 89 | + type: "image", |
| 90 | + source: { kind: "base64", mediaType: "image/png", data: png }, |
| 91 | + }, |
| 92 | + ], |
| 93 | +}; |
| 94 | +``` |
| 95 | + |
| 96 | +The four part types. |
| 97 | + |
| 98 | +```typescript |
| 99 | +type ContentPart = |
| 100 | + | { type: "text"; text: string } |
| 101 | + | { type: "image"; source: MediaSource } |
| 102 | + | { type: "document"; source: MediaSource; filename?: string } |
| 103 | + | { type: "audio"; source: MediaSource }; |
| 104 | + |
| 105 | +type MediaSource = |
| 106 | + | { kind: "base64"; mediaType: string; data: string } |
| 107 | + | { kind: "url"; url: string }; |
| 108 | +``` |
| 109 | + |
| 110 | +A message may contain any mix of text and media parts in any order. Assistant replies always come back as plain string content on the chat endpoints. |
| 111 | + |
| 112 | +## Provider Capability Matrix |
| 113 | + |
| 114 | +| Part | OpenAI | Anthropic | Google | xAI | Ollama | |
| 115 | +|----------|---------------------------------|--------------|--------------|--------------|--------| |
| 116 | +| image | all vision models | all models | all models | grok-2-vision, grok-4 | llava, llama3.2-vision, etc. | |
| 117 | +| document | vision models (base64 PDF) | all models | all models | not supported | not supported | |
| 118 | +| audio | gpt-4o-audio-preview, gpt-4o-mini-audio-preview | not supported | all models | not supported | not supported | |
| 119 | + |
| 120 | +Unsupported combinations throw a clear error at the adapter boundary rather than silently dropping content. If you attach audio to a non-audio provider, the call raises instead of the model receiving only the text. |
| 121 | + |
| 122 | +### Source Kind Compatibility |
| 123 | + |
| 124 | +Not every provider accepts every source kind for every media type. |
| 125 | + |
| 126 | +- **Images**: base64 and URL accepted on OpenAI, Anthropic, and xAI. Google accepts base64 directly and routes `kind: "url"` through its Files API (`file_data.file_uri`) — plain public URLs are not fetched by the Gemini server and should be uploaded first. |
| 127 | +- **Documents**: base64 works everywhere that supports documents. URL works on Anthropic (native) and Google (via Files API). OpenAI requires base64 — for large PDFs, upload via the Files API and reference by `file_id` in a text message. |
| 128 | +- **Audio**: base64 only, and only on providers in the matrix above. |
| 129 | + |
| 130 | +## Running the Same Prompt Across Providers |
| 131 | + |
| 132 | +Because the adapter layer hides the wire-format differences, the same `ContentPart[]` works across every multimodal provider. |
| 133 | + |
| 134 | +```typescript |
| 135 | +import { compose, model, message } from "@threaded/ai"; |
| 136 | + |
| 137 | +const userMessage = message("What color is this?", { |
| 138 | + images: [{ kind: "base64", mediaType: "image/png", data: png }], |
| 139 | +}); |
| 140 | + |
| 141 | +const providers = [ |
| 142 | + "openai/gpt-4o-mini", |
| 143 | + "anthropic/claude-sonnet-4-5", |
| 144 | + "google/gemini-2.5-flash-lite", |
| 145 | +]; |
| 146 | + |
| 147 | +for (const provider of providers) { |
| 148 | + const result = await compose(model({ model: provider }))({ |
| 149 | + history: [userMessage], |
| 150 | + }); |
| 151 | + console.log(provider, "->", result.lastResponse?.content); |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +## PDF Example |
| 156 | + |
| 157 | +```typescript |
| 158 | +import { compose, model, message } from "@threaded/ai"; |
| 159 | +import { readFileSync } from "fs"; |
| 160 | + |
| 161 | +const pdf = readFileSync("invoice.pdf").toString("base64"); |
| 162 | + |
| 163 | +const result = await compose(model({ model: "openai/gpt-4o-mini" }))({ |
| 164 | + history: [ |
| 165 | + message("Extract the line items from this invoice as JSON.", { |
| 166 | + documents: [ |
| 167 | + { |
| 168 | + source: { kind: "base64", mediaType: "application/pdf", data: pdf }, |
| 169 | + filename: "invoice.pdf", |
| 170 | + }, |
| 171 | + ], |
| 172 | + }), |
| 173 | + ], |
| 174 | +}); |
| 175 | + |
| 176 | +console.log(result.lastResponse?.content); |
| 177 | +``` |
| 178 | + |
| 179 | +Works identically against `google/gemini-2.5-flash` or `anthropic/claude-sonnet-4-5`. |
| 180 | + |
| 181 | +## Audio Example |
| 182 | + |
| 183 | +```typescript |
| 184 | +import { compose, model, message } from "@threaded/ai"; |
| 185 | +import { readFileSync } from "fs"; |
| 186 | + |
| 187 | +const wav = readFileSync("meeting.wav").toString("base64"); |
| 188 | + |
| 189 | +const result = await compose( |
| 190 | + model({ model: "openai/gpt-4o-audio-preview" }), |
| 191 | +)({ |
| 192 | + history: [ |
| 193 | + message("Transcribe this meeting and list the action items.", { |
| 194 | + audio: [{ kind: "base64", mediaType: "audio/wav", data: wav }], |
| 195 | + }), |
| 196 | + ], |
| 197 | +}); |
| 198 | +``` |
| 199 | + |
| 200 | +When a message contains audio, the OpenAI adapter automatically adds `modalities: ["text"]` to the request body. Without that, the audio-preview models refuse to describe the input. |
| 201 | + |
| 202 | +Supported audio formats on OpenAI: `audio/wav`, `audio/mp3`. Gemini accepts `audio/wav`, `audio/mpeg` (mp3), `audio/aiff`, `audio/aac`, `audio/ogg`, and `audio/flac`. |
| 203 | + |
| 204 | +## Mixing Media and Tools |
| 205 | + |
| 206 | +Multimodal input composes with tool execution the same way text does. |
| 207 | + |
| 208 | +```typescript |
| 209 | +import { compose, scope, model, message } from "@threaded/ai"; |
| 210 | + |
| 211 | +const saveNote = { |
| 212 | + name: "save_note", |
| 213 | + description: "Save a note to the database", |
| 214 | + schema: { text: { type: "string", description: "The note content" } }, |
| 215 | + execute: async ({ text }) => ({ ok: true, text }), |
| 216 | +}; |
| 217 | + |
| 218 | +const result = await compose( |
| 219 | + scope({ tools: [saveNote] }, model({ model: "openai/gpt-4o-mini" })), |
| 220 | +)({ |
| 221 | + history: [ |
| 222 | + message("Read the sticky note in this photo, then save it.", { |
| 223 | + images: [{ kind: "base64", mediaType: "image/jpeg", data: jpeg }], |
| 224 | + }), |
| 225 | + ], |
| 226 | +}); |
| 227 | +``` |
| 228 | + |
| 229 | +## Threads |
| 230 | + |
| 231 | +Persistent threads work transparently with multimodal content — `thread.message()` only takes a string today, so use `thread.generate()` to push a pre-built multimodal message into history. |
| 232 | + |
| 233 | +```typescript |
| 234 | +import { getOrCreateThread, compose, model, message } from "@threaded/ai"; |
| 235 | + |
| 236 | +const thread = getOrCreateThread("user-42"); |
| 237 | + |
| 238 | +await thread.generate(async (ctx) => ({ |
| 239 | + ...ctx, |
| 240 | + history: [ |
| 241 | + ...ctx.history, |
| 242 | + message("What's in this chart?", { |
| 243 | + images: [{ kind: "base64", mediaType: "image/png", data: png }], |
| 244 | + }), |
| 245 | + ], |
| 246 | +})); |
| 247 | + |
| 248 | +await thread.generate(compose(model({ model: "openai/gpt-4o-mini" }))); |
| 249 | +``` |
| 250 | + |
| 251 | +History is persisted via the thread's store — the `ContentPart[]` survives JSON serialization cleanly. |
| 252 | + |
| 253 | +## Error Handling |
| 254 | + |
| 255 | +Unsupported combinations throw at call time, not later. |
| 256 | + |
| 257 | +```typescript |
| 258 | +try { |
| 259 | + await compose(model({ model: "xai/grok-4" }))({ |
| 260 | + history: [ |
| 261 | + message("What does this say?", { |
| 262 | + documents: [ |
| 263 | + { source: { kind: "base64", mediaType: "application/pdf", data: pdf } }, |
| 264 | + ], |
| 265 | + }), |
| 266 | + ], |
| 267 | + }); |
| 268 | +} catch (err) { |
| 269 | + // "xAI does not support document/PDF input on the chat completions API" |
| 270 | +} |
| 271 | +``` |
| 272 | + |
| 273 | +Catch at the composition boundary and retry against a provider that supports the media type. |
0 commit comments