> ## Documentation Index
> Fetch the complete documentation index at: https://servamind.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Client SDK

> The official Python client for the Serva Encoder: one client for encode, decode, Hugging Face, and PyTorch.

The **`serva`** Python package is the official client for the Serva Encoder. You install it once, create a single **`Serva`** client, and every capability hangs off that one object: encoding files to **`.serva`**, decoding them back, pushing and pulling encoded datasets on the Hugging Face Hub, and loading `.serva` files straight into PyTorch.

> [!NOTE]
>
> The SDK wraps the [REST API](https://servamind.mintlify.site/reference/api-library.md) and handles the multi step
> streaming flow for you. For most cases it is the smoothest way to work with
> `.serva` files.

## Install

```bash theme={null}
pip install serva
```

PyTorch ships with the package, so `ServaDataset` works out of the box with nothing extra to install.

## The Serva client

Generate a **Service API key** from the [dashboard](https://serva.servamind.com) (Settings → API keys), then pass it to the client or set `SERVA_API_KEY` in your environment.

```python theme={null}
from serva import Serva

client = Serva(api_key="sk_live_...")   # or set SERVA_API_KEY
```

The client accepts a few optional settings:

| Setting | How | Default |
| - | - | - |
| API key | `Serva(api_key=...)` or `SERVA_API_KEY` | required |
| Base URL | `Serva(base_url=...)` or `SERVA_BASE_URL` | production API |
| Timeout | `Serva(timeout=...)` | client default |
| Hugging Face token | `Serva(hf_token=...)` or `HF_TOKEN` | none |

## Encode and decode

Encoding turns a file into the portable `.serva` format. Decoding turns it back. Both are a single call that runs the initiate, stream, and download steps for you.

```python theme={null}
result = client.encode("dataset.csv")
print(result.output_path, result.savings_percent)

client.decode("dataset.serva")
```

By default the output lands next to the source with a `.serva` suffix. Pass `output=` to choose the path.

Rows of numbers encode the same way. They are staged as a float32 `.npy` and encoded as a file. An array has no filename to borrow, so pass `output=` to name the result:

```python theme={null}
client.encode([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], output="rows.serva")
```

Encoding takes no password. `decode` still accepts one, for a `.serva` issued while encoding still locked files:

```python theme={null}
client.decode("older.serva", password="my-secret", output="older.csv")
```

The returned `EncodeResult` carries the details of the run:

| Field | Meaning |
| - | - |
| `output_path` | Where the `.serva` file was written |
| `file_id` | Server side id for the encoded file |
| `original_size_bytes` | Size of the source file |
| `encoded_size_bytes` | Size of the `.serva` file |
| `savings_percent` | How much smaller the encoded file is |
| `roundtrip_ok` | Whether the server verified a lossless roundtrip |

## Datasets and training

* **[Hugging Face](https://servamind.mintlify.site/sdk/huggingface.md).** Push encoded datasets to the Hub and pull them back, with a dataset card written for you.
* **[PyTorch](https://servamind.mintlify.site/sdk/pytorch.md).** Load a folder of `.serva` files into a training loop with `ServaDataset`.

> [!NOTE]
>
> A `.serva` does not carry the original filename, so decoding cannot restore it.
> Pass `output=` to name the decoded file; without it you get the `.serva` name
> with a `.out` suffix.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.