Files
Vorleser-Kit/docs/OLLAMA_SPEECH_API.md
T

6.2 KiB
Raw Blame History

Ollama speech API contract

Sources: Ollama speech documentation (https://docs.ollama.com/capabilities/speech), Ollama blog "Speech models" (https://ollama.com/blog/speech-models), Gradium Phonon voice catalogue.

This file is the contract VorleserOllama implements. Anything marked VERIFY is not fully specified in the public docs. Confirm it against a real server with scripts/capture-fixtures.sh before you rely on it, and update this file with the observed behaviour.

Server requirements

  • Ollama ≥ 0.40.9 (documented minimum for speech). Check with GET /api/version → {"version":"0.40.x"}. Compare it semantically and raise ollamaTooOld if it is lower.
  • Apple Silicon Mac. Linux/Windows support was announced as forthcoming.
  • Default base URL: http://127.0.0.1:11434.

1. Synthesize speech

POST /v1/audio/speech (OpenAI-compatible)

Request: send exactly these fields and nothing else.

{
  "model": "phonon",
  "input": "Text to speak, at most 4096 characters.",
  "voice": "default",
  "response_format": "pcm"
}
Field Rule
model Installed speech model tag, e.g. phonon, phonon:40m-de.
input ≤ 4,096 characters. Vorleser caps at 1,500 (see ARCHITECTURE §6.2). Must be non-empty after trimming.
voice A name from /api/show voices, or default. Case- and accent-insensitive.
response_format pcm (Vorleser default) or wav. Others → 400.

Do not send speed, instructions, or other OpenAI fields. Unsupported options return 400; the server does not ignore them.

Response (200):

Format Body
pcm Raw signed 16-bit little-endian, mono, 24,000 Hz, no header. Streamed during generation.
wav 16-bit mono 24 kHz WAV. The header is written first with size fields set to their maximum value (length unknown), so you cannot trust RIFF/data sizes.

Streaming: the body arrives incrementally. Playback can start on the first chunk. Consume it with URLSession.bytes(for:).

Byte math: 24,000 samples/s × 2 bytes = 48,000 bytes/s. A 100 ms chunk = 4,800 bytes.

Text handling done by the server

  • Prices, numbers, symbols, e-mail addresses, and URLs are spelled out (e.g. $4.59 → "4 dollars and 59 cents").
  • Times and numeric dates are read as written. Write dates out for natural reading.
  • Emoji, bullet characters, and markdown markers (*, #) are skipped.

Consequence for Vorleser: do not pre-expand numbers/symbols. Keep the text faithful.

2. Model information and voices

POST /api/show

{ "model": "phonon" }

Relevant response fields:

Field Meaning
capabilities Array of strings. Speech models include "speech".
voices Model-specific voice list. VERIFY element shape: it is either an array of strings or an array of objects with a name field. Decode both.

Defensive decoding:

enum VoiceEntry: Decodable {
    case name(String)
    case object(name: String)
    init(from d: Decoder) throws {
        let c = try d.singleValueContainer()
        if let s = try? c.decode(String.self) { self = .name(s); return }
        struct O: Decodable { let name: String }
        self = .object(name: try c.decode(O.self).name)
    }
}

If voices is missing or empty, treat the list as ["default"].

CLI equivalent for humans: ollama show phonon.

3. Installed models

GET /api/tags → {"models":[{"name":"phonon:latest", "model":"phonon:latest", …}]}

Find the speech models: for each tag whose name starts with phonon, call /api/show and keep it if capabilities contains speech. Probing every installed model is expensive, so only probe others on "Refresh models" in Settings. Normalise phonon:latest ↔ phonon when you compare against routing settings.

4. Error classification

Condition HTTP / transport Map to
Connection refused / host unreachable URLError.cannotConnectToHost, .cannotFindHost, .timedOut (connect) ollamaUnreachable
Model not installed VERIFY: typically 404 with body containing not found modelNotInstalled(model)
Unknown voice 400, body lists the available voices unknownVoice(voice, available). Parse names out of the message best-effort; fall back to re-querying /api/show.
Model is not a speech model 400 modelNotSpeech (if body mentions speech/capability), else badRequest
Unsupported option / format 400 badRequest(message). This means a bug in Vorleser, so log at .fault.
Other 4xx/5xx status server(status, message)
Stream stalls > 15 s idle timer server(status: 0, message: "stalled"). Retry once.
Task cancelled CancellationError / URLError.cancelled cancelled. This is not an error for UI purposes.

Error bodies from Ollama are JSON {"error":"…"}. Fall back to the raw text.

5. Warm-up

To reduce the first-request latency, send a one-word synthesis ("input": "Ready.") with the routed model and discard the audio. Do this on app launch (if keepModelWarm) and when the routed model changes. Only for loopback endpoints.

6. Fixtures

scripts/capture-fixtures.sh records the following into Packages/VorleserKit/Tests/VorleserOllamaTests/Fixtures/:

  • version.json, tags.json
  • show-<model>.json for each installed phonon model
  • speech-<model>-short.pcm (one sentence) and the HTTP status/headers
  • error-unknown-voice.json, error-missing-model.json, error-unsupported-option.json

Unit tests use these through a URLProtocol stub. Keep fixtures small (< 200 KB each).

7. Known models (for defaults and Settings hints only — never hard-code as "available")

Tag Language Params Licence
phonon / phonon:31m-en English 31M Apache-2.0
phonon:40m-en English 40M CC BY-NC 4.0
phonon:40m-de German 40M CC BY-NC 4.0
phonon:40m-fr French 40M CC BY-NC 4.0
phonon:40m-es Spanish 40M CC BY-NC 4.0
phonon:40m-pt Portuguese 40M CC BY-NC 4.0
phonon:40m-multilingual en, de, fr, es, pt 40M CC BY-NC 4.0

The upstream Gradium catalogue contains 102 voices (EN-US, EN-UK, EN characters, DE, FR, ES-ES, ES-MX, PT-BR). What a given Ollama model exposes is defined only by its /api/show voices field.