Python SDK
The Python SDK uses HTTPX and Pydantic v2 and requires Python 3.10 or later.
Install nicia-ai on PyPI and import
nicia. Version 0.1.0 wraps the HTTP API, which addresses spaces at
/v1/brains/{handle}.
Install
Section titled “Install”python -m pip install nicia-aiProvision a space and scope access
Section titled “Provision a space and scope access”Use NiciaOrgClient with an organization key (nsk_…) to provision a space
and mint a space key (nbk_…). Omit mode from put_space to preserve an
existing space’s admission mode. Keep the organization key on your backend
and store the minted secret when you receive it.
import os
from nicia import NiciaOrgClient
handle = "tenant:acme"with NiciaOrgClient(api_key=os.environ["NICIA_MANAGEMENT_KEY"]) as org: provisioned = org.put_space(handle, name="Acme") minted = org.mint_space_key( provisioned.space.handle, name="Knowledge pipeline", capabilities=["read", "write"], )
if minted.secret is None: raise RuntimeError("Load the existing space key from your secret store.")space_key = minted.secretWrite documents and cited records
Section titled “Write documents and cited records”Use a stable document ID to upsert the same address. For occurred_at,
pass a timezone-aware datetime or an ISO 8601 string with an offset.
from nicia import NiciaSpaceClientfrom nicia.models import DocumentInput
policy = "Acme renewals require finance approval before a quote goes out."with NiciaSpaceClient(handle, api_key=space_key) as space: uploaded = space.write_documents( DocumentInput( id="renewal-policy", text=policy, occurred_at="2026-11-30T09:00:00-08:00", ) ) print(uploaded.document.status)write_documents accepts a DocumentInput, a dictionary, or a batch of up
to 100. Check the document’s status with list_documents. Once it is
indexed, cite its exact words in a record. query filters accepted records
by kind and declared fields. For paginated reads, pass the result’s
next_cursor as cursor on the next call.
from nicia.models import DocumentAddress, DocumentCitation, RecordInput
with NiciaSpaceClient(handle, api_key=space_key) as space: written = space.write_records( RecordInput( id="acme-renewal-approval", kind="rule", label="Finance approval for renewal quotes", fields={"status": "active", "body": policy}, citations=[ DocumentCitation( document=DocumentAddress(id="renewal-policy"), quote=policy, ) ], ) ) print(written.change.status) matching = space.query("rule", where={"status": "active"}) print(matching.records)batch is an alias for write_records. The space’s admission policy
decides whether a write lands or waits for Review. The examples import
models from nicia.models, where you can also import return types such
as ContextResult for your own annotations.
Read context and check its receipt
Section titled “Read context and check its receipt”context calls /context. Save the receipt alongside your agent’s answer.
Use receipt_now with the key that made the read or an admin key to
check what changed since.
with NiciaSpaceClient(handle, api_key=space_key) as space: answer = space.context("Who needs to approve an Acme renewal quote?") print(answer.text, answer.citations, answer.receipt) print(space.receipt_now(answer.receipt).changed)For historical context on the default branch, pass valid_at for when a
statement applied and known_at for what Nicia had recorded by a given time.
See Ask what was known then.
Use async clients
Section titled “Use async clients”AsyncNiciaOrgClient and AsyncNiciaSpaceClient expose the same operations.
Use async with to close their HTTP sessions and await for requests.
import asyncioimport os
from nicia import AsyncNiciaOrgClient, AsyncNiciaSpaceClientfrom nicia.models import ContextResult
async def read_renewal_policy() -> ContextResult: handle = "tenant:acme" async with AsyncNiciaOrgClient(api_key=os.environ["NICIA_MANAGEMENT_KEY"]) as org: await org.put_space(handle, name="Acme") minted = await org.mint_space_key( handle, name="Async renewal reader", capabilities=["read"] )
if minted.secret is None: raise RuntimeError("Load the existing space key from your secret store.")
async with AsyncNiciaSpaceClient(handle, api_key=minted.secret) as space: return await space.context("Who needs to approve an Acme renewal quote?")
answer = asyncio.run(read_renewal_policy())print(answer.text, answer.citations, answer.receipt)For a custom API host, pass base_url to the client.
Review reopened records and findings
Section titled “Review reopened records and findings”attention lists work waiting on a person. findings lists disagreements.
Use a key with the permissions required for Review.
with NiciaSpaceClient(handle, api_key=os.environ["NICIA_MANAGEMENT_KEY"]) as reviewer: for item in reviewer.attention(reason="reopened").items: print(item.subject.id, item.console_url) for finding in reviewer.findings(resolved=False).findings: print(finding.id, finding.subject, finding.predicate)When a reviewer chooses which statement to keep, pass the receipt and support fingerprint from the exact finding they reviewed. Reuse the same idempotency key for retries of that decision.
from nicia.models import FindingResult
def keep_statement(reviewed: FindingResult, statement_id: str, decision_key: str) -> None: with NiciaSpaceClient(handle, api_key=os.environ["NICIA_MANAGEMENT_KEY"]) as reviewer: decision = reviewer.decide_finding( reviewed.finding.id, keep=statement_id, reviewed_receipt=reviewed.receipt, reviewed_fingerprint=reviewed.finding.support_fingerprint, idempotency_key=decision_key, ) print(decision.outcome.kind)Use reviewer.get_finding(finding_id) to get the FindingResult to show
the reviewer. Pass that result to keep_statement after they choose.
See Review for the available decisions.
Export a space
Section titled “Export a space”Start the job and poll with the same key. wait_for_export returns a
completed or failed job. If the polling window ends, it raises NiciaError
with code export_timeout. Use the same job ID to resume polling.
from pathlib import Path
with NiciaSpaceClient(handle, api_key=space_key) as space: job = space.export(idempotency_key="acme-export-v1") finished = space.wait_for_export(job.id, poll_interval=2, timeout=60) if finished.status == "failed": raise RuntimeError(finished.error or "Export failed") Path("acme-export.zip").write_bytes(space.download_export(job.id))Branches and proposals
Section titled “Branches and proposals”Fork from the space’s accepted knowledge with create_branch and an
idempotency_key.
with NiciaSpaceClient(handle, api_key=space_key) as space: fork = space.create_branch(idempotency_key="acme-renewal-draft-v1") branch_id = fork.branch.branch_idAfter editing the branch, inspect its delta and open a proposal for Review.
with NiciaSpaceClient(handle, api_key=space_key) as space: comparison = space.compare_branch(branch_id) print(comparison.summary) proposal = space.create_proposal(branch_id, intent="Clarify renewal approvals") print(proposal.proposal.id)See Branches and proposals and the runnable LangGraph recipe for a branch edit.
Handle errors and retries
Section titled “Handle errors and retries”Catch NiciaError for status, code, request_id, details, and
attempts. It has four subclasses for common failures:
| Exception | When to handle it |
|---|---|
NiciaAuthError |
The credential is missing, invalid, or lacks access. |
NiciaConflictError |
The request conflicts with current state. |
NiciaRateLimitError |
The API rate limited the request. |
NiciaValidationError |
The SDK or API rejected the input. |
A client-side validation error has no HTTP status and records zero attempts.
The SDK retries transport failures, HTTP 429, and HTTP 503 on GETs and
replay-safe writes carrying an idempotency key. It honors Retry-After up to
60 seconds and defaults to three attempts. Context and query calls create
receipts and are sent once. See Errors and
Idempotency.