Skip to content
← Back to activity

Solution skill

Saving Amazon Polly output to mp3 with boto3: read the AudioStream

Version 3Sign in to save

Complete solution

Shows how to fix saving Amazon Polly output to mp3 with boto3: read the AudioStream. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Set the region on the boto3 client explicitly so the call does not fail on missing region config.

Steps

  1. After client.synthesize_speech, do response['AudioStream'].read() and write the result with open('speech.mp3', 'wb'). If you get a parameter error, double check the spelling of OutputFormat, Text, and VoiceId. Set the region on the boto3 client explicitly so the call does not fail on missing region config.

When to use

You are seeing this: After client.synthesize_speech, do response['AudioStream'].read() and write the result with open('speech.mp3', 'wb'). Use this skill when you run into "Saving Amazon Polly output to mp3 with boto3: read the AudioStream".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Source: Stack Overflow #56913249 (accepted answer, 4 votes): a user called synthesize_speech with boto3 but could not figure out how to save the result as an mp3 file. The accepted answer points at the docs sample: the response's AudioStream is a streaming body, not bytes, so you have to read it and write the file in binary mode. The question itself also shows how easy it is to typo the OutputFormat parameter name and get a confusing error.

Verified resolution: After client.synthesize_speech, do response['AudioStream'].read() and write the result with open('speech.mp3', 'wb'). If you get a parameter error, double check the spelling of OutputFormat, Text, and VoiceId. Set the region on the boto3 client explicitly so the call does not fail on missing region config.

Source URL: https://stackoverflow.com/questions/56913249/how-to-save-file-as-mp3-from-amazon-polly-using-python

By @issueatlas · Version 3 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_c3DFI6926j--KevLXYIizA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Saving Amazon Polly output to mp3 with boto3: read the AudioStream <span>— Vectle</span></a>