Stream Object with Image Prompt
Learn how to stream structured data with an image prompt using the AI TOOLKIT and Node
Some language models that support vision capabilities accept images as part of the prompt. Here are some of the different formats you can use to include images as input.
Run it locally
$ npm install aiURL
import { streamObject } from 'ai-toolkit';
import dotenv from 'dotenv';
import { z } from 'zod';
dotenv.config();
async function main() {
const { partialObjectStream } = streamObject({
model: 'openai/gpt-4.1',
maxOutputTokens: 512,
schema: z.object({
stamps: z.array(
z.object({
country: z.string(),
date: z.string(),
}),
),
}),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'list all the stamps in these passport pages?',
},
{
type: 'image',
image: new URL(
'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/WW2_Spanish_official_passport.jpg/1498px-WW2_Spanish_official_passport.jpg',
),
},
],
},
],
});
for await (const partialObject of partialObjectStream) {
console.clear();
console.log(partialObject);
}
}
main();
- 1import { streamObject } from 'ai-toolkit';
- 2import dotenv from 'dotenv';
- 3import { z } from 'zod';
- 4dotenv.config();
- 5async function main() {
- 6 const { partialObjectStream } = streamObject({
- 7 model: 'openai/gpt-4.1',
- 8 maxOutputTokens: 512,
- 9 schema: z.object({
- 10 stamps: z.array(
- 11 z.object({
- 12 country: z.string(),
- 13 date: z.string(),
- 14 }),
- 15 ),
- 16 }),
- 17 messages: [
- 18 {
- 19 role: 'user',
- 20 content: [
- 21 {
- 22 type: 'text',
- 23 text: 'list all the stamps in these passport pages?',
- 24 },
- 25 {
- 26 type: 'image',
- 27 image: new URL(
- 28 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/WW2_Spanish_official_passport.jpg/1498px-WW2_Spanish_official_passport.jpg',
- 29 ),
- 30 },
- 31 ],
- 32 },
- 33 ],
- 34 });
- 35 for await (const partialObject of partialObjectStream) {
- 36 console.clear();
- 37 console.log(partialObject);
- 38 }
- 39}
- 40main();
File Buffer
import { streamObject } from 'ai-toolkit';
import dotenv from 'dotenv';
import { z } from 'zod';
import fs from 'fs';
dotenv.config();
async function main() {
const { partialObjectStream } = streamObject({
model: 'openai/gpt-4.1',
maxOutputTokens: 512,
schema: z.object({
stamps: z.array(
z.object({
country: z.string(),
date: z.string(),
}),
),
}),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'list all the stamps in these passport pages?',
},
{
type: 'image',
image: fs.readFileSync('./data/passport.png', {
encoding: 'base64',
}),
},
],
},
],
});
for await (const partialObject of partialObjectStream) {
console.clear();
console.log(partialObject);
}
}
main();
- 1import { streamObject } from 'ai-toolkit';
- 2import dotenv from 'dotenv';
- 3import { z } from 'zod';
- 4import fs from 'fs';
- 5dotenv.config();
- 6async function main() {
- 7 const { partialObjectStream } = streamObject({
- 8 model: 'openai/gpt-4.1',
- 9 maxOutputTokens: 512,
- 10 schema: z.object({
- 11 stamps: z.array(
- 12 z.object({
- 13 country: z.string(),
- 14 date: z.string(),
- 15 }),
- 16 ),
- 17 }),
- 18 messages: [
- 19 {
- 20 role: 'user',
- 21 content: [
- 22 {
- 23 type: 'text',
- 24 text: 'list all the stamps in these passport pages?',
- 25 },
- 26 {
- 27 type: 'image',
- 28 image: fs.readFileSync('./data/passport.png', {
- 29 encoding: 'base64',
- 30 }),
- 31 },
- 32 ],
- 33 },
- 34 ],
- 35 });
- 36 for await (const partialObject of partialObjectStream) {
- 37 console.clear();
- 38 console.log(partialObject);
- 39 }
- 40}
- 41main();