Azure CosmosDB - pymongo.errors.ServerSelectionTimeoutError: encoding with 'idna' codec failed

saurabh pophare 0 Reputation points
2025-03-07T13:41:07.34+00:00

User's image

I am following the official Azure documentation to build a Travel AI Agent using Python, FastAPI, and Azure CosmosDB (MongoDB API) as the database. The project involves storing and retrieving travel-related data using CosmosDB as the vector store. However, when executing the main.py script responsible for loading data into CosmosDB, the process fails with a Server Selection Timeout Error due to an IDNA (Internationalized Domain Name) encoding issue.

The error message suggests that the CosmosDB hostname is too long, leading to a Unicode encoding failure when pymongo attempts to process it. The pymongo.errors.ServerSelectionTimeoutError occurs when the MongoDB client is unable to establish a connection within the allocated timeout (30 seconds). The traceback indicates that the root cause is:

pymongo.errors.ServerSelectionTimeoutError: encoding with 'idna' codec failed (UnicodeError: label too long)

Python version: 3.11

  • pymongo version: (output of pip show pymongo)
  • Azure CosmosDB API: MongoDB API
  • Connection string: Using MongoClient(connection_string)

Steps Taken:

  1. I verified the CosmosDB connection string is correct.
  2. I tried replacing the MongoClient connection with connect=False.
  3. I ensured my database and collection exist in CosmosDB.
    • Python version: 3.11
      • pymongo version: (output of pip show pymongo)
        • Azure CosmosDB API: MongoDB API
          • Connection string: Using MongoClient(connection_string)
    Steps Taken:
    1. I verified the CosmosDB connection string is correct.
    2. I tried replacing the MongoClient connection with connect=False.
  4. I ensured my database and collection exist in CosmosDB.
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,786 questions
{count} votes

1 answer

Sort by: Most helpful
  1. PratikLad 240 Reputation points Microsoft External Staff
    2025-03-11T09:45:59.42+00:00

    Hi saurabh pophare,

    Glad to see you resolve the issue posting it as an answer to help other community members.

    pymongo.errors.ServerSelectionTimeoutError: encoding with 'idna' codec failed (UnicodeError: label too long)

    The error can cause because of many reasons but one of the reasons is if you have special characters like @,/ in your username and password these need to be escaped before it could be used normally.

    special characters such as those that may be used in the user and password need to be URL encoded to be parsed correctly. This includes the @ sign.

    Below is an example :

    Here password : "kx@jj5/g", where the "@" sign and "/"characters are Present

    Which can be escaped bu repenteng %40 as @ and %2F as / respectively

    Reference : https://blog.csdn.net/qq_41878442/article/details/141337484


    Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.