Python SDK
jaina-sdk — sync and async clients with Pydantic typing.
Install
Heads up:
jaina-sdkis publishing soon and is not yet on PyPI. Until it ships, use the REST API directly. The examples below are correct for when the package is live.
pip install jaina-sdk
Quickstart (sync)
import os
from jaina import JainaClient
with JainaClient(api_key=os.environ["JAINA_API_KEY"]) as jaina:
projects = jaina.projects.list()
for p in projects.items:
print(p.slug)
Quickstart (async)
import asyncio, os
from jaina import AsyncJainaClient
async def main():
async with AsyncJainaClient(api_key=os.environ["JAINA_API_KEY"]) as jaina:
projects = await jaina.projects.list()
for p in projects.items:
print(p.slug)
asyncio.run(main())
Typed records with Pydantic
from pydantic import BaseModel
from jaina import JainaClient
class Enemy(BaseModel):
name: str
hp: int
attack: int
sprite: str
rarity: str
with JainaClient(api_key="...") as jaina:
enemies = jaina.records.list(
"my-game",
"enemy",
model=Enemy,
filter={"data.hp": {"gte": 50}},
)
for e in enemies.items:
print(f"{e.data.name} (HP {e.data.hp})")
Generate the Pydantic model with jaina codegen --lang python.
Pagination
for page in jaina.records.paginate("my-game", "enemy"):
for record in page.items:
process(record)
Or async:
async for page in jaina.records.paginate_async("my-game", "enemy"):
for record in page.items:
await process(record)
Error handling
from jaina import JainaError, ValidationError
try:
jaina.records.create("my-game", "enemy", data={"name": "Goblin"})
except ValidationError as e:
print(e.issues)
except JainaError as e:
print(e.status, e.code, e.message)
Context managers
JainaClient and AsyncJainaClient both implement context-manager protocols. Always use with / async with to ensure the underlying HTTP session is closed.
