Use the OpenAI Python SDK with a custom Base URL
The official Python SDK accepts a custom base_url. Point it at MonkeyAI, keep the key in an environment variable, and call models.list() before trying a paid generation. This guide records the exact 401 test run on August 3, 2026; it does not claim that every SDK endpoint is supported.
What was tested?
We installed openai==2.48.0 in a clean Python virtual environment and created OpenAI(api_key=..., base_url="https://monkeytheme.com/v1", max_retries=0, timeout=20). Calling client.models.list() with an intentionally invalid key reached MonkeyAI and returned HTTP 401 with INVALID_API_KEY. That proves the SDK, TLS route and custom Base URL reached the authentication layer. It does not prove that a real key, a particular model or a generation endpoint will succeed.
| Checked | Observed result |
|---|---|
| SDK package | openai 2.48.0 |
| Configured Base URL | https://monkeytheme.com/v1 |
| Method | client.models.list() |
| Invalid-key result | AuthenticationError, HTTP 401, structured JSON |
How do I install and configure it?
python3 -m venv .venv source .venv/bin/activate python -m pip install openai export MONKEYAI_API_KEY="***"
Do not commit the key to a Python file. The OpenAI SDK README recommends environment-based secret loading, and the constructor accepts an explicit api_key value.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["MONKEYAI_API_KEY"],
base_url="https://monkeytheme.com/v1",
)/v1. Do not append /models or /chat/completions to base_url; SDK methods add their endpoint paths.How do I verify the key before generating text?
models = client.models.list()
for model in models.data:
print(model.id)A successful list gives you the model IDs visible to that key. Copy an exact ID into later requests instead of guessing. A 401 means the request reached the service but authentication failed. If the call fails before receiving an HTTP response, inspect the network or TLS exception instead.
How should I handle errors?
import openai
try:
models = client.models.list()
except openai.AuthenticationError:
print("Check MONKEYAI_API_KEY")
except openai.APIConnectionError as exc:
print("Network or TLS failure:", exc.__cause__)
except openai.APIStatusError as exc:
print("HTTP status:", exc.status_code)
print("Request ID:", exc.request_id)The SDK documents APIConnectionError for connection failures and APIStatusError for non-success HTTP responses. It also exposes a request ID on status errors when one is available. Log the request ID, not the API key.
What about retries and timeouts?
The official SDK README says selected connection errors, 408, 409, 429 and server errors are retried twice by default. It also documents a ten-minute default request timeout. Those defaults can hide repeated work during debugging, so the verification example uses max_retries=0 and timeout=20.
client = OpenAI(
api_key=os.environ["MONKEYAI_API_KEY"],
base_url="https://monkeytheme.com/v1",
max_retries=0,
timeout=20.0,
)Choose production retry and timeout values for your workload. A short model-list check and a long streaming generation should not automatically share the same policy.
Which generation API should I call?
The OpenAI SDK supports both responses.create() and chat.completions.create(). An OpenAI-compatible relay may implement a different subset. Check the MonkeyAI guide for your client or endpoint before choosing one. We did not run authenticated generation in this verification, so this page deliberately stops at model discovery.
Sources
FAQ
What Base URL should I use?
Use https://monkeytheme.com/v1 as the OpenAI-compatible API base.
Can I set OPENAI_BASE_URL instead?
The official SDK documents OPENAI_BASE_URL. This guide passes base_url explicitly so the active endpoint is visible in code.
Does a 401 mean the custom Base URL is broken?
No. A structured 401 means the request reached the service and authentication was rejected.
Does models.list() prove generation will work?
No. It verifies model discovery for that key. A model or generation route can still have separate availability or compatibility limits.
Are all OpenAI SDK endpoints supported?
This guide does not make that claim. OpenAI-compatible services can implement different endpoint subsets; verify the endpoint you plan to use.