AI SDK's uploadFile function uploads a file to a provider and returns a ProviderReference that you reuse in later API calls. Upload a large image, PDF, or document once, then reference it by ID in your messages instead of re-sending the bytes on every request. If you switch providers mid-conversation, you can merge references so the same file works across them. Anthropic, Google, OpenAI, and xAI support file uploads.
In this guide, you'll learn how to:
- Upload a file with
uploadFileand get aProviderReference - Reference an uploaded file in a
generateTextorstreamTextmessage - Pass provider-specific upload options, such as OpenAI's
purpose - Reuse one file across multiple providers by merging references
Before you begin, make sure you have:
- The
aipackage - A provider package that supports file uploads, such as
@ai-sdk/openaior@ai-sdk/anthropic, with provider credentials - A file to upload (an image, PDF, text file, or other supported document)
A ProviderReference is a Record<string, string> that maps each provider's name to that provider's own file identifier, such as { openai: 'file-abc123' }. When you upload a file, uploadFile returns one. Pass the reference in the message content, and the provider looks up its own file ID. If the reference has no entry for the provider handling the request, the SDK throws an error. The same ProviderReference shape identifies other provider assets, too, such as uploaded skills.
Call uploadFile with the provider's files API and the file data. It returns a providerReference that points to the uploaded file.
As a shorthand, pass the provider instance directly to api (for example, api: openai) and the SDK calls .files() for you. The media type is auto-detected from the file bytes, so set mediaType explicitly only when you need to, such as 'application/pdf' for a PDF.
Pass the providerReference in a message content part. For an image, use an image part, and the provider resolves the reference to its own file when it processes the request.
For a document such as a PDF, use a file part with the reference as data and the media type:
Some providers accept extra upload options through providerOptions. OpenAI, for example, requires a purpose field. Each provider exports an options type you can apply with satisfies for type safety.
To use one file with more than one provider, upload it to each provider and merge the references into a single ProviderReference. The merged reference works no matter which provider processes the request, because each provider finds its own file ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
api | FilesV4 or ProviderV4 | Yes | The provider's files API, such as openai.files(), or a provider instance as shorthand |
data | DataContent | Yes | The file contents, such as a Buffer, Uint8Array, or base64 string |
mediaType | string | No | The file's media type; auto-detected from the bytes when omitted |
filename | string | No | The file name to send to the provider |
providerOptions | ProviderOptions | No | Provider-specific upload options, such as OpenAI's purpose |
| Field | Type | Description |
|---|---|---|
providerReference | ProviderReference | Maps provider names to their file IDs; pass it in message content parts |
providerMetadata | ProviderMetadata? | Additional provider-specific metadata, when available |
warnings | Warning[] | Warnings for unsupported options |
These providers support files() and file uploads:
| Provider | Factory method |
|---|---|
| Anthropic | anthropic.files() |
google.files() | |
| OpenAI | openai.files() |
| xAI | xai.files() |
A provider without file upload support throws an UnsupportedFunctionalityError if it encounters a provider reference in a message.
- Read the
uploadFilereference for the full signature and return fields. - See Skill uploads, which use the same
ProviderReferencepattern for skills. - Learn how message content parts work in Prompts.
- Check Providers and models for provider support and setup.