# Python SDK

> Install the antarya package and send requests from Python.

## Install

```python
pip install antarya
```

## Client

The client reads `ANTARYA_API_KEY`. Pass `api_key="ak-..."` to use a different key.

```python
from antarya import Antarya

client = Antarya()

reply = client.chat.create(
    messages=[{"role": "user", "content": "Summarise this contract's termination clause."}],
)
print(reply.content[0].text)
```

## Streaming

Call `client.chat.stream` (or `client.chat.create` with `stream=True`) and read the events as they arrive.

```python
with client.chat.stream(
    messages=[{"role": "user", "content": "Draft a reply that declines the meeting."}],
) as stream:
    for text in stream.text:
        print(text, end="")
```

## Files and images

A message can carry files and images next to its text. Upload a file once and refer to it by its id in any message, or send a file or an image inside the request, where it is read for that request only and not stored. Files can be PDF, Word, Excel, text, Markdown, CSV or JSON, up to 10 MB each. Images can be PNG, JPEG or WebP, up to 10 MB each and 20 a request. The [files and images guide](https://antarya.ai/docs/guides/files-and-images.md) has more.

```python
import base64

# Upload once, then refer to the file by id.
file = client.files.create(file="agreement.pdf")

reply = client.chat.create(
    messages=[{
        "role": "user",
        "content": [
            {"type": "file", "file_id": file.id},
            {"type": "text", "text": "Summarise the termination clause in this agreement."},
        ],
    }],
)

# Or send the file inside the request, without storing it.
with open("agreement.pdf", "rb") as f:
    data = base64.b64encode(f.read()).decode()

reply = client.chat.create(
    messages=[{
        "role": "user",
        "content": [
            {"type": "file", "source": {"type": "base64", "filename": "agreement.pdf", "media_type": "application/pdf", "data": data}},
            {"type": "text", "text": "Summarise the termination clause in this agreement."},
        ],
    }],
)

# An image goes the same way.
with open("signature-page.png", "rb") as f:
    image = base64.b64encode(f.read()).decode()

reply = client.chat.create(
    messages=[{
        "role": "user",
        "content": [
            {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": image}},
            {"type": "text", "text": "Who signed this page?"},
        ],
    }],
)
```

## Sessions

A session keeps the conversation on Antarya's side, so you send only the new turn. See the [sessions guide](https://antarya.ai/docs/guides/sessions.md) for how it works and the [sessions reference](https://antarya.ai/docs/api/sessions.md) for its limits. The package has these calls from version 0.1.0.

```python
session = client.sessions.create(metadata={"matter": "acme-msa"})

reply = client.chat.create(
    session_id=session.id,
    messages=[{"role": "user", "content": "Summarise the termination clause."}],
)

page = client.sessions.list(limit=20)
if page.has_more:
    page = client.sessions.list(limit=20, after=page.data[-1].id)

saved = client.sessions.get(session.id)
print(saved.messages)

client.sessions.delete(session.id)
```

`create` returns `id`, `object`, `created_at`, `updated_at` and `metadata`, with ids like `sess_4f2a9c1e7b3d48a6915e0c2d8f7a6b31`. `get` adds `messages`. `list` returns `object`, `data` and `has_more`, and `client.files.list` pages the same way. `delete` returns `id` and `deleted`.

## Errors and retries

A failed request raises `antarya.APIError`. It carries `status`, `type`, `message` and `request_id`. The client retries 429 and 5xx replies twice, with a short wait between tries. Change that with `Antarya(max_retries=4)`.

```python
import antarya

try:
    client.chat.create(messages=[{"role": "user", "content": "Hello"}])
except antarya.APIError as e:
    print(e.status, e.type, e.request_id)
```

## Async

```python
import asyncio
from antarya import AsyncAntarya

client = AsyncAntarya()

async def main():
    reply = await client.chat.create(
        messages=[{"role": "user", "content": "Summarise this contract's termination clause."}],
    )
    print(reply.content[0].text)

asyncio.run(main())
```

## Next steps

- [TypeScript SDK](https://antarya.ai/docs/sdk/typescript.md): The same set for TypeScript.
- [CLI](https://antarya.ai/docs/cli.md): Ask from your terminal.
- [Quickstart](https://antarya.ai/docs/quickstart.md): Make a key and send your first request.
