📄 ai-sdk/cookbook/next/generate-object-with-file-prompt

File: generate-object-with-file-prompt.md | Updated: 11/15/2025

Source: https://ai-sdk.dev/cookbook/next/generate-object-with-file-prompt

AI SDK

Menu

Guides

RAG Agent

Multi-Modal Agent

Slackbot Agent Guide

Natural Language Postgres

Get started with Computer Use

Get started with Gemini 2.5

Get started with Claude 4

OpenAI Responses API

Google Gemini Image Generation

Get started with Claude 3.7 Sonnet

Get started with Llama 3.1

Get started with GPT-5

Get started with OpenAI o1

Get started with OpenAI o3-mini

Get started with DeepSeek R1

Next.js

Generate Text

Generate Text with Chat Prompt

Generate Image with Chat Prompt

Stream Text

Stream Text with Chat Prompt

Stream Text with Image Prompt

Chat with PDFs

streamText Multi-Step Cookbook

Markdown Chatbot with Memoization

Generate Object

Generate Object with File Prompt through Form Submission

Stream Object

Call Tools

Call Tools in Multiple Steps

Model Context Protocol (MCP) Tools

Share useChat State Across Components

Human-in-the-Loop Agent with Next.js

Send Custom Body from useChat

Render Visual Interface in Chat

Caching Middleware

Node

Generate Text

Generate Text with Chat Prompt

Generate Text with Image Prompt

Stream Text

Stream Text with Chat Prompt

Stream Text with Image Prompt

Stream Text with File Prompt

Generate Object with a Reasoning Model

Generate Object

Stream Object

Stream Object with Image Prompt

Record Token Usage After Streaming Object

Record Final Object after Streaming Object

Call Tools

Call Tools with Image Prompt

Call Tools in Multiple Steps

Model Context Protocol (MCP) Tools

Manual Agent Loop

Web Search Agent

Embed Text

Embed Text in Batch

Intercepting Fetch Requests

Local Caching Middleware

Retrieval Augmented Generation

Knowledge Base Agent

API Servers

Node.js HTTP Server

Express

Hono

Fastify

Nest.js

React Server Components

Copy markdown

Generate Object with File Prompt through Form Submission

=======================================================================================================================================================================================

This feature is limited to models/providers that support PDF inputs (Anthropic , OpenAI , Google Gemini , and Google Vertex ).

With select models, you can send PDFs (files) as part of your prompt. Let's create a simple Next.js application that allows a user to upload a PDF send it to an LLM for summarization.

Client


On the frontend, create a form that allows the user to upload a PDF. When the form is submitted, send the PDF to the /api/analyze route.

'use client';
import { useState } from 'react';
export default function Page() {  const [description, setDescription] = useState<string>();  const [loading, setLoading] = useState(false);
  return (    <div>      <form        action={async formData => {          try {            setLoading(true);            const response = await fetch('/api/analyze', {              method: 'POST',              body: formData,            });            setLoading(false);
            if (response.ok) {              setDescription(await response.text());            }          } catch (error) {            console.error('Analysis failed:', error);          }        }}      >        <div>          <label>Upload Image</label>          <input name="pdf" type="file" accept="application/pdf" />        </div>        <button type="submit" disabled={loading}>          Submit{loading && 'ing...'}        </button>      </form>      {description && (        <pre>{JSON.stringify(JSON.parse(description), null, 2)}</pre>      )}    </div>  );}

Server


On the server, create an API route that receives the PDF, sends it to the LLM, and returns the result. This example uses the generateObject function to generate the summary as part of a structured output.

import { openai } from '@ai-sdk/openai';import { generateObject } from 'ai';import { z } from 'zod';
export async function POST(request: Request) {  const formData = await request.formData();  const file = formData.get('pdf') as File;
  // Convert the file's arrayBuffer to a Base64 data URL  const arrayBuffer = await file.arrayBuffer();  const uint8Array = new Uint8Array(arrayBuffer);
  // Convert Uint8Array to an array of characters  const charArray = Array.from(uint8Array, byte => String.fromCharCode(byte));  const binaryString = charArray.join('');  const base64Data = btoa(binaryString);  const fileDataUrl = `data:application/pdf;base64,${base64Data}`;
  const result = await generateObject({    model: openai('gpt-4o'),    messages: [      {        role: 'user',        content: [          {            type: 'text',            text: 'Analyze the following PDF and generate a summary.',          },          {            type: 'file',            data: fileDataUrl,            mediaType: 'application/pdf',          },        ],      },    ],    schema: z.object({      people: z        .object({          name: z.string().describe('The name of the person.'),          age: z.number().min(0).describe('The age of the person.'),        })        .array()        .describe('An array of people.'),    }),  });
  return Response.json(result.object);}

On this page

Generate Object with File Prompt through Form Submission

Client

Server

Deploy and Scale AI Apps with Vercel.

Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.

Trusted by industry leaders:

  • OpenAI
  • Photoroom
  • leonardo-ai Logoleonardo-ai Logo
  • zapier Logozapier Logo

Talk to an expert