OpenAI SDK
Point the official OpenAI SDK at Gradiated. You only need to change the base URL, API key, and model.
Install#
pip install openai
Chat Completions#
from openai import OpenAI
import os
client = OpenAI(
base_url="https://api.gradiated.com",
api_key=os.environ["GRADIATED_API_KEY"],
)
response = client.chat.completions.create(
model="YOUR_MODEL",
messages=[
{"role": "user", "content": "Explain inference in one sentence."}
],
extra_body={"service_tier": "default"},
)
print(response.choices[0].message.content)
Replace YOUR_MODEL with a chat model from models and pricing. Decision models accept only SystemOne requests.
The example uses the default service tier. Change to flex for background agents where latency is less important, or fast when you want faster responses and are happy to pay more.
Responses API#
response = client.responses.create(
model="YOUR_MODEL",
input="Explain inference in one sentence.",
extra_body={"service_tier": "default"},
)
print(response.output_text)
Streaming#
Set stream to true to process output as the model generates it.
stream = client.chat.completions.create(
model="YOUR_MODEL",
messages=[{"role": "user", "content": "Write one short sentence."}],
stream=True,
)
for chunk in stream:
if not chunk.choices:
continue
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
Check models and pricing to confirm that the model supports streaming.
Tool calling#
Send function definitions with the request. The model can then ask your application to run one of them.
response = client.chat.completions.create(
model="YOUR_MODEL",
messages=[{"role": "user", "content": "What is the weather in Warsaw?"}],
tools=[{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}],
)
tool_calls = response.choices[0].message.tool_calls
Your application must validate the arguments and run the function. Gradiated does not run it.