> ## Documentation Index
> Fetch the complete documentation index at: https://docs.finesse.flamapp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK

> The official Python library for Finesse, on PyPI as flam-finesse: sync and async clients, speech, streaming and realtime.

The Finesse Python SDK wraps the API in typed **sync and async** clients. It covers voices,
cloning, one-shot and streamed speech, and realtime sessions for voice agents, and it handles
retries, timeouts and errors for you.

> [card] [PyPI](https://pypi.org/project/flam-finesse/)
>
> `flam-finesse`, for Python 3.10+. Built on `httpx`, Pydantic and `websockets`.

> [card] [Source on GitHub](https://github.com/homingos/finesse-python)
>
> `homingos/finesse-python`, Apache-2.0. The full README and every option live there.

## Install

```bash pip theme={null}
pip install flam-finesse
```

```bash uv theme={null}
uv add flam-finesse
```

The package is `flam-finesse`; you import it as `flam_finesse`.

Create an API key in the [dashboard](https://finesse.flamapp.ai/dashboard/developers?tab=api-keys)
under **Developers → API Keys**, and put it in your environment, where the client reads it:

```bash theme={null}
export FINESSE_API_KEY="flv_..."
```

Keep the key on your server. See [Authentication](https://docs.finesse.flamapp.ai/guides/authentication.md).

## Hello, Finesse

```python hello.py theme={null}
from flam_finesse import Finesse

client = Finesse()  # reads FINESSE_API_KEY

# Pick any voice you can use: your own, or one from the public catalog.
voice = next(v for v in client.voices.list() if v.is_ready)

audio = client.speech.create(voice_id=voice.id, text="Hello from Finesse!")
audio.save("hello.wav")
```

`hello.wav` is a 24 kHz WAV file.

## Text to speech

`speech.create` returns the whole clip in one response. `speech.stream` sends audio as it is
generated, so playback can start before the clip is finished:

```python theme={null}
with client.speech.stream(voice_id=voice.id, text="Hello, streamed.") as stream:
    for chunk in stream:
        speaker.write(chunk.pcm)  # raw PCM: s16le, 24 kHz, mono
```

Both take `output_format` (`"wav"`, raw PCM formats, or `"mulaw"` for phone calls) and
`sample_rate`. See [Synthesize speech](https://docs.finesse.flamapp.ai/voice-api/synthesize-speech.md) for every parameter.

## Async

`AsyncFinesse` has the same methods as `Finesse`, with `await`:

```python theme={null}
import asyncio
from flam_finesse import AsyncFinesse


async def main() -> None:
    async with AsyncFinesse() as client:
        audio = await client.speech.create(voice_id=VOICE_ID, text="Hello!")
        audio.save("hello.wav")


asyncio.run(main())
```

## Realtime

Realtime is for voice agents: one WebSocket, text in as your LLM writes it, audio back as fast as
the engine makes it, and cancel on barge-in. It uses the async client:

```python theme={null}
import asyncio
from flam_finesse import AsyncFinesse, AudioEvent


async def main() -> None:
    client = AsyncFinesse()
    async with client.realtime.connect() as rt:
        async for event in rt.say(voice_id=VOICE_ID, text="Hi! How can I help you today?"):
            if isinstance(event, AudioEvent):
                speaker.write(event.pcm)  # s16le, 24 kHz, mono


asyncio.run(main())
```

To stream an LLM reply, open a context, `send` text as it arrives and `finish()` the turn; call
`cancel()` when the user interrupts. The concepts, events and limits are the same as in the
TypeScript [Realtime](https://docs.finesse.flamapp.ai/sdks/realtime.md) guide.

## Direct sessions

For the lowest latency, a direct session runs the socket straight against the synthesis GPU in your
account's region:

```python theme={null}
async with client.realtime.connect(
    get_session=lambda: client.realtime.sessions.create(voice_ids=[VOICE_ID])
) as rt:
    async for event in rt.say(voice_id=VOICE_ID, text="Hello, straight from the GPU."):
        ...
    print(rt.direct_session.region)  # "ap-south" or "us-east"
```

A session is single use, limited to the voices you list, and has a character budget and a one-hour
lifetime: see [Direct sessions](https://docs.finesse.flamapp.ai/sdks/realtime.md#direct-sessions-lowest-latency) and
[Create a direct realtime session](https://docs.finesse.flamapp.ai/voice-api/realtime-sessions.md). To connect from somewhere that
holds no API key, create the session on your server and open it there with `connect_direct`.

## Errors

Every error derives from `flam_finesse.FinesseError`. HTTP errors carry `status_code`, a
machine-readable `code` (branch on this, not on the message), `retry_after` and `request_id`:

```python theme={null}
import flam_finesse as ff

try:
    client.speech.create(voice_id=voice.id, text="Hello!")
except ff.PaymentRequiredError as e:
    print("Out of credits:", e.code)
except ff.RateLimitError as e:
    print("Slow down; retry after", e.retry_after)
except ff.APIStatusError as e:
    print(e.status_code, e.code, e.request_id)
```

Requests that cost credits send an `Idempotency-Key`, so the client's automatic retries never bill
twice. See [Errors](https://docs.finesse.flamapp.ai/voice-api/errors.md) for every code.

## More

The [README on GitHub](https://github.com/homingos/finesse-python) covers everything else: cloning
a voice, short-lived tokens, audio formats, per-request options, timeouts and logging.


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