Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Elevenlabs Elevenlabs python Text Iterator Pattern

From Leeroopedia
Knowledge Sources
Domains Streaming, Data_Preparation
Last Updated 2026-02-15 00:00 GMT

Overview

User-defined pattern for creating text iterators as input to the realtime TTS streaming API.

Description

This is a Pattern Doc. Users must implement a Python generator or iterator that yields str values to pass as the text parameter to convert_realtime(). The SDK does not provide a specific class for this — it accepts any Iterator[str].

Usage

Create a generator function that yields text strings and pass it as the text parameter to convert_realtime().

Interface Specification

import typing

def text_source() -> typing.Iterator[str]:
    """
    User-defined text source for realtime TTS.

    Yields:
        str: Text fragments. Can be words, sentences, or any granularity.
             The text_chunker will buffer at sentence boundaries.
    """
    ...

I/O Contract

Inputs

Name Type Required Description
(user-defined) any varies Whatever data source drives the text generation

Outputs

Name Type Description
(yields) Iterator[str] Text fragments to pass to convert_realtime(text=...)

Example Implementations

Simple Generator

def simple_text():
    yield "Hello, "
    yield "this is a test. "
    yield "How are you doing today?"

LLM Stream Adapter

def openai_stream_adapter(messages):
    """Adapt OpenAI streaming response to text iterator."""
    import openai
    client = openai.OpenAI()
    response = client.chat.completions.create(
        model="gpt-4",
        messages=messages,
        stream=True,
    )
    for chunk in response:
        if chunk.choices[0].delta.content:
            yield chunk.choices[0].delta.content

File Reader

def file_text_source(filepath: str, chunk_size: int = 100):
    """Read text from file in chunks."""
    with open(filepath, "r") as f:
        while chunk := f.read(chunk_size):
            yield chunk

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment