# OpenAI Python

Use the official `openai` Python library with Aurora's base URL and API token.
Call `client.chat.completions.create`; the library's Responses API examples do
not apply to Aurora.

> Available Inference endpoint: `https://ai.aur.lu/v1` (Aurora).

## Install and prepare

Use a supported Python version in a virtual environment:

```bash
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade openai
export AURORA_API_TOKEN="<your-token>"
```

On Windows, activate with `.venv\Scripts\Activate.ps1` in PowerShell. Obtain the
token through [Getting started](https://docs.aur.lu/docs/inference/getting-started).

## Send a chat request

Save as `aurora_chat.py`:

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://ai.aur.lu/v1",
    api_key=os.environ["AURORA_API_TOKEN"],
)

response = client.chat.completions.create(
    model="deepseek/deepseek-v4-flash",
    messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
print(response.choices[0].message.content)
print(response.usage)
```

```bash
python aurora_chat.py
```

Expect a generated greeting and token usage. The base URL includes `/v1`; the SDK
appends the request path. Use a full ID from the [model catalog](https://docs.aur.lu/docs/inference/models-and-pricing).

To inspect the catalog with the same client:

```python
for model in client.models.list():
    print(model.id)
```

## Stream the answer

Replace the request and print statements in `aurora_chat.py` with:

```python
stream = client.chat.completions.create(
    model="deepseek/deepseek-v4-flash",
    messages=[{"role": "user", "content": "Explain streaming in three sentences."}],
    stream=True,
    stream_options={"include_usage": True},
)

with stream:
    for chunk in stream:
        if chunk.choices:
            print(chunk.choices[0].delta.content or "", end="", flush=True)
        if chunk.usage is not None:
            print("\nUsage:", chunk.usage)
print()
```

The final usage chunk can have an empty `choices` array, so check before indexing.
For asynchronous applications, instantiate `AsyncOpenAI` with the same settings
and await `client.chat.completions.create(...)`.

## Troubleshooting

- **`KeyError: AURORA_API_TOKEN`:** Export the token in the shell running Python.
- **Import errors:** Do not name your script `openai.py`; confirm the package was
  installed in the active environment.
- **HTTP errors:** Catch `openai.APIStatusError` to inspect `status_code` and the
  error response. Avoid logging the token or authorization headers.
- **Unsupported endpoint:** Use `chat.completions`, not `responses.create`,
  Assistants, embeddings, or audio APIs.

See [shared troubleshooting](https://docs.aur.lu/docs/inference/integrations#troubleshooting) and the
[OpenAI Python SDK reference](https://github.com/openai/openai-python).
