Python SDK
The official OpenAI Python SDK works out of the box with ModelBridge. Just change the base URL and API key.
Installation
Install
pip install openai
Configuration
Set up your environment variables. If you're signed in, select your API key to auto-fill:
export OPENAI_BASE_URL="https://api.modelbridge.dev/v1"
export OPENAI_API_KEY="mb_live_your_key_here"Then create a client that reads from environment variables:
from openai import OpenAI
client = OpenAI() # reads OPENAI_BASE_URL and OPENAI_API_KEY from env
Or configure explicitly -- select your model and API key:
from openai import OpenAI
client = OpenAI(
base_url="https://api.modelbridge.dev/v1",
api_key="mb_live_your_key_here",
)
response = client.chat.completions.create(
model="",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello! What can you help me with?"},
],
)
print(response.choices[0].message.content)Streaming
Stream responses token-by-token for real-time output:
from openai import OpenAI
client = OpenAI(
base_url="https://api.modelbridge.dev/v1",
api_key="mb_live_your_key_here",
)
stream = client.chat.completions.create(
model="",
messages=[
{"role": "user", "content": "Write a short poem about code."},
],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
print()Tool calling (function calling)
Define tools and let the model decide when to call them:
from openai import OpenAI
client = OpenAI(
base_url="https://api.modelbridge.dev/v1",
api_key="mb_live_your_key_here",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name, e.g. San Francisco",
},
},
"required": ["location"],
},
},
}
]
response = client.chat.completions.create(
model="",
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=tools,
tool_choice="auto",
)
message = response.choices[0].message
if message.tool_calls:
tool_call = message.tool_calls[0]
print(f"Function: {tool_call.function.name}")
print(f"Arguments: {tool_call.function.arguments}")Async usage
Use AsyncOpenAI for non-blocking operations:
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI() # reads from env vars
async def main():
response = await client.chat.completions.create(
model="claude-opus-4-6",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())
Error handling
from openai import OpenAI, APIError, AuthenticationError, RateLimitError
client = OpenAI(max_retries=3, timeout=120.0)
try:
response = client.chat.completions.create(
model="claude-sonnet-4-6",
messages=[{"role": "user", "content": "Hello"}],
)
except AuthenticationError:
print("Invalid API key")
except RateLimitError:
print("Spending limit reached or rate limited")
except APIError as e:
print(f"API error: {e.message}")