Getting started¶
Install¶
nirmala uses OpenCode as its default model gateway, so you never paste provider API keys into your code. Install OpenCode and sign in once:
The first time nirmala needs a model it starts a private opencode serve on a free localhost
port, in an empty temporary directory, with every tool permission denied for image sessions. It
stops the server when the client is closed or the interpreter exits.
No OpenCode?
Use any OpenAI-compatible endpoint instead:
nirmala.Extractor(nirmala.OpenAICompatibleClient("model-id", base_url=..., api_key=...)).
Your first extraction¶
from datetime import date
from decimal import Decimal
from pydantic import BaseModel
import nirmala
from nirmala import field
class Invoice(BaseModel):
"""the tax invoice"""
number: str = field("Invoice number", verbatim=True, critical=True)
issued: date | None = field("Invoice date")
total: Decimal | None = field("Grand total", critical=True)
result = nirmala.extract("invoice.pdf", Invoice, gate=nirmala.Gate(field=80, average=90))
print(result.data)
print(result.verdict.decision, result.verdict.field_confidence, result.verdict.blockers)
result is an ExtractionResult:
| Attribute | Meaning |
|---|---|
data |
the validated model (None if a required field is invalid) |
values |
grounded, normalized values before validation |
notes |
what grounding changed or could not verify, per field |
pages, mode |
pages shown to the model; text or vision |
verdict |
the decision, when rules or a gate were given |
Choosing a model¶
nirmala.OpenCodeClient("opencode-go/kimi-k3.1-pro") # provider/model as OpenCode lists it
nirmala.OpenCodeClient("openai/gpt-6.2-sol#high") # optional #variant (reasoning effort)
Set NIRMALA_MODEL to change the default everywhere. The judge can use a different model:
nirmala.Judge(model="opencode-go/qwen4-max").