# OpenAI-compatible endpoints

Four endpoints under `/v1/` take the same requests as OpenAI's audio API, so a
program with an "OpenAI base URL" setting (a chat UI, a voice agent framework
or OpenAI's own SDK) can use Mirli for speech and transcription. They run the
same models through the same queue as the [speech endpoints](https://mirli.app/docs/api-speech/index.md).

The base URL is `http://127.0.0.1:17494/v1`. The API key can be any string,
unless the server has an [access token](https://mirli.app/docs/install-access/index.md#access-token);
then the token is the key, since both travel as
`Authorization: Bearer <TOKEN>`.

Each endpoint lists the errors it returns itself. Every error here, the common
errors included, uses OpenAI's
[error envelope](https://mirli.app/docs/api-errors/index.md#openai-error-envelope),
`{ "error": { message, type, param, code } }`. The common errors arrive with
these codes:

| Status | `code` | Cause |
|---|---|---|
| `400` | `invalid_request` | The body failed validation (`400` here, not `422`). `param` names the field |
| `401` | `invalid_api_key` | The access token is wrong or missing |
| `403` | `cross_site_request` | A simple request from a web page on another site |
| `429` | `queue_full` | The job queue is full |
| `429` | `gpu_busy` | A generation holds the GPU. `Retry-After` gives the wait when known |

## Requests from a browser

`POST` requests here do not need the `X-Soundbox` header that `/api/` asks
for. A JSON body passes, because another site's page cannot send one without a
preflight the server's CORS policy refuses. A simple body (multipart,
form-encoded, plain text or none) is refused with `403` when it carries an
untrusted `Origin` or `Sec-Fetch-Site: cross-site`. Browsers add those to every
cross-site `POST`; scripts and SDKs send neither.

## Create speech

`POST /v1/audio/speech`

Reads text aloud and answers with the audio. The model is the one `model`
names when it is a speech model here; otherwise the model the voice was made
on, when it is ready; otherwise a ready speech model, preferring the one in
memory and never one that may charge per read. A name the server does not know
(`tts-1`, which many clients send) falls through to the next choice.

When the model can stream (`supports.streaming` on the
[model object](https://mirli.app/docs/api-models/index.md#model-object)), the input is at most 5,000
characters and the format is `wav`, `pcm` or `sse`, the audio is sent while it
is being made; a streamed WAV has open-ended sizes in its header. Otherwise, or
when the GPU is busy, the read goes through the queue and arrives whole.

| Name | Type | Required | Description |
|---|---|---|---|
| `input` | string | Yes | 1 to 20,000 characters |
| `voice` | string or object | No | See [Supported fields](#speech). Default: the model's default voice |
| `model` | string | No | A Mirli speech model, by id or name |
| `response_format` | string | No | `wav` (as the model made it), `pcm` (headerless 16-bit little-endian mono at 24 kHz) or `mp3`. Default `mp3` |
| `speed` | number | No | 0.25 to 4. Default 1 |
| `instructions` | string | No | Up to 4,096 characters. The first 2,000 are passed on |
| `stream_format` | string | No | `audio` (the default) or `sse` |
| `keep` | boolean | No | Mirli's own. `true` keeps the read in the library; by default it is deleted once sent |

This request reads one line as WAV, with curl or with OpenAI's Python SDK:

```bash title="curl"
curl "http://127.0.0.1:17494/v1/audio/speech" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "tts-1",
    "input": "Hello from Mirli.",
    "voice": "default",
    "response_format": "wav"
  }' \
  -o hello.wav
```

```python title="Python"
from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:17494/v1", api_key="mirli")

with client.audio.speech.with_streaming_response.create(
  model="tts-1",
  voice="default",
  input="Hello from Mirli.",
  response_format="wav",
) as response:
  response.stream_to_file("hello.wav")
```

A successful response is `200 OK` with `audio/wav`, `audio/pcm` or
`audio/mpeg`. `X-Soundbox-Model` and `X-Soundbox-Voice` name what read it,
`X-Soundbox-Sample-Rate` gives the rate of PCM and streamed audio, and
`X-Soundbox-Sound-Id` is the kept sound's id. With no `response_format` and no
`ffmpeg`, the response is the WAV with `X-Soundbox-Format-Fallback: wav`.

With `stream_format: "sse"`, `speech.audio.delta` events carry base64 PCM
(24 kHz mono) in `audio`, and a last `speech.audio.done` carries a `usage` of
zeros. A failure mid-stream is an `error` event; a raw audio stream stops.

| Status | Cause |
|---|---|
| `400` | `model_not_found`: `model` names a Mirli model that cannot speak |
| `400` | `voice_not_found`: no voice has this name. `message` lists the voices |
| `400` | `unsupported_format`: the format is not available, `mp3` needs `ffmpeg`, or `sse` was asked for with a format other than `pcm` |
| `400` | The read cannot start. `code` is the [failure topic](https://mirli.app/docs/api-errors/index.md#failure-object) or `model_error` |
| `404` | `model_not_installed`: the named model is not downloaded, or no speech model is ready |
| `409` | `cancelled`: cancelled from the app |
| `499` | The caller disconnected; the read is cancelled |
| `500` | The read failed. `code` is the failure topic or `generation_failed`; `detail` holds the raw text |

## Create a transcription

`POST /v1/audio/transcriptions`

Turns speech into text. The body is `multipart/form-data`, up to 64 MB. The
model is the one `model` names when it is a ready transcription model,
otherwise a ready one, preferring the one in memory.

| Name | Type | Required | Description |
|---|---|---|---|
| `file` | file | Yes | The audio. WAV is read directly; MP3, M4A, WebM and other formats need `ffmpeg` |
| `model` | string | No | A transcription model, by id or name |
| `language` | string | No | A language hint |
| `response_format` | string | No | `json` (the default), `text` or `verbose_json` |
| `stream` | string | No | `true` or `1` is refused |

This request transcribes `hello.wav`:

```bash
curl "http://127.0.0.1:17494/v1/audio/transcriptions" \
  -F file=@hello.wav \
  -F model=whisper-1
```

A successful response is `200 OK` with the text, and `X-Soundbox-Model` names
the model. `json` is `{ "text" }` and `text` is plain text. `verbose_json` adds
`task`, `language` and `duration` in seconds:

```json
{
  "task": "transcribe",
  "language": "en",
  "duration": 1.62,
  "text": "Hello from Mirli."
}
```

| Status | Cause |
|---|---|
| `400` | `invalid_request`: the body is not multipart, or has no `file` with audio |
| `400` | `unsupported_format`: `response_format` is `srt`, `vtt` or another value |
| `400` | `unsupported_stream`: `stream` is `true` |
| `400` | `invalid_audio`: the file cannot be read as audio |
| `400` | `model_not_found`: `model` names a Mirli model that does not transcribe |
| `404` | `model_not_installed`: no transcription model is ready |
| `409` | `cancelled`: cancelled from the app |
| `499` | The caller disconnected |
| `500` | The transcription failed. `code` is the failure topic or `generation_failed` |

## List models

`GET /v1/models`

Returns the ready speech and transcription models in OpenAI's list shape. This
endpoint takes no parameters.

This request lists the models:

```bash
curl "http://127.0.0.1:17494/v1/models"
```

A successful response is `200 OK` with the list. `name` and `task` (`speech`
or `transcription`) are Mirli's own:

```json
{
  "object": "list",
  "data": [
    {
      "id": "audiocpp-supertonic-3",
      "object": "model",
      "created": 0,
      "owned_by": "Supertone",
      "name": "Supertonic 3 (audio.cpp)",
      "task": "speech"
    }
  ]
}
```

This endpoint returns only the common errors.

## List voices

`GET /v1/audio/voices`

Returns the saved voices and the ready models' own voices. OpenAI has no such
endpoint. This endpoint takes no parameters.

This request lists the voices:

```bash
curl "http://127.0.0.1:17494/v1/audio/voices"
```

A successful response is `200 OK` with the same entries under `data` and
`voices`. `kind` is `clone` or `builtin` for a saved voice, `builtin` for a
model's own voice, and `default` for the model's default voice, listed when a
ready model needs no voice reference. A model's voice named like a saved voice
is left out. An entry's `id` or `name` works as `voice`:

```json
{
  "object": "list",
  "data": [
    {
      "id": "7b3e2a1c-4d5f-4e6a-9b8c-0d1e2f3a4b5c",
      "object": "audio.voice",
      "name": "Ada",
      "kind": "clone",
      "model": "audiocpp-supertonic-3"
    }
  ],
  "voices": [
    {
      "id": "7b3e2a1c-4d5f-4e6a-9b8c-0d1e2f3a4b5c",
      "object": "audio.voice",
      "name": "Ada",
      "kind": "clone",
      "model": "audiocpp-supertonic-3"
    }
  ]
}
```

This endpoint returns only the common errors.

## Supported fields

`/v1/audio/translations` and every OpenAI route outside audio are not provided
and return `404`. `/v1/audio/voices` has no OpenAI equivalent.

### Speech

| OpenAI field | Mirli |
|---|---|
| `model` | **Partly**: a Mirli speech model id or name. Any other name falls back to the voice's model, then a ready one |
| `input` | **Supported**, up to 20,000 characters |
| `voice` | **Supported**: a saved voice id or name (any case), a model's own voice, `default`, or `{ "id" }` |
| `instructions` | **Partly**: passed as `instruct` to models that have it, ignored by others |
| `response_format` | **Partly**: `wav`, `pcm`, and `mp3` with `ffmpeg`. `opus`, `aac` and `flac` are **Refused**, `400` `unsupported_format` |
| `speed` | **Partly**: applied to models with a speed setting, held to the model's range |
| `stream_format` | **Supported**: `audio` and `sse`. `sse` needs `pcm` or no format |

Unknown fields are ignored. Mirli adds `keep`.

### Transcriptions

| OpenAI field | Mirli |
|---|---|
| `file` | **Supported**. Formats other than WAV need `ffmpeg`, else `400` `invalid_audio` |
| `model` | **Partly**: a transcription model id or name; other values pick a ready one. A Mirli model that does not transcribe is `400` `model_not_found` |
| `language` | **Supported**, as a hint |
| `response_format` | **Partly**: `json`, `text`, and `verbose_json` without segments or word timings. `srt` and `vtt` are **Refused**, `400` `unsupported_format` |
| `stream` | **Refused** when true, `400` `unsupported_stream` |
| `prompt`, `temperature`, `timestamp_granularities[]`, `chunking_strategy`, `include[]` | **Ignored** |

### Models

| OpenAI field | Mirli |
|---|---|
| `id`, `object` | **Supported** |
| `created` | **Partly**: always `0` |
| `owned_by` | **Partly**: the model's vendor, or `mirli` |

Mirli adds `name` and `task`.
