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

# OpenAI API Integration

> Connect Open WebUI with OpenAI and OpenAI-compatible providers

## Overview

Open WebUI supports OpenAI's API and any OpenAI-compatible API endpoints, including Azure OpenAI, Anthropic, Google AI, and custom providers.

## Quick Start

<Steps>
  <Step title="Get API Key">
    Obtain an API key from your provider (OpenAI, Azure, etc.)
  </Step>

  <Step title="Configure Environment">
    Set the API key and base URL
  </Step>

  <Step title="Enable OpenAI API">
    Enable in Admin Panel or set `ENABLE_OPENAI_API=True`
  </Step>
</Steps>

## Configuration

### OpenAI

<CodeGroup>
  ```bash Environment Variables theme={null}
  OPENAI_API_KEY=sk-proj-...
  OPENAI_API_BASE_URL=https://api.openai.com/v1
  ENABLE_OPENAI_API=True
  ```

  ```bash Docker theme={null}
  docker run -d -p 3000:8080 \
    -e OPENAI_API_KEY=sk-proj-... \
    -v open-webui:/app/backend/data \
    ghcr.io/open-webui/open-webui:main
  ```

  ```bash Multiple Keys theme={null}
  # Separate with semicolons
  OPENAI_API_KEYS="sk-key1;sk-key2;sk-key3"
  OPENAI_API_BASE_URLS="https://api.openai.com/v1;https://api.openai.com/v1;https://api.openai.com/v1"
  ```
</CodeGroup>

### Azure OpenAI

<CodeGroup>
  ```bash Environment Variables theme={null}
  OPENAI_API_KEY=your-azure-key
  OPENAI_API_BASE_URL=https://your-resource.openai.azure.com
  ```

  ```json API Configuration theme={null}
  {
    "OPENAI_API_CONFIGS": {
      "0": {
        "azure": true,
        "api_version": "2024-09-01-preview",
        "auth_type": "bearer"
      }
    }
  }
  ```

  ```json Azure Entra ID (Microsoft Entra) theme={null}
  {
    "OPENAI_API_CONFIGS": {
      "0": {
        "azure": true,
        "api_version": "2024-09-01-preview",
        "auth_type": "microsoft_entra_id"
      }
    }
  }
  ```
</CodeGroup>

<Note>
  Azure OpenAI requires deployment names. The model ID in Open WebUI should match your Azure deployment name.

  *File: backend/open\_webui/routers/openai.py:800*
</Note>

## Advanced Configuration

### Authentication Types

Open WebUI supports multiple authentication methods:

<CodeGroup>
  ```json Bearer Token (Default) theme={null}
  {
    "auth_type": "bearer",
    "key": "your-api-key"
  }
  ```

  ```json Session-based Auth theme={null}
  {
    "auth_type": "session"
  }
  ```

  ```json System OAuth theme={null}
  {
    "auth_type": "system_oauth"
  }
  ```

  ```json Azure AD / Microsoft Entra ID theme={null}
  {
    "auth_type": "microsoft_entra_id"
  }
  ```

  ```json No Authentication theme={null}
  {
    "auth_type": "none"
  }
  ```
</CodeGroup>

*File: backend/open\_webui/routers/openai.py:149*

### Custom Headers

Add custom headers to API requests:

```json theme={null}
{
  "OPENAI_API_CONFIGS": {
    "0": {
      "headers": {
        "X-Custom-Header": "value",
        "Authorization": "Custom auth-scheme token"
      }
    }
  }
}
```

### Model Filtering

Specify which models to expose:

```json theme={null}
{
  "OPENAI_API_CONFIGS": {
    "0": {
      "model_ids": [
        "gpt-4-turbo",
        "gpt-4",
        "gpt-3.5-turbo"
      ]
    }
  }
}
```

### Connection Types

Tag connections as local or external:

```json theme={null}
{
  "OPENAI_API_CONFIGS": {
    "0": {
      "connection_type": "local",
      "prefix_id": "local",
      "tags": ["fast", "private"]
    },
    "1": {
      "connection_type": "external",
      "prefix_id": "cloud",
      "tags": ["cloud", "gpt4"]
    }
  }
}
```

*File: backend/open\_webui/routers/openai.py:421*

## OpenAI-Compatible Providers

### Anthropic

```bash theme={null}
OPENAI_API_BASE_URL=https://api.anthropic.com/v1
OPENAI_API_KEY=sk-ant-...
```

Open WebUI automatically detects Anthropic URLs and uses appropriate model mapping.

*File: backend/open\_webui/routers/openai.py:96*

### Google AI (Gemini)

```bash theme={null}
GEMINI_API_KEY=your-gemini-key
GEMINI_API_BASE_URL=https://generativelanguage.googleapis.com/v1
```

### OpenRouter

```bash theme={null}
OPENAI_API_BASE_URL=https://openrouter.ai/api/v1
OPENAI_API_KEY=sk-or-...
```

OpenRouter requires specific headers:

```json theme={null}
{
  "OPENAI_API_CONFIGS": {
    "0": {
      "headers": {
        "HTTP-Referer": "https://openwebui.com/",
        "X-Title": "Open WebUI"
      }
    }
  }
}
```

