generateSpeech
Synthesize speech from text.
Turns text into audio using a speech model. voice, speed, and
outputFormat are passed through to the provider when it supports them.
let result = try await generateSpeech(
model: OpenAISpeechModel("gpt-4o-mini-tts"),
text: "Your order has shipped.",
voice: "alloy"
)Signature
func generateSpeech(
model: any SpeechModel,
text: String,
voice: String? = nil,
instructions: String? = nil,
speed: Double? = nil,
outputFormat: String? = nil,
providerOptions: JSONValue? = nil,
maxRetries: Int = 2
) async throws -> GenerateSpeechResultDefined in Sources/AI/Core/SpeechGeneration.swift.
Parameters
| Parameter | Type | Default |
|---|---|---|
model | any SpeechModel | required |
text | String | required |
voice | String? | nil |
instructions | String? | nil |
speed | Double? | nil |
outputFormat | String? | nil |
providerOptions | JSONValue? | nil |
maxRetries | Int | 2 |
Returns
GenerateSpeechResult with the audio file and providerMetadata.