The Vercel AI SDK provides a simple and unified API for interacting with OpenAI-compatible language models. This guide explains how to integrate the SDK with OpenAI or any OpenAI-compatible API endpoint for text generation, streaming, image generation, and multimodal (vision) capabilities.
Prerequisites
Before you begin, ensure you have:
- Node.js 18+
- An OpenAI-compatible API endpoint
- An API key
- npm or another package manager
Installation
Install the required packages:
npm install ai @ai-sdk/openai dotenv
Package Overview
| Package | Description |
|---|
ai | Core Vercel AI SDK containing text generation, streaming, and image APIs. |
@ai-sdk/openai | OpenAI provider that supports both OpenAI and OpenAI-compatible endpoints. |
dotenv | Loads API keys and configuration from environment variables. |
Environment Variables
Create a .env file:
API_KEY=your_api_key
BASE_URL=https://your-openai-compatible-endpoint/v1
MODEL_NAME=gpt-4o-mini
IMAGE_MODEL_NAME=gpt-image-2
BASE_URL is optional when using the official OpenAI API. Specify it only when connecting to an OpenAI-compatible provider or proxy.
Client Configuration
Create a reusable client configuration.
import { createOpenAI } from '@ai-sdk/openai';
import * as dotenv from 'dotenv';
dotenv.config();
const apiKey = process.env.API_KEY;
const baseURL = process.env.BASE_URL;
export const MODEL = process.env.MODEL_NAME;
export const IMAGE_MODEL = process.env.IMAGE_MODEL_NAME;
export const openai = createOpenAI({
apiKey,
baseURL,
});
Next Steps
To explore individual capabilities with code examples, check the following sections in the sidebar:
Best Practices
- Store API keys in environment variables.
- Reuse a single OpenAI client instance throughout your application.
- Use streaming for chat interfaces to improve user experience.
- Validate generation parameters before making requests.
- Handle API errors gracefully with retry or fallback logic.
- Use system prompts to define consistent assistant behavior.
- Keep prompts concise and specific for better model responses.
Supported Features
| Feature | Supported |
|---|
| Text Generation | ✅ |
| Streaming Responses | ✅ |
| System Prompts | ✅ |
| Temperature | ✅ |
| Top P | ✅ |
| Top K | ✅ |
| Max Tokens | ✅ |
| Frequency Penalty | ✅ |
| Presence Penalty | ✅ |
| Seed | ✅ |
| Stop Sequences | ✅ |
| Image Generation | ✅ |
| Vision (Image Input) | ✅ |
| OpenAI-Compatible Base URL | ✅ |
Next Steps
You are now ready to build applications using the Vercel AI SDK with any OpenAI-compatible endpoint. Explore additional SDK capabilities such as structured outputs, tool calling, agents, and chat history management as your application grows.