> ## Documentation Index
> Fetch the complete documentation index at: https://www.edenai.co/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Use the OpenAI Python SDK with Eden AI

> Point the OpenAI Python SDK at Eden AI by changing base_url and api_key, then call 500+ models with chat completions, vision and error handling.

**Use the OpenAI Python SDK with Eden AI**

Point the OpenAI Python SDK at Eden AI by changing base\_url and api\_key, then call 500+ models with chat completions, vision and error handling.

Set `api_key` and `base_url` on an existing OpenAI client and switch model ids to the `provider/model` format; your `client.chat.completions` calls and their response objects stay the same.

## Overview

Eden AI's V3 chat completions endpoint is OpenAI-compatible. Models from OpenAI, Anthropic, Google, Cohere and other providers use the same call. The other OpenAI APIs (Files, Assistants, Batches) are not served under `/v3`; uploads go through `/v3/upload`. For streaming, see [Streaming](https://www.edenai.co/docs/v3/llms/streaming.md).

## Installation

```bash pip theme={null}
pip install openai
```

```bash poetry theme={null}
poetry add openai
```

## Quick Start

Configure the OpenAI client to use Eden AI:

```python Python theme={null}
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",  # Get from https://app.edenai.run
    base_url="https://api.edenai.run/v3"
)

response = client.chat.completions.create(
    model="openai/gpt-latest",
    messages=[
        {"role": "user", "content": "Hello! How are you?"}
    ]
)

print(response.choices[0].message.content)
```

## Model naming

Models use the `provider/model` format, for example `openai/gpt-latest`, `anthropic/claude-sonnet-latest` and `vertex/gemini-flash-latest`. Browse the catalog in [List LLM Models](https://www.edenai.co/docs/v3/llms/listing-models.md).

## Multi-Turn Conversations

Build conversational applications with message history:

```python Python theme={null}
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.edenai.run/v3"
)

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is the capital of France?"}
]

response = client.chat.completions.create(
    model="vertex/gemini-flash-latest",
    messages=messages
)

assistant_response = response.choices[0].message.content
print(assistant_response)

# Add assistant response to history
messages.append({"role": "assistant", "content": assistant_response})

# Continue conversation
messages.append({"role": "user", "content": "What's the population?"})

response = client.chat.completions.create(
    model="vertex/gemini-flash-latest",
    messages=messages
)

print(response.choices[0].message.content)
```

## Vision Capabilities

Send images to a model that accepts them: its `capabilities.input_modalities` in [List LLM Models](https://www.edenai.co/docs/v3/llms/listing-models.md) includes `image`.

```python Python theme={null}
from openai import OpenAI
import requests

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.edenai.run/v3"
)

# First, upload the image to get a file_id
with open("image.jpg", "rb") as f:
    upload_response = requests.post(
        "https://api.edenai.run/v3/upload",
        headers={"Authorization": "Bearer YOUR_API_KEY"},
        files={"file": f}
    )
file_id = upload_response.json()["file_id"]

# Use the file_id in a chat message
response = client.chat.completions.create(
    model="openai/gpt-latest",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {"type": "file", "file": {"file_id": file_id}}
            ]
        }
    ]
)

print(response.choices[0].message.content)
```

## Error Handling

The SDK raises a subclass of `openai.APIStatusError` for most statuses; 402 and 451 have none. These are the statuses specific to Eden AI:

| Status | Exception | Meaning |
| - | - | - |
| 401 | `openai.AuthenticationError` | The key is not accepted. The detail is `Invalid API key` for a wrong or revoked `sk-eden-...` key, `Custom token has expired.` for an expired one, and `Invalid token` for other token shapes. |
| 402 | `openai.APIStatusError` | Insufficient credit. The SDK has no subclass for 402. |
| 403 | `openai.PermissionDeniedError` | A compliance or guardrail rule refused the request. |
| 429 | `openai.RateLimitError` | Rate limit exceeded. The SDK retries twice before raising; the `Retry-After` header says how long to wait. |
| 451 | `openai.APIStatusError` | No provider of the model runs in a region that the endpoint you called, or your organization's [compliance policy](https://www.edenai.co/docs/v3/data-governance/compliance-policies.md), allows, or, for a [routing policy](https://www.edenai.co/docs/v3/llms/routing-policies.md), every target was dropped for its region. |

An unknown model id answers 400 `Model(s) not found or inactive`, an invalid request 422, and a provider failure 5xx. The `APIStatusError` handler below catches them with the others.

```python Python theme={null}
from openai import OpenAI
import openai

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.edenai.run/v3"
)

try:
    response = client.chat.completions.create(
        model="openai/gpt-latest",
        messages=[{"role": "user", "content": "Hello!"}]
    )

    print(response.choices[0].message.content)

except openai.AuthenticationError as e:
    print(f"Authentication failed: {e}")
except openai.PermissionDeniedError as e:
    print(f"Request refused: {e}")
except openai.RateLimitError as e:
    print(f"Rate limit exceeded: {e}")
except openai.APIStatusError as e:
    # 402 (insufficient credit), 451 (region refusal) and any other status
    print(f"API error {e.status_code}: {e}")
except openai.APIConnectionError as e:
    print(f"Could not reach the API: {e}")
```

## Environment Variables

```bash .env theme={null}
EDEN_AI_API_KEY=your_api_key_here
```

```python Python theme={null}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("EDEN_AI_API_KEY"),
    base_url="https://api.edenai.run/v3"
)
```

## Next Steps

* [Chat Completions](https://www.edenai.co/docs/v3/llms/chat-completions.md) - Core LLM endpoint
* [Streaming](https://www.edenai.co/docs/v3/llms/streaming.md) - Stream tokens as they are generated
* [File Attachments](https://www.edenai.co/docs/v3/llms/file-upload.md) - Working with images and documents
* [List LLM Models](https://www.edenai.co/docs/v3/llms/listing-models.md) - Browse available providers and models


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.