Learn how to stream text with an image prompt using the AI SDK and Next.js
Vision models such as GPT-4o can process both text and images. In this example, we will show you how to send an image URL along with the user's message to the model with useChat.
The server route uses convertToModelMessages to handle the conversion from UIMessages to model messages, which automatically handles multimodal content including images.
app/api/chat/route.ts
import { streamText } from 'ai';export const maxDuration = 60;export async function POST(req: Request) { const { messages } = await req.json(); // Call the language model const result = streamText({ model: 'openai/gpt-4.1', messages: convertToModelMessages(messages), }); // Respond with the stream return result.toUIMessageStreamResponse();}