Skip to content
PrismRoute

Connect with Python.

Use the official OpenAI SDK with your PrismRoute key and a custom API base URL. These examples run in Python.

On this page

Install and configure

Install the current official SDK. Use a Python version supported by that SDK release.

Install the SDK
python -m pip install --upgrade openai

Set your key and exact model ID in the terminal. Replace both placeholders. The environment variable names below belong to this guide; the client receives them explicitly.

macOS / Linux shell
export PRISMROUTE_API_KEY="YOUR_PRISMROUTE_API_KEY"
export PRISMROUTE_MODEL="YOUR_MODEL_ID"
Windows PowerShell
$env:PRISMROUTE_API_KEY="YOUR_PRISMROUTE_API_KEY"
$env:PRISMROUTE_MODEL="YOUR_MODEL_ID"

Send a Chat Completions request

Save this as example.py. The base_url option is the API root, not the full /chat/completions URL. Use a model listed as supporting Chat Completions.

example.py
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["PRISMROUTE_API_KEY"],
    base_url="https://codex.aicpr.icu/v1",
    timeout=60.0,
    max_retries=0,
)

completion = client.chat.completions.create(
    model=os.environ["PRISMROUTE_MODEL"],
    messages=[{"role": "user", "content": "Hello, PrismRoute!"}],
)
print(completion.choices[0].message.content or "")
Run the example
python example.py

The example uses a 60-second timeout and disables automatic retries for the first connection check. For a production application, choose an explicit timeout and a bounded retry policy suitable for its work.

Read streamed text

For a model that supports streaming, read each text delta as it arrives. Some chunks carry metadata instead of text; the example handles empty choices and missing content.

Streaming example
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["PRISMROUTE_API_KEY"],
    base_url="https://codex.aicpr.icu/v1",
    timeout=60.0,
    max_retries=0,
)

stream = client.chat.completions.create(
    model=os.environ["PRISMROUTE_MODEL"],
    messages=[{"role": "user", "content": "Hello, PrismRoute!"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
print()

A streaming connection can fail after some text has arrived. Preserve that partial output and inspect the error before deciding whether to retry the request.

Use Responses when the model supports it

Optional Responses example
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["PRISMROUTE_API_KEY"],
    base_url="https://codex.aicpr.icu/v1",
    timeout=60.0,
    max_retries=0,
)

response = client.responses.create(
    model=os.environ["PRISMROUTE_MODEL"],
    input="Hello, PrismRoute!",
)
print(response.output_text)

Responses uses input and returns typed output items. The SDK’s output_text helper joins text output. Its streaming events differ from Chat Completions deltas; do not reuse the streaming loop above for a Responses stream.

Check your integration

Confirm that the generated request goes to codex.aicpr.icu, includes /v1 only once and uses the exact model ID. If the request fails, keep its status code and a redacted error message for troubleshooting.

Technical references