Skip to main content

Custom TTS

Implement a custom provider when none of the built-in TTS engines meet your requirements. This guide covers the component class, streaming modes, and credentials configuration.
Create Custom TTS with SkillsRasa MCP Tools includes AI Skills to scaffold and audit your custom TTS component with your IDE copilot. Run rasa tools init in your project root to install the agent skills.
You can implement your own custom TTS component as a Python class to integrate with any third-party text-to-speech service. A custom TTS component must subclass the TTSEngine class from rasa.core.channels.voice_stream.tts.tts_engine. Your custom TTS component must output audio in the RasaAudioBytes format and convert it using the engine_bytes_to_rasa_audio_bytes method.

Component methods

Implement name(), from_config_dict(config, format, rasa_language, additional_languages), and get_default_config(rasa_language) for your component. For streaming input, also implement send_text_chunk(), signal_text_done(), and stream_audio(). The base class provides connect(), close_connection(), synthesize(), and audio wrapping; override them when your provider needs custom connection, synthesis, cleanup, or audio conversion behavior.

Optional Methods

You may also override these methods as needed:
  • connect(config: Optional[T]): Establish connection to the TTS engine if necessary. Default implementation does nothing.
  • close_connection(): Custom cleanup when closing connections (e.g., closing websockets or HTTP sessions). Default implementation does nothing.
  • send_text_chunk(text: str): Send text chunks to the TTS system for streaming synthesis. Used with stream_audio() for real-time streaming.
  • signal_text_done(): Signal TTS engine to process any remaining buffered text and prepare to end the stream.
  • stream_audio(): Stream audio output from the TTS engine. Continuously yields audio chunks as they are produced by the engine.

Streaming vs Non-Streaming TTS

The TTSEngine supports both streaming and non-streaming modes:
  • Non-streaming: Implement only synthesize() method for simple request-response synthesis
  • Streaming: Set streaming_input = True and implement send_text_chunk(), signal_text_done(), and stream_audio() methods for real-time streaming synthesis

Required Class Attributes

You can optionally define these class attributes:
  • required_env_vars: Tuple of required environment variable names
  • required_packages: Tuple of required Python package names
  • streaming_input: Boolean indicating if the engine supports streaming input (defaults to False)
See Configure a custom TTS component for the credentials configuration.

Example Implementation

Here’s an example of an implementation for TTS streaming:
custom_tts.py
Non-Streaming TTSIf your TTS service doesn’t support continuous input streaming (i.e., you need to send all text at once), set streaming_input = False and only implement the synthesize() method. You can skip implementing connect(), send_text_chunk(), signal_text_done(), and stream_audio() methods. The synthesize() method should handle the entire synthesis process from text to audio.

Configure a custom TTS component

To use a custom TTS component, configure its module path and settings in your credentials.yml file. The module path follows the format path.to.module.ClassName. For example, a class MyTTS in addons/custom_tts.py has module path addons.custom_tts.MyTTS.

Custom TTS Configuration Example

credentials.yml
Any custom parameters you define in your configuration class (e.g., MyASRConfig or MyTTSConfig) can be passed through the credentials file and will be available in your component via self.config. Set the MY_TTS_API_KEY environment variable before starting Rasa. The example reads this variable when it opens the provider connection.