requests.exceptions.ConnectTimeout error in Azure Cognitive Services Text-to-speech REST API

eera5607 20 Reputation points

So, I have been trying process a folder with thousands of text files to convert each one to speech using Azure Cognitive Services Text-to-speech REST API. It works fine until it doesn't. I get errors after several successful conversions. I would like to have a stable connection so I can reliably leave the script running and not have to manually restart each time I get an error.

  TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
    urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='', port=443): Max retries exceeded with url: /sts/v1.0/issueToken (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F63AF32650>, 'Connection to timed out. (connect timeout=None)'))
    raise ConnectTimeout(e, request=request)
    requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='', port=443): Max retries exceeded with url: /sts/v1.0/issueToken (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001F63AF32650>, 'Connection to timed out. (connect timeout=None)'))

This is my current script:

 import os
    import requests
    import time
    import chardet
    subscription_key = 'here my subscription key'
    region = 'eastus'
    voice_name = 'es-MX-DaliaNeural'
    output_format = 'audio-24khz-96kbitrate-mono-mp3'
    tts_url = f'https://{region}'
    headers = {
        'Authorization': '',
        'Content-Type': 'application/ssml+xml',
        'X-Microsoft-OutputFormat': output_format,
        'User-Agent': 'YOUR_RESOURCE_NAME'
    # looping through all text files in the input folder
    input_folder = 'C:/path/to/text/files'
    output_folder = 'C:/path/to/folder'
    for filename in os.listdir(input_folder):
        # Check if the file is a text file
        if filename.endswith('.txt'):
            # Read the contents of the file and detect the encoding
            with open(os.path.join(input_folder, filename), 'rb') as f:
                rawdata =
                encoding = chardet.detect(rawdata)['encoding']
                text = rawdata.decode(encoding)
            # creating the SSML body for the TTS request
            ssml = f'<speak version="1.0" xmlns="" xmlns:mstts="" xml:lang="es-MX"><voice name="{voice_name}">{text}</voice></speak>'
            # getting the access token for the TTS service
            token_url = f'https://{region}'
            token_headers = {'Ocp-Apim-Subscription-Key': subscription_key}
            response =, headers=token_headers)
            access_token = response.text
            headers['Authorization'] = f'Bearer {access_token}'
            response = requests. Post(tts_url, headers=headers, data=ssml.encode('utf-8'))
            if response.status_code == 200:
                # save the audio content to a file
                audio_filename = os.path.splitext(filename)[0] + '.mp3'
                with open(os.path.join(output_folder, audio_filename), 'wb') as f:
                print(f'Successfully converted "{filename}" to speech')
                print(f'Error converting "{filename}" to speech: {response.content}')
            time. Sleep(30)

I leave 30 seconds between each conversion, but it isn't working. It converts 20-30 files and then the errors. Any help to get a more stable process? Thanks.

Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,451 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,458 questions
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 42,966 Reputation points Microsoft Employee

    @eera5607 I think you might be reaching your TTS limits of 200 TPS for a resource before the errors are seen. You can increase this upto 1000 by raising a quota increase request through Azure support but the limits are still cumulative of all the calls made to the service i.e REST API, SDK, CLI or speech studio.

    This increase should resolve any issues related to limits or quotas.

    Since you are using the REST API to generate a token and then call the TTS URL for short audio it might be easier to migrate to the batch synthesis API as it will soon replace the Long Audio API too for larger files. Other options available, is to use the SDK instead of REST API which can help you collect logs to trace your requests if the failures continue to occur.

    If you are still seeing issues with timeouts, then you might want to use the request ids or SDK logs to trace the reason for timeouts since as it could also be an issue with networks as seen in one of the threads I worked recently.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.
    0 comments No comments