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

# Python SDK reference

> Look up client methods, stream events and the binary format of returned media.

`MStarClient` wraps `POST /generate` and ships with `mstar-ai`. Create one client per server address and reuse its session. For a first request, see [Send requests](https://docs.m-star.org/clients.md#python-sdk).

## Construct a client

```python theme={null}
from mstar import MStarClient

client = MStarClient("http://localhost:8000", timeout=600)
```

| Argument | Default | Use |
| - | - | - |
| `base_url` | `http://localhost:8000` | Server root, without a `/v1` suffix. |
| `timeout` | `600.0` seconds | HTTP timeout per request. `health()` uses ten seconds. |
| `session` | new `requests.Session` | Supply your own session for pooling or proxies. |
| `api_key` | `MSTAR_API_KEY` | Bearer credential for authenticated routes. |
| `prefer_binary` | `False` | Request binary frames when streaming. Video-frame output already requests them. |
| `enable_nvtx` | `False` | Record NVTX ranges around CUDA-client reads and decodes. |

## Choose a method

| Method | Result |
| - | - |
| `generate(...)` | `GenerateResult`. Set `stream=True` for an iterator of events. Accepts `text`, `images`, `audio`, `video` and the desired `output_modalities`. |
| `stream(**kwargs)` | Iterator over the same request as `generate(..., stream=True)`. |
| `chat(prompt, ...)` | `GenerateResult` or stream. Accepts images, audio and output modalities. |
| `generate_image(prompt, **kwargs)` | First generated image as bytes. |
| `edit_image(prompt, image, **kwargs)` | Edited image as bytes. For a multi-reference model, supply a list of images. |
| `tts(text, *, voice=None, reference_audio=None, **kwargs)` | `AudioBuffer`. Models that clone voices accept one reference clip. |
| `transcribe(audio, *, language=None, text="", **kwargs)` | Transcript as a string. |
| `voices()` | Preset voice IDs from `/v1/audio/voices`, when available. |
| `health()` | Boolean readiness result. |

Supply media as a path, raw bytes or `(filename, bytes)`. Give an explicit filename for formats whose extension matters. Additional keywords become [model generation parameters](https://docs.m-star.org/clients.md#generation-parameters). A refused request raises `requests.HTTPError`. For streams this happens when iteration begins. A server-side failure after streaming starts raises `RuntimeError` during iteration.

## Read results

A completed `GenerateResult` exposes `.request_id`, `.text`, `.images` and `.audio`. `.raw` contains all decoded chunks with their modality, bytes and metadata. For complete MP4s and binary action arrays, request `stream=False` and read `.raw` to preserve the bytes. Raw RGB frame output uses `stream=True`.

| Stream event | Payload |
| - | - |
| `TextChunk` | `.text` contains incrementally decoded UTF-8. |
| `ImageChunk` | `.data` contains the encoded image. Use `.save(path)`. |
| `AudioChunk` | `.pcm` contains PCM16 samples. Read `.sample_rate` and `.metadata`. |
| `VideoFrameChunk` | `.to_numpy()` returns a read-only uint8 array shaped `[frame_count, height, width, 3]`. Read `.fps` for playback. |

All four events carry `.metadata`. For completed audio, `AudioBuffer.to_wav(path)` preserves the stream’s sample rate and channels. `to_numpy()` returns int16 samples. For completed images, use `GenerateResult.save_image(path, index=0)`.

## Source

For exact Python signatures, see the [client source](https://github.com/mstar-project/mstar/tree/main/mstar/client).


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