Metadata-Version: 2.4
Name: melara
Version: 0.1.1
Summary: Official Python Client SDK for Melara.
Author: Melara
License-Expression: Apache-2.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: httpcore>=1.0.9
Requires-Dist: httpx>=0.28.1
Requires-Dist: jsonpath-python>=1.0.6
Requires-Dist: pydantic<2.13,>=2.11.2
Requires-Dist: websockets<18,>=13.0
Dynamic: license-file

# Melara Python SDK

The official Python client for the Melara API, with sync and async clients, streaming and realtime sessions. Python 3.10 or later is required.

```bash
pip install melara
```

## Quick start

```python
import os
from melara import Melara

with Melara(api_key=os.environ["MELARA_API_KEY"],
            app_name="Example app", app_url="https://app.example") as client:
    result = client.chat.send(
        model="your-model-id",
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(result.choices[0].message.content)
```

## Configuration

The client calls Melara's API by default. To use another base URL, set `server_url` or the `MELARA_BASE_URL` environment variable, including `/v1`. An explicit value takes precedence over the environment. The API key comes from `api_key` or `MELARA_API_KEY`; you can also pass a function that returns the key, and it is called for each request.

`app_name` and `app_url` identify your app on every request (sent as `X-Melara-App-Name` and `X-Melara-App-Url`), and can be overridden per call.

## What you can call

- `chat.send` and `responses.send` (`beta.responses` is a deprecated alias)
- `embeddings.generate`
- `models.list`, which returns the model records in `response.result`
- `images.generate`, which returns URL or base64 images
- `tts.create_speech` for text to speech
- `stt.create_transcription_multipart` for speech to text, with an uploaded audio file
- `client.realtime` for live voice and text sessions (below)

Every method has an `_async` counterpart.

## Streaming

Pass `stream=True` to `chat.send` or `responses.send` and iterate the result. Use the stream as a context manager, or close it when you are done. Requests are retried automatically; pass `retry_config=None` to turn retries off. If you pass in your own HTTPX client, you stay responsible for closing it.

## Realtime

`client.realtime.connect(model=...)` opens a voice and text session from your server. Send events as dictionaries and iterate the session to receive events. Iteration ends when the server closes the session normally; an abnormal close raises `RealtimeError` with its `code` and `reason`. `connect_async` is the asyncio version.

```python
with Melara(api_key=os.environ["MELARA_API_KEY"]) as client:
    with client.realtime.connect(model="your-realtime-model") as session:
        session.send({"type": "response.create", "response": {"output_modalities": ["text"]}})
        for event in session:
            if event["type"] == "response.output_text.delta":
                print(event["delta"], end="")
            if event["type"] == "response.done":
                break
```

For a browser app, call `client.realtime.client_secrets.create(model=...)` on your server and hand the returned `value` and `url` to the page. The secret is short-lived and opens one session without exposing your API key.

## Zero data retention

Pass `provider={"zdr": True}` to ask for zero data retention. Whether a request can be served with zero data retention depends on the model you choose.

## Errors

```python
from melara import errors

try:
    result = client.chat.send(model="your-model-id", messages=[])
except errors.PaymentRequiredResponseError as error:
    print(error.data.error.code)  # for example no_credits or credits_exhausted
```

Errors keep the Melara error code, the message, the HTTP status, the headers and the raw response. Error details are meant for developers; show your own message to end users.

## License

Apache-2.0
