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

# OpenAI Python SDK

> Direct SDK with custom base URL

<Error>
  This page contains an unverified test date marked in <span style="color:red">red</span>. Needs verification (Vijay, Adam, Alex, Product/Legal).
</Error>

# OpenAI Python SDK

Radium exposes an OpenAI-compatible Chat Completions API. If your application already uses the OpenAI Python SDK, you only need to change the API key, base URL, and model name.

## Before you start

You need Python 3.10 or newer, a Radium API key, and a terminal or PowerShell window.

## 1. Create a project and install the SDK

<CodeGroup>
  ```bash macOS or Linux theme={null}
  mkdir radium-openai-python && cd radium-openai-python
  python3 -m venv .venv
  source .venv/bin/activate
  python -m pip install openai
  ```

  ```powershell Windows PowerShell theme={null}
  mkdir radium-openai-python; cd radium-openai-python
  py -m venv .venv
  .venv\Scripts\Activate.ps1
  python -m pip install openai
  ```
</CodeGroup>

## 2. Set your Radium credentials

<CodeGroup>
  ```bash macOS or Linux theme={null}
  export RADIUM_API_KEY="your-radium-api-key"
  export RADIUM_MODEL="hal-1.0"
  ```

  ```powershell Windows PowerShell theme={null}
  $env:RADIUM_API_KEY = "your-radium-api-key"
  $env:RADIUM_MODEL = "hal-1.0"
  ```
</CodeGroup>

<Warning>
  Keep the key on the server. Do not paste it into `main.py`, commit it, or expose it in browser code.
</Warning>

## 3. Create main.py

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

api_key = os.getenv("RADIUM_API_KEY")
if not api_key:
    raise SystemExit("Set RADIUM_API_KEY before running this program.")

model = os.getenv("RADIUM_MODEL", "hal-1.0")
client = OpenAI(
    api_key=api_key,
    base_url="https://api.radium.cloud/v1",
    timeout=90,
    max_retries=0,
)

response = client.chat.completions.create(
    model=model,
    messages=[
        {"role": "system", "content": "Follow the user's instruction exactly."},
        {"role": "user", "content": "Reply with exactly: Radium connected."},
    ],
    max_tokens=512,
    temperature=0,
)

content = response.choices[0].message.content
if not content or not content.strip():
    raise RuntimeError("Radium returned no visible text.")
print(f"RADIUM_RESPONSE: {content.strip()}")
```

<Note>
  The base URL must include `/v1`. Do not append `/chat/completions`; the SDK adds that path.
</Note>

## 4. Run it

```bash theme={null}
python main.py
```

Expected output:

```
RADIUM_RESPONSE: Radium connected.
```

## Choose a model

Set `RADIUM_MODEL` to any validated model before running the program:

```bash theme={null}
export RADIUM_MODEL="hal-1.0"  # or: clarke-1.0, tycho-1.0
python main.py
```

## Migrate an existing OpenAI client

```python theme={null}
# Before:
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# After:
client = OpenAI(
    api_key=os.environ["RADIUM_API_KEY"],
    base_url="https://api.radium.cloud/v1",
)
```

Keep your existing messages and response parsing. Replace the old model ID with `hal-1.0`, `clarke-1.0`, or `tycho-1.0`.

## Troubleshooting

| Problem | Fix |
| - | - |
| `401` or `403` | Verify that `RADIUM_API_KEY` is set in the same terminal running Python |
| `404` | Verify the base URL and use one of the model IDs listed above |
| Empty output | Keep `max_tokens` at 512 or higher so model reasoning does not consume the visible response allowance |
| `ModuleNotFoundError` | Activate `.venv` and rerun the installation command |

## Validation

<Check>
  These instructions were verified with Python 3.12.7 and `openai==2.54.0` on <span style="color:red">August 20, 2026</span>. You do not need that exact package version.
</Check>

Text generation and a complete function-calling loop passed with `hal-1.0`, `clarke-1.0`, and `tycho-1.0`.

## Next steps

<CardGroup cols={2}>
  <Card title="API quickstart" icon="bolt" href="/quickstart">Call Radium directly</Card>
  <Card title="Tool calling" icon="wrench" href="/core-concepts/tool-calling">The tool\_use and tool\_result contract</Card>
</CardGroup>


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