Skip to content

PollyClient.synthesize_speech

rboto-polly==0.4.0 · Service index · Types

Model summary (excerpt): Synthesizes UTF-8 input, plain text or SSML, to a stream of bytes. SSML input must be valid, well-formed SSML. Some alphabets might not be available with all the voices (for example, Cyrillic might not be read at all by English voices) unless phoneme mapping is used. For more information, see How it Works . To use a custom voice, specify its ID in VoiceId . The voice must be in active status....

Install and imports

python -m pip install "rboto[polly]==0.4.0"
from rboto import PollyClient
from collections.abc import Sequence
from rboto_polly.types import (
    Engine,
    LanguageCode,
    OutputFormat,
    SpeechMarkType,
    SynthesizeSpeechOutput,
    TextType,
    VoiceId,
)

Signature

async def synthesize_speech(
    *,
    output_format: OutputFormat,
    text: str,
    voice_id: VoiceId,
    engine: Engine | None = None,
    language_code: LanguageCode | None = None,
    lexicon_names: Sequence[str] | None = None,
    sample_rate: str | None = None,
    speech_mark_types: Sequence[SpeechMarkType] | None = None,
    text_type: TextType | None = None,
) -> SynthesizeSpeechOutput: ...

Arguments without defaults are required. None omits an optional request argument. Static types do not validate resource existence, permissions, lengths, ranges, or conditional service requirements.

Arguments

Argument Required Model notes (excerpt)
output_format Yes The format in which the returned output will be encoded. For audio stream, this will be mp3, ogg_vorbis, ogg_opus, mu-law, a-law or pcm. For speech marks, this will be json. When pcm is used, the content returned is audio/pcm in a signed 16-bit, 1 channel (mono), little-endian format.
text Yes Input text to synthesize. If you specify ssml as the TextType , follow the SSML format for the input text.
voice_id Yes The voice ID to use for the synthesis. You can get a list of available voice IDs by calling the DescribeVoices operation. To use a custom voice, specify the voice ID that CreateVoice returned, for example, vc-9a8fa85dd3 . You can't specify the ARN of a voice.
engine No Specifies the engine ( standard , neural , long-form , or generative ) for Amazon Polly to use when processing input text for speech synthesis. Provide an engine that is supported by the voice you select. If you don't provide an engine, the standard engine is selected by default. If a chosen voice isn't supported by the standard engine, this will result in an error. For information on Amazon...
language_code No Optional language code for the Synthesize Speech request. This is only necessary if using a bilingual voice, such as Aditi, which can be used for either Indian English (en-IN) or Hindi (hi-IN). If a bilingual voice is used and no language code is specified, Amazon Polly uses the default language of the bilingual voice. The default language for any voice is the one returned by the...
lexicon_names No List of one or more pronunciation lexicon names you want the service to apply during synthesis. Lexicons are applied only if the language of the lexicon is the same as the language of the voice. For information about storing lexicons, see PutLexicon .
sample_rate No The audio frequency specified in Hz. The valid values for mp3 and ogg_vorbis are "8000", "16000", "22050", "24000", "44100" and "48000". The default value for standard voices is "22050". The default value for neural voices is "24000". The default value for long-form voices is "24000". The default value for generative voices is "24000". Valid values for pcm are "8000" and "16000" The default...
speech_mark_types No The type of speech marks returned for the input text.
text_type No Specifies whether the input text is plain text or SSML. The default value is plain text. For more information, see Using SSML .

Output

SynthesizeSpeechOutput.

Read-only native attributes, accessed as response.field. Collection getters materialize Python values; to_dict() recursively materializes a mapping. Streams are consumed once.

Field Type Presence References
audio_stream ByteStream Always present ByteStream
content_type str | None Always present —
request_characters int Always present —

| None means a value can be absent. Output mappings include every declared key; optional input keys may be omitted. Unknown output enum/union variants are not automatically valid inputs.

Engine, LanguageCode, OutputFormat, SpeechMarkType, SynthesizeSpeechOutput, TextType, VoiceId

Follow these links for nested inputs and outputs; recursive types link back to the same type page.

Pagination and streaming

No generated paginator for this operation.

Output streams: ByteStream.

Consume with async for. Do not reuse a consumed stream.

ByteStream.read() buffers the complete body.

Errors

Import from rboto_polly.exceptions:

ConflictError, EngineNotSupportedError, InvalidSampleRateError, InvalidSsmlError, LanguageNotSupportedError, LexiconNotFoundError, MarksNotSupportedForFormatError, ResourceNotFoundError, ServiceFailureError, SsmlMarksNotSupportedForTextTypeError, TextLengthExceededError.

Other service failures use the service base error; credential, transport and timeout failures use shared errors.

Example

Complete, type-checked service example. Replace its placeholders and configure credentials before running it.

import asyncio
from pathlib import Path

from rboto import PollyClient


async def main() -> None:
    client = PollyClient(region="us-east-1")
    response = await client.synthesize_speech(
        text="Hello from rboto", output_format="mp3", voice_id="Joanna",
    )
    audio = await response.audio_stream.read()
    await asyncio.to_thread(Path("speech.mp3").write_bytes, audio)


if __name__ == "__main__":
    asyncio.run(main())

Service guide · Pinned model