OpenAI SDK (Python, JS)
Call eWest with the official OpenAI libraries.
The official OpenAI libraries work with eWest: chat (/v1/chat/completions), the Responses API (/v1/responses), models (/v1/models and /v1/models/{id}) and the image, audio and embedding endpoints. Install with pip install openai or npm install openai.
Setup
Pass base_url (Python) or baseURL (JavaScript) as https://api.ewest.ai/v1, and your eWest key as the API key; it is sent as Authorization: Bearer. Or set OPENAI_BASE_URL and OPENAI_API_KEY and create the client without arguments.
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
# GET /v1/models/{id}: free, and proves the key and base URL work.
print(client.models.retrieve("ewest/gemini-flash").id)
Choose a model
client.models.list() returns the models your key may use. client.models.retrieve(id) returns one, with its context_window, input_modalities, supports_tools and supports_streaming. Names need the ewest/ prefix.
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
# Every model your key may call. Use the id as model=...
for model in client.models.list():
print(model.id)
Verify it works
A short chat completion. Every response has an x-request-id header; paste it into the console under Requests to see the request (trace IDs).
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
response = client.chat.completions.create(
model="ewest/gemini-flash",
max_tokens=512,
messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(response.choices[0].message.content)
Streaming and tools
- Stream long answers (
stream=True): without streaming, the whole answer has to arrive within about 90 seconds. - Tool calls use the standard
toolsformat, on chat completions and on the Responses API. ewest/gemini-flashdoes not think by default. When you turn reasoning on (reasoning_effortlow, medium or high), give it room inmax_tokens(512 or more): thinking uses output tokens, and a small limit can return empty text or a cut-off tool call.- On the Responses API, conversations are stored by default: pass the earlier response's
idasprevious_response_idand send only the newinput, or sendstore=Falseand the full conversation every time. See Responses.
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
# Streaming: print the answer as it arrives.
stream = client.chat.completions.create(
model="ewest/gemini-flash",
max_tokens=512,
messages=[{"role": "user", "content": "Write one sentence about the sea."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print()
# Tool calling: the model asks for get_weather, your code runs it.
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
response = client.chat.completions.create(
model="ewest/claude-haiku",
max_tokens=512,
messages=[{"role": "user", "content": "What is the weather in Paris?"}],
tools=tools,
)
for call in response.choices[0].message.tool_calls or []:
print("tool call:", call.function.name, call.function.arguments)
Troubleshooting
API errors raise APIStatusError with the status and body; the Errors page explains each code.
401: the key is missing, mistyped or revoked. Copy it again from API keys.404withmodel_not_found: the model name needs theewest/prefix (ewest/claude-haiku, notclaude-haiku) and must be in Models.403: your key is not allowed to use that model;402: the key's budget is used up. See Errors.- Empty answer from
ewest/gemini-flashwith reasoning on: raisemax_tokens, or setreasoning_efforttonone.
import os
import openai
from openai import OpenAI
client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
try:
response = client.chat.completions.create(
model="ewest/gemini-flash",
max_tokens=512,
messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(response.choices[0].message.content)
except openai.APIStatusError as e:
# 401 key, 402 budget, 403 model not allowed for this key, 404 unknown model id.
print(e.status_code, e.body)
print("trace:", e.response.headers.get("x-request-id"))
Still stuck? Follow Troubleshoot a request.