*File: backend/open\_webui/routers/openai.py:134*

### Local Providers

<CodeGroup>
  ```bash LM Studio theme={null}
  OPENAI_API_BASE_URL=http://localhost:1234/v1
  OPENAI_API_KEY=lm-studio
  ```

  ```bash LocalAI theme={null}
  OPENAI_API_BASE_URL=http://localhost:8080/v1
  OPENAI_API_KEY=local-ai
  ```

  ```bash Text Generation WebUI theme={null}
  OPENAI_API_BASE_URL=http://localhost:5001/v1
  OPENAI_API_KEY=any
  ```

  ```bash vLLM theme={null}
  OPENAI_API_BASE_URL=http://localhost:8000/v1
  OPENAI_API_KEY=EMPTY
  ```
</CodeGroup>

## Reasoning Models (o1, o3)

Open WebUI automatically handles OpenAI's reasoning models:

<CodeGroup>
  ```json o1-mini / o1-preview theme={null}
  // Converts max_tokens to max_completion_tokens
  // Converts system role to user role
  ```

  ```json o1 / o3 / o4 / gpt-5 theme={null}
  // Converts max_tokens to max_completion_tokens
  // Converts system role to developer role
  ```
</CodeGroup>

*File: backend/open\_webui/routers/openai.py:101*

## Responses API

For providers supporting OpenAI's Responses API:

```json theme={null}
{
  "OPENAI_API_CONFIGS": {
    "0": {
      "api_type": "responses"
    }
  }
}
```

Automatic conversion from Chat Completions to Responses API format:

*File: backend/open\_webui/routers/openai.py:827*

## API Endpoints

### Model Management

* `GET /openai/models` - List available models\
  *File: backend/open\_webui/routers/openai.py:556*

* `POST /openai/verify` - Verify connection and API key\
  *File: backend/open\_webui/routers/openai.py:659*

### Completions

* `POST /openai/chat/completions` - Chat completions\
  *File: backend/open\_webui/routers/openai.py:936*

* `POST /openai/responses` - Responses API (experimental)\
  *File: backend/open\_webui/routers/openai.py:1256*

### Audio

* `POST /openai/audio/speech` - Text-to-speech\
  *File: backend/open\_webui/routers/openai.py:272*

## Embeddings

For RAG and semantic search:

```python theme={null}
# Automatically routed based on model
POST /openai/embeddings
{
  "model": "text-embedding-3-small",
  "input": "Your text here"
}
```

*File: backend/open\_webui/routers/openai.py:1151*

## Troubleshooting

<AccordionGroup>
  <Accordion title="API Key Invalid">
    1. Verify the API key format
    2. Check for leading/trailing whitespace
    3. Ensure the key has necessary permissions
    4. Test with the provider's verify endpoint
  </Accordion>

  <Accordion title="Models Not Loading">
    OpenAI API filters out certain models by default:

    * Embedding models
    * TTS/Whisper models
    * DALL-E models
    * Legacy babbage/davinci models

    *File: backend/open\_webui/routers/openai.py:505*
  </Accordion>

  <Accordion title="Azure OpenAI Errors">
    Common issues:

    * Deployment name doesn't match model ID
    * Incorrect API version
    * Missing api-key header for non-Entra auth
    * Unsupported parameters for API version

    Allowed parameters vary by API version.

    *File: backend/open\_webui/routers/openai.py:754*
  </Accordion>

  <Accordion title="Rate Limiting">
    Open WebUI respects provider rate limits. Consider:

    * Using multiple API keys with load balancing
    * Implementing backoff in Pipelines
    * Monitoring usage through provider dashboard
  </Accordion>
</AccordionGroup>

## Advanced Features

### Logit Bias

Control token generation probabilities:

```json theme={null}
{
  "logit_bias": {
    "1234": -100,
    "5678": 100
  }
}
```

*File: backend/open\_webui/routers/openai.py:1058*

### User Info Forwarding

Forward user context to API:

```bash theme={null}
ENABLE_FORWARD_USER_INFO_HEADERS=true
```

Headers:

* `X-OpenWebUI-User-Name`
* `X-OpenWebUI-User-Id`
* `X-OpenWebUI-User-Email`
* `X-OpenWebUI-User-Role`
* `X-OpenWebUI-Chat-Id`

*File: backend/open\_webui/routers/openai.py:80*

### Model Caching

Models are cached for performance:

```bash theme={null}
MODELS_CACHE_TTL=300  # 5 minutes
```

*File: backend/open\_webui/routers/openai.py:482*

## Best Practices

<CardGroup cols={2}>
  <Card title="Use Environment Variables" icon="lock">
    Keep API keys in environment variables, not in database
  </Card>

  <Card title="Multiple Keys" icon="key">
    Configure multiple API keys for load balancing and redundancy
  </Card>

  <Card title="Model Access Control" icon="shield">
    Use Open WebUI's RBAC to control model access per user
  </Card>

  <Card title="Monitor Costs" icon="dollar-sign">
    Track usage through provider dashboards and OpenTelemetry
  </Card>
</CardGroup>
