Skip to main content

SEG Python SDK

PyPI version

The official Python library for the SovereignEG (SEG) API, a MENA-first, OpenAI-compatible AI inference platform. It provides typed synchronous and asynchronous clients for chat completions, embeddings, image generation, moderation, model listing, and decisions, built on httpx2 and Pydantic.

Installation

pip install seg-sdk

To update to the latest version:

pip install --upgrade seg-sdk

Requires Python 3.10 or newer. The distribution is seg-sdk; the import name is seg.

Usage

Create an API key in the SovereignEG dashboard and export it:

export SEG_API_KEY="sk-..."
from seg import SEG

client = SEG()  # reads SEG_API_KEY from the environment

completion = client.chat.completions.create(
    model="llama-3.1-8b-instruct",
    messages=[{"role": "user", "content": "Say hello in one short sentence."}],
    max_tokens=300,
)
print(completion.choices[0].message.content)

Pick a model from client.models.list(); the catalog depends on your plan. Reasoning models may spend the max_tokens budget thinking before answering, and expose that thinking as extra fields on the message (reasoning / reasoning_details, or reasoning_content depending on the serving provider).

Client options

Option Environment variable Default
api_key SEG_API_KEY required
base_url SEG_BASE_URL https://sovereigneg.com/v1
timeout 10 minutes
max_retries 2
default_headers none
http_client an httpx2.Client

api_key may also be a callable that returns the key; it is called before every request, which is useful for keys that rotate.

from seg import SEG

client = SEG(api_key="sk-...", base_url="http://127.0.0.1:8000/v1", timeout=30.0, max_retries=3)

Every option can also be set at module level for quick scripts:

import seg

seg.api_key = "sk-..."
print(seg.models.list().data[0].id)

Async usage

Import AsyncSEG and await each call:

import asyncio
from seg import AsyncSEG

client = AsyncSEG()


async def main() -> None:
    completion = await client.chat.completions.create(
        model="llama-3.1-8b-instruct",
        messages=[{"role": "user", "content": "Say hello"}],
    )
    print(completion.choices[0].message.content)


asyncio.run(main())

Install seg-sdk[aiohttp] and pass http_client=DefaultAioHttpClient() to use aiohttp instead of httpx2.

Streaming

Pass stream=True to receive Server-Sent Events as they are produced:

