Python SDK

SovereignEG works with the official OpenAI Python SDK, so you don't need a custom SDK. Point base_url at SovereignEG and use your SovereignEG API key. Supported: chat completions (client.chat.completions, including streaming and tools), embeddings (client.embeddings), image generation on image models (client.images.generate) and client.models.list(). Not available (each returns 404 route_not_found): the Responses API (client.responses), legacy client.completions, Assistants and threads, audio, files, batches, fine-tuning and image edits. Switch tools that default to the Responses API, such as the OpenAI Agents SDK, to Chat Completions.

Installation

pip install openai

Setup

from openai import OpenAI
 
client = OpenAI(
    api_key="sk-...",
    base_url="https://backend.sovereigneg.com/v1"
)

Or use environment variables:

export OPENAI_API_KEY=sk-...
export OPENAI_BASE_URL=https://backend.sovereigneg.com/v1
from openai import OpenAI
client = OpenAI()  # reads from env vars

Chat completions

response = client.chat.completions.create(
    model="gpt-oss-20b",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    temperature=0.7,
    max_tokens=256,
)
 
print(response.choices[0].message.content)

Streaming

stream = client.chat.completions.create(
    model="gpt-oss-20b",
    messages=[{"role": "user", "content": "Write a haiku"}],
    stream=True,
)
 
for chunk in stream:
    if not chunk.choices:  # usage-only chunk at the end of the stream
        continue
    content = chunk.choices[0].delta.content or ""
    print(content, end="", flush=True)

Async client

from openai import AsyncOpenAI
import asyncio
 
async_client = AsyncOpenAI(
    api_key="sk-...",
    base_url="https://backend.sovereigneg.com/v1"
)
 
async def main():
    response = await async_client.chat.completions.create(
        model="gpt-oss-20b",
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(response.choices[0].message.content)
 
asyncio.run(main())

Embeddings

response = client.embeddings.create(
    model="embeddinggemma-300m",  # live embed id
    input="Hello from Cairo!"
)
 
print(f"Dimensions: {len(response.data[0].embedding)}")

Function calling

response = client.chat.completions.create(
    model="gpt-oss-20b",
    messages=[{"role": "user", "content": "What's the weather in Cairo?"}],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a city",
            "parameters": {
                "type": "object",
                "properties": {"city": {"type": "string"}},
                "required": ["city"]
            }
        }
    }]
)

Error handling

from openai import APIError, RateLimitError, AuthenticationError
 
try:
    response = client.chat.completions.create(...)
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    retry_after = e.response.headers.get("Retry-After", 60)
    print(f"Rate limited. Retry after {retry_after}s")
except APIError as e:
    print(f"API error: {e.message}")

LangChain integration

from langchain_openai import ChatOpenAI
 
llm = ChatOpenAI(
    model="gpt-oss-20b",
    api_key="sk-...",
    base_url="https://backend.sovereigneg.com/v1"
)
 
response = llm.invoke("Hello from LangChain!")

Full step-by-step guides for LangChain, LangGraph, CrewAI, LlamaIndex and ready-made agent tools are under Frameworks & Tools.