> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/vercel/ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Provider

> Learn how to use the OpenAI provider for text generation, embeddings, and image generation with the AI SDK.

# OpenAI Provider

The OpenAI provider enables you to use OpenAI's language models, including GPT-4, GPT-5, and o3 models, for text generation, structured outputs, tool calling, and more.

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @ai-sdk/openai
  ```

  ```bash pnpm theme={null}
  pnpm add @ai-sdk/openai
  ```

  ```bash yarn theme={null}
  yarn add @ai-sdk/openai
  ```

  ```bash bun theme={null}
  bun add @ai-sdk/openai
  ```
</CodeGroup>

## Setup

Get your API key from [OpenAI's platform](https://platform.openai.com/api-keys) and set it as an environment variable:

```bash theme={null}
OPENAI_API_KEY=your-api-key
```

## Usage

### Basic Text Generation

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';

const { text } = await generateText({
  model: openai('gpt-4-turbo'),
  prompt: 'What is the capital of France?',
});
```

### Streaming Responses

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

const result = streamText({
  model: openai('gpt-4-turbo'),
  prompt: 'Write a story about a robot.',
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
```

### Structured Output

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { generateObject } from 'ai';
import { z } from 'zod';

const { object } = await generateObject({
  model: openai('gpt-4-turbo'),
  schema: z.object({
    name: z.string(),
    age: z.number(),
    occupation: z.string(),
  }),
  prompt: 'Generate a person profile.',
});
```

## Configuration

### Custom Provider Instance

```typescript theme={null}
import { createOpenAI } from '@ai-sdk/openai';

const openai = createOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: 'https://api.openai.com/v1',
  organization: 'your-org-id',
});
```

### Provider Options

* **apiKey**: Your OpenAI API key (defaults to `OPENAI_API_KEY` env var)
* **baseURL**: Custom API endpoint URL
* **organization**: OpenAI organization ID
* **project**: OpenAI project ID
* **headers**: Custom headers for requests

## Available Models

### Chat Models

* `gpt-5` - Latest GPT-5 model
* `gpt-4-turbo` - GPT-4 Turbo
* `gpt-4` - GPT-4
* `gpt-3.5-turbo` - GPT-3.5 Turbo

### Reasoning Models

* `o3-mini` - Smaller reasoning model
* `o3` - Advanced reasoning model

### Embeddings

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { embed } from 'ai';

const { embedding } = await embed({
  model: openai.embedding('text-embedding-3-small'),
  value: 'Hello, world!',
});
```

**Available Models:**

* `text-embedding-3-small` (1536 dimensions)
* `text-embedding-3-large` (3072 dimensions)
* `text-embedding-ada-002` (1536 dimensions)

### Image Generation

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { generateImage } from 'ai';

const { image } = await generateImage({
  model: openai.image('dall-e-3'),
  prompt: 'A futuristic city at sunset',
  size: '1024x1024',
});
```

**Available Models:**

* `dall-e-3` - Latest DALL-E model
* `dall-e-2` - Previous generation

## Advanced Features

### Tool Calling

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';

const result = await generateText({
  model: openai('gpt-4-turbo'),
  tools: {
    weather: {
      description: 'Get the weather for a location',
      parameters: z.object({
        location: z.string(),
      }),
      execute: async ({ location }) => {
        return { temperature: 72, condition: 'sunny' };
      },
    },
  },
  prompt: 'What is the weather in San Francisco?',
});
```

### Vision

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import fs from 'fs';

const result = await generateText({
  model: openai('gpt-4-vision-preview'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'What is in this image?' },
        { type: 'image', image: fs.readFileSync('./image.png') },
      ],
    },
  ],
});
```

## Resources

* [OpenAI API Documentation](https://platform.openai.com/docs)
* [AI SDK Documentation](/docs)
* [Example Projects](/examples)
