Metadata-Version: 2.4
Name: curva-ai
Version: 0.1.0
Summary: Typed decisions with calibrated probabilities from any LLM: Python client and bundled Curva server
Author: Tarkova Private Limited
License-Expression: LicenseRef-Curva-Free-License
Project-URL: Homepage, https://pypi.org/project/curva-ai/
Keywords: llm,classification,calibration,probabilities,structured-output
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: pytest-cov>=5; extra == "dev"
Dynamic: license-file

# curva-ai

The Python package for Curva, which gives typed decisions with calibrated probabilities from any LLM. The SDK uses the standard library only.

```bash
pip install curva-ai
```

On Linux, macOS and Windows the wheel also contains the `curva` server binary, so there is nothing else to install. Set one model key of any provider (`GEMINI_API_KEY`, `OPENROUTER_API_KEY`, `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, `AWS_BEARER_TOKEN_BEDROCK`, `GROQ_API_KEY`, ...), exported or in a `.env` file, and decide:

```python
import curva
d = curva.decide("I was charged twice for order A-104, please refund me",
                 {"team": ["billing", "technical"], "refund": "Asks for a refund?", "order": str})
print(d.team, d.refund, d.order)   # billing True A-104
d["team"].confidence               # how sure, from 0 to 1
```

The first `curva.decide` starts a private server for this Python process and stops it when Python exits. Curva picks a small, fast model of the provider whose key it finds; set `CURVA_MODEL` to choose one. Pictures, PDFs, recordings and cameras go through the same call: `images=["photo.jpg"]`, `sources="contracts"` (after `curva.library("contracts").add("contract.pdf")`), `assets={"call": Asset.audio("call.wav")}`, and `for event in curva.watch("clip.mp4")`.

For every option, `curva.local()` returns the client behind it:

```python
import curva
from curva import Choice, Noul

client = curva.local()   # starts `curva serve` on a free localhost port; it stops when Python exits
d = client.decide(
    state={"ticket": "Charged twice, refund please"},
    questions={
        "team": Choice("Which team?", ["billing", "technical"]),
        "refund": Noul("Asks for a refund"),
    },
)
d["team"].choice, d["team"].confidence, d["refund"].noul
```

`curva.local(model=..., db=...)` passes your environment to the server. Decisions and calibration are kept in `~/.curva/curva.db` unless you pass `db=`, and the server's warnings and errors in `server.log` next to it. Use it as `with curva.local() as client:` to stop the server at the end of the block.

Already running a server (`curva serve`, Docker)? Point a client at it instead:

```python
from curva import Curva
client = Curva("http://localhost:7777")   # or CURVA_BASE_URL; CURVA_API_KEY for auth
```

`client.system_one(state=..., questions=...)` and `Choice(instructions=..., criteria={...})` are accepted as aliases.

Other calls: `feedback(d.id, key, label)`, `calibration(key)`, `audit()`, `models()`, `drift(key, weeks=8)`, `health()`. A Score answer knows its level names, so `d["mood"].level` is the most likely one. `d.request_id` is the server's `x-request-id` header (None if it sends none), for matching server logs.

`Curva(timeout=30.0, max_retries=3, retry_statuses=(429, 500, 502, 503, 504), headers={...})`: retries honour `Retry-After`, and `headers` are sent with every request.

## Async and batches

```python
from curva import AsyncCurva

async with AsyncCurva("http://localhost:7777") as client:   # same arguments and methods as Curva
    d = await client.decide(state, questions)
    results = await client.decide_many([(state1, questions), (state2, questions)], concurrency=8)
```

`decide_many` returns decisions in input order. With `return_exceptions=True` a failed item holds its error instead of raising the first one; other keyword arguments (`project=`, `model=`, ...) go to every call.

`AsyncCurva` stays dependency-free by running the blocking client in a worker thread per in-flight request (`asyncio.to_thread`). That is fine up to hundreds of concurrent calls; the default executor runs at most `min(32, cpus + 4)` at once and queues the rest.

## Route a prompt, and never crash on a failed model

```python
r = curva.route(prompt, {"small-fast": "short questions, lookups", "large-reasoning": "hard reasoning, long code"},
                default="large-reasoning", min_confidence=0.7)
r.model, r.confidence          # which of your models should answer, and how sure Curva is

d = curva.decide(ticket, {"team": ["billing", "technical"]}, fallback={"team": "billing"})
d.fallback                     # True when no model could answer and "billing" is your fallback

c = curva.check({"tool": "refund", "amount": 500}, "Refunds above $200 need a manager's approval.")
c.verdict, c.allowed           # "block", False: check an agent's action against your policy

g = curva.guard(user_message)  # prompt injection, personal data, toxicity
g.verdict                      # "allow", "review" or "block"
```

## Errors

Every error is a `CurvaError` with `status`, `type` and `message` (status 0 = server unreachable). Catch a subclass for one case:

| Class | Status |
|---|---|
| `AuthError` | 401 |
| `InvalidRequestError` | 400, 422 |
| `NotFoundError` | 404 |
| `RateLimitError` | 429, with `retry_after` in seconds |
| `ModelError` | 502, 504 |

A reply that is not JSON, such as a proxy's error page, raises `CurvaError` with type `invalid_response`.

## Run the tests

The tests use pytest; the SDK itself needs nothing beyond the standard library. From `sdk/python`:

```bash
python3 -m venv .venv && .venv/bin/pip install pytest pytest-cov    # the package's `dev` extra
.venv/bin/python -m pytest -m "not e2e"                              # unit tests, no server, about 10 s
.venv/bin/python -m pytest                                           # plus end-to-end against a real `curva serve`
.venv/bin/python -m pytest --cov=curva --cov-report=term-missing     # line coverage of src/curva
```

End-to-end tests (marker `e2e`) run the server with a stand-in model, so they need no key and cost nothing. They use the binary in `CURVA_BIN`, or build it with `cargo build -p curva` (into `CARGO_TARGET_DIR` when set). The older end-to-end files read `target/debug/curva` unless `CURVA_BIN` is set, so set it when you build elsewhere. Tests marked `live` call a real provider through the server at `CURVA_BASE_URL` and run only with `CURVA_LIVE=1`.

© 2026 Tarkova Private Limited. All rights reserved. Free to use under the Curva Free License (the `LICENSE` file in this package); Curva and the Curva logo are trademarks of Tarkova Private Limited.
