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 -> GenerateSpeechResult

Defined in Sources/AI/Core/SpeechGeneration.swift.

Parameters

ParameterTypeDefault
modelany SpeechModelrequired
textStringrequired
voiceString?nil
instructionsString?nil
speedDouble?nil
outputFormatString?nil
providerOptionsJSONValue?nil
maxRetriesInt2

Returns

GenerateSpeechResult with the audio file and providerMetadata.

See also