Metadata-Version: 2.4
Name: oneapii
Version: 0.1.0
Summary: Python SDK for the OneAPIi AI gateway
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: httpx<1,>=0.27
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: build>=1; extra == "dev"
Requires-Dist: twine>=6; extra == "dev"
Requires-Dist: mypy>=1; extra == "dev"

# OneAPIi Python SDK

A Python 3.10+ synchronous client for the OneAPIi hosted AI gateway. One gateway
key and interface; no provider keys or provider SDKs are used.

**Gateway status:** this repository contains only the SDK. No backend contract or
implementation was supplied. The [expected gateway contract](docs/gateway-contract.md)
is a proposed integration target, not a claim that these endpoints are live.
Tests use a local mock HTTP server, not a real service.

## Installation

After this release is published:

```sh
pip install oneapii
```

Until then, install this checkout with `python -m pip install .`.

## Usage

Set `ONEAPII_API_KEY` in your environment to your OneAPIi gateway key. The SDK
requires an explicit key and does not load environment variables itself.

```python
import os
from oneapii import OneAPIi

with OneAPIi(api_key=os.environ["ONEAPII_API_KEY"]) as client:
    models = client.models.list()
    print(models["data"])
    response = client.chat.create(
        model="your-gateway-model-id",  # Choose an ID returned by models.list().
        messages=[{"role": "user", "content": "Hello!"}],
    )
    print(response["choices"][0]["message"]["content"])
```

Responses and chunks are JSON dictionaries; fields are accessed with `[]`, not
attributes. Unknown JSON fields are preserved. The SDK checks JSON object framing,
not every response field. The gateway validates messages and model availability.

## Streaming

```python
import os
from oneapii import OneAPIi

with OneAPIi(api_key=os.environ["ONEAPII_API_KEY"]) as client:
    with client.chat.create(
        model="your-gateway-model-id",
        messages=[{"role": "user", "content": "Tell me a story."}],
        stream=True,
    ) as stream:
        for chunk in stream:
            for choice in chunk.get("choices", []):
                print(choice.get("delta", {}).get("content") or "", end="", flush=True)
```

Streaming opens the HTTP request when `create` is called, then decodes SSE events
incrementally without buffering the complete response. Memory scales with the
current event. `[DONE]` closes the stream. Premature EOF is an error. Always use
the stream context manager or call `stream.close()` when stopping early, even if
iteration never starts. Errors can occur both at creation and during iteration.
Use the client context manager or `client.close()` to release its connection pool.

## Timeouts and errors

The default base URL is `https://api.oneapii.com/v1`. To target your own gateway,
pass `base_url="https://your-gateway.example/v1"`. Use HTTP only for local testing.
Redirects are not followed. Environment proxy configuration is disabled, and the
SDK never selects or contacts provider endpoints.

`timeout=60.0` is the default per-operation timeout, not an overall completion
limit. A custom `httpx.Timeout(connect=5, read=120, write=30, pool=5)` is also
accepted. Streaming read timeouts apply while waiting for the next network data.
The SDK does not retry automatically, avoiding accidental duplicate billed calls.

```python
import os
from oneapii import OneAPIi, AuthenticationError, RateLimitError, ProviderError

with OneAPIi(api_key=os.environ["ONEAPII_API_KEY"], timeout=30.0) as client:
    try:
        print(client.models.list())
    except AuthenticationError:
        print("The gateway rejected the OneAPIi key.")
    except RateLimitError as exc:
        print("Rate limited; Retry-After:", exc.retry_after)
    except ProviderError as exc:
        print("Gateway reported provider failure:", exc, exc.request_id)
```

All SDK errors inherit `OneAPIiError`. HTTP errors inherit `APIError` and expose
`status_code`, `code`, `request_id`, and the raw `retry_after` header (seconds or
HTTP date, if provided). 401/403 map to `AuthenticationError`, 429 to
`RateLimitError`, and explicit `error.type="provider_error"` to `ProviderError`.
Other unsuccessful HTTP statuses use `APIError`; generic 5xx errors are not
assumed to be provider failures. `APITimeoutError` and `APIConnectionError` wrap
HTTPX transport failures. `APIResponseError` reports malformed JSON, incorrect
stream content types, or truncated streams.

## Development and release

```sh
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -e '.[dev]'
python -m pytest -q
python -m mypy src/oneapii
python -m build
python -m twine check dist/*
```

After reviewing the contract, version, package metadata, and built artifacts,
publish version 0.1.0 with these exact commands in the activated environment:

```sh
python -m twine check dist/oneapii-0.1.0.tar.gz dist/oneapii-0.1.0-py3-none-any.whl
python -m twine upload --repository pypi dist/oneapii-0.1.0.tar.gz dist/oneapii-0.1.0-py3-none-any.whl
```

Use your PyPI account's upload credentials through Twine's prompt or trusted
credential configuration, never an AI gateway key. Publishing requires ownership
of the `oneapii` PyPI name; availability and account access have not been verified.
No publication is performed by this repository's build or test commands.
