Generate speech
Use generateSpeech when a provider exposes text-to-speech.
import Foundationimport SwiftAISDK
@mainstruct GenerateSpeechExample { static func main() async throws { let provider = try AIProviders.openAI() let model = try provider.speechModel("gpt-4o-mini-tts")
let result = try await model.generateSpeech( "Welcome to the SwiftAISDK demo.", voice: "alloy", format: "mp3", speed: 1.0 )
try result.audio.write(to: URL(fileURLWithPath: "welcome.mp3")) }}The protocol method is named speak(_:) for low-level model implementations. Application code should usually prefer the facade spelling generateSpeech.
let result = try await AI.generateSpeech( "Read this in a calm tone.", using: model, instructions: "Calm, precise, and friendly.")Options
Section titled “Options”voiceselects a provider voice.formatrequests an output audio format.speedadjusts playback speed when supported.languagehints the spoken language.instructionsgives delivery or style guidance.providerOptionscarries provider-specific controls.extraBody,headers,abortSignal,retryPolicy, andtelemetryfollow the shared facade behavior.
Results
Section titled “Results”SpeechResult contains the generated bytes and metadata:
audiocontentTypewarningsproviderMetadatarequestMetadataresponseMetadata