new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

AI SDK · Cookbook · all subjects

multimodal/file-upload

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

ImagePart image content formats

ImagePart image field accepts base64 encoded content, base64 data URLs, or http(s) URLs as strings, or binary data as Uint8Array, Buffer, or ArrayBuffer.

FilePart data content formats

FilePart data field accepts base64 encoded content, base64 data URLs, or http(s) URLs as strings, or binary data as Uint8Array, Buffer, or ArrayBuffer.

Send PDF file in message to Claude

To send a PDF file in a message to Claude, include it in the user message content using FilePart. In a UserModelMessage, add a FilePart to the content array with type: 'file', data: (base64 string, base64 data URL, http(s) URL, or binary Uint8Array/Buffer/ArrayBuffer), and mediaType: 'application/pdf' or the appropriate IANA media type.

uploadFile basic usage example

import { uploadFile } from 'ai'; import { openai } from '@ai-sdk/openai'; import fs from 'node:fs'; const { providerReference } = await uploadFile({ api: openai.files(), data: fs.readFileSync('./photo.png'), filename: 'photo.png', });

uploadFile parameters specification

uploadFile accepts the following parameters: api (required, type FilesV4 | ProviderV4, the files API interface to use for uploading, can be a FilesV4 instance like openai.files() or a provider instance directly like openai where .files() is called automatically); data (required, type DataContent | { type: "stream"; stream: ReadableStream<Uint8Array> }, can be Uint8Array, base64-encoded string, ArrayBuffer, Buffer, or tagged stream shape for providers supporting streaming uploads, URLs not supported); mediaType (optional, string, IANA media type like image/png or application/pdf, auto-detected from file bytes if not provided, stream data defaults to application/octet-stream); filename (optional, string, filename for uploaded file, multipart-based providers default to "blob" when omitted); abortSignal (optional, AbortSignal, signal to cancel the upload); headers (optional, Record<string, string>, additional HTTP headers); providerOptions (optional, ProviderOptions, additional provider-specific options like OpenAI's required purpose field).

uploadFile return value specification

uploadFile returns an object with the following properties: providerReference (required, type ProviderReference which is Record<string, string> mapping provider names to provider-specific file identifiers, pass as data or image field in message content parts); byteSize (optional, number, size of uploaded file in bytes if reported by provider); createdAt (optional, Date, when file was created if reported by provider); expiresAt (optional, Date, when provider will delete file if reported by provider); providerMetadata (optional, ProviderMetadata, additional provider-specific metadata from upload); warnings (required, Warning[], warnings from provider such as unsupported settings).

uploadFile data parameter stream support

The data parameter of uploadFile supports streaming uploads via a tagged { type: "stream", stream: ReadableStream<Uint8Array> } shape for providers that support streaming uploads. The stream is sent without buffering. If an upload fails, including validation failures before a request is made, the stream is cancelled and must not be reused. Providers that do not support streaming uploads reject with an UnsupportedFunctionalityError.

uploadFile does not support URLs directly

The uploadFile function does not support URLs as file data. To upload content from a URL, fetch the content first and pass the bytes to the data parameter.

Give your agent this brain