Skip to main content
Text to speech (TTS) turns written text into audio. The gateway supports two ways to call it:
ApproachUse forBase path
Unified API (OpenAI-compatible)OpenAI, Azure OpenAI, Groqhttps://{controlPlaneUrl}/api/llm
Native SDKDeepgram, Cartesia, ElevenLabs, Gemini, Vertexhttps://{controlPlaneUrl}/api/llm/tts/{providerAccountName}
Before you start: Replace {controlPlaneUrl} with your gateway URL and your-tfy-api-key with your TrueFoundry API key. For the native SDK, replace {providerAccountName} with the display name of your provider account on TrueFoundry.
Model names: For audio (STT/TTS), the model ID in code must match the display name of the model on your TrueFoundry provider account.
Which SDK to use: For OpenAI, Azure OpenAI, and Groq, use the OpenAI SDK (same API). For Deepgram, Cartesia, ElevenLabs, Gemini, and Vertex, use each provider’s native SDK with the gateway URL above.

Code snippets

from pathlib import Path
from openai import OpenAI

BASE_URL = "https://{controlPlaneUrl}/api/llm"
API_KEY = "your-tfy-api-key"

client = OpenAI(api_key=API_KEY, base_url=BASE_URL)

output_path = Path(__file__).parent / "output.mp3"
with client.audio.speech.with_streaming_response.create(
    model="openai-main/gpt-4o-mini-tts",  # truefoundry model name
    voice="alloy",
    input="Hello, how are you?",
) as response:
    response.stream_to_file(output_path)

print(output_path)

Response

TTS responses are audio bytes. Each snippet above writes the audio to a file and prints the output path.