stream = client.chat.completions.create(
    model="llama-3.1-8b-instruct",
    messages=[{"role": "user", "content": "Count from 1 to 5."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

The async client works the same way with async for.

Structured outputs and tools

client.chat.completions.parse() turns a Pydantic model into a JSON schema, sends it as response_format, and parses the reply back into your model. seg.pydantic_function_tool() does the same for tool calls. See helpers.md for the full guide, including streaming helpers.

from pydantic import BaseModel
from seg import SEG


class Answer(BaseModel):
    city: str
    country: str


client = SEG()
completion = client.chat.completions.parse(
    model="llama-3.1-8b-instruct",
    messages=[{"role": "user", "content": "Where is the Great Pyramid?"}],
    response_format=Answer,
)
print(completion.choices[0].message.parsed)

Other resources

# Embeddings
client.embeddings.create(model="<embedding-model>", input="Cairo at night")

# Image generation
client.images.generate(model="<image-model>", prompt="A felucca on the Nile at sunset")

# Moderation
client.moderations.create(input="Some user text")

# Models
client.models.list()
client.models.retrieve("llama-3.1-8b-instruct")

# Decisions: typed questions about one input
client.decisions.create(
    model="<decision-model>",
    input="The package arrived late and the box was damaged.",
    questions=[
        {
            "type": "predicate",
            "name": "is_complaint",
            "instructions": "Is the customer complaining?",
        },
        {
            "type": "choice",
            "name": "sentiment",
            "instructions": "Overall sentiment?",
            "choices": [{"value": "positive"}, {"value": "neutral"}, {"value": "negative"}],
        },
    ],
)

The complete list of methods, parameters, and return types is in api.md.

Errors

Connection failures raise seg.APIConnectionError; non-2xx responses raise a subclass of seg.APIStatusError carrying status_code, request_id, and the decoded error body.

Status code Error type
400 BadRequestError
401 AuthenticationError
403 PermissionDeniedError
404 NotFoundError
422 UnprocessableEntityError
429 RateLimitError
>=500 InternalServerError
N/A APIConnectionError
import seg
from seg import SEG

client = SEG()
try:
    client.chat.completions.create(model="missing-model", messages=[{"role": "user", "content": "hi"}])
except seg.RateLimitError:
    print("Slow down")
except seg.APIStatusError as e:
    print(e.status_code, e.request_id, e.body)

Every response carries an X-Request-Id header, and inference responses (chat, embeddings, images, moderations, decisions) also carry rate-limit headers such as X-RateLimit-Remaining-Requests; read them through with_raw_response.

Retries and timeouts

Connection errors, 408, 409, 429, and 5xx responses are retried twice with exponential backoff by default. Requests time out after 10 minutes. Both can be changed per client or per request:

client = SEG(max_retries=0, timeout=20.0)
client.with_options(max_retries=5, timeout=60.0).chat.completions.create(...)

Advanced

Raw responses and headers

response = client.chat.completions.with_raw_response.create(
    model="llama-3.1-8b-instruct",
    messages=[{"role": "user", "content": "hi"}],
)
print(response.headers.get("X-Request-Id"))
completion = response.parse()  # the parsed ChatCompletion

with_streaming_response does the same without reading the body eagerly; use it inside a with block.

Undocumented requests and parameters

client.get, client.post, and friends send arbitrary requests. Every method also accepts extra_query, extra_body, and extra_headers for parameters the SDK does not know about yet.

Custom HTTP client

import httpx2
from seg import SEG, DefaultHttpxClient

client = SEG(
    base_url="http://localhost:8000/v1",
    http_client=DefaultHttpxClient(
        proxy="http://my.proxy:3128", transport=httpx2.HTTPTransport(local_address="0.0.0.0")
    ),
)

Use the client as a context manager (with SEG() as client:) or call client.close() to release connections.

Logging

Set SEG_LOG=debug to log each request's method, response status, retries, and request id. Headers, bodies, and the API key are never written to the log.

Supported API surface

Resource Endpoint
client.chat.completions POST /v1/chat/completions
client.embeddings POST /v1/embeddings
client.images.generate POST /v1/images/generations
client.moderations POST /v1/moderations
client.models GET /v1/models, GET /v1/models/{id}
client.decisions POST /v1/decisions

Versioning

This package follows SemVer. Changes that only affect static types, internal modules (prefixed with _), or that fix bugs are not considered breaking.

Requirements

Python 3.10 or higher.

Contributing

See CONTRIBUTING.md.

License

Apache License 2.0. This SDK is derived from the openai-python client, Copyright OpenAI, used under the same license; see NOTICE for attributions.

Metadata

Release files for seg-sdk 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for seg-sdk 0.1.2
File Size Uploaded
seg_sdk-0.1.2.tar.gz 416.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for seg-sdk 0.1.2
File Interpreter ABI Platform
seg_sdk-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 640.9 kB

Release files / seg_sdk-0.1.2.tar.gz

Download URL seg_sdk-0.1.2.tar.gz
Size 416.3 kB
Tags Source
SHA-256 checksum
How to use checksums
277d843aae522d367d99be16bf05619b65765b6134b7c3135275aa37899cf632
BLAKE2b-256 checksum
How to use checksums
40e1f89d640a392d83f932a842b24d5df2f9b4a0a89a1ad38526d918c03f1b29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / seg_sdk-0.1.2-py3-none-any.whl

Download URL seg_sdk-0.1.2-py3-none-any.whl
Size 224.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f91b019bbcfa09b6cd6bbb5638f713ae67e50db7cab9396c1804e8ad27f710c9
BLAKE2b-256 checksum
How to use checksums
b4e6f0156c11d9bd4f0e721284e77a92c0eb70be5f75b65037d4f7e6e44508c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page