High severity intermediate · Fix: 2-5 min

asyncio.TimeoutError

asyncio.exceptions.TimeoutError

What this error means
The Mistral async client failed to connect within the allotted timeout period, causing an asyncio TimeoutError.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    response = await client.chat.completions.create(model="mistral-7b", messages=messages)
  File "/usr/local/lib/python3.10/site-packages/mistral/client.py", line 128, in create
    await asyncio.wait_for(self._send_request(payload), timeout=10)
  File "/usr/lib/python3.10/asyncio/tasks.py", line 481, in wait_for
    raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError
QUICK FIX
Increase the async client's timeout setting to a higher value to allow more time for the connection to establish.

Why it happens

The Mistral async client uses asyncio to manage network requests. If the server does not respond within the configured timeout window, asyncio raises a TimeoutError. This can happen due to network latency, server overload, or incorrect timeout settings.

Detection

Monitor for asyncio.TimeoutError exceptions in your async calls to Mistral client and log the duration of requests to detect slow or stalled connections before failure.

Causes & fixes

1

The default timeout for the async client is too low for the current network conditions or server response time.

✓ Fix

Increase the timeout parameter in the client call, e.g., pass a higher timeout value to asyncio.wait_for or the client configuration.

2

Network connectivity issues or firewall blocking the connection to the Mistral API endpoint.

✓ Fix

Verify network connectivity, firewall rules, and proxy settings to ensure the client can reach the Mistral server.

3

The Mistral server is overloaded or temporarily down, causing delayed or no responses.

✓ Fix

Implement retry logic with exponential backoff and monitor Mistral service status to handle transient server issues gracefully.

Code: broken vs fixed

Broken - triggers the error
python
import asyncio
from mistral import Mistral

client = Mistral()

async def main():
    # This line triggers asyncio.TimeoutError if server is slow
    response = await client.chat.completions.create(model="mistral-7b", messages=[{"role": "user", "content": "Hello"}])
    print(response)

asyncio.run(main())
Fixed - works correctly
python
import os
import asyncio
from mistral import Mistral

client = Mistral()

async def main():
    # Increased timeout to 30 seconds to prevent connection timeout
    response = await asyncio.wait_for(
        client.chat.completions.create(model="mistral-7b", messages=[{"role": "user", "content": "Hello"}]),
        timeout=30
    )
    print(response)

if __name__ == "__main__":
    asyncio.run(main())
Wrapped the client call with asyncio.wait_for and increased the timeout to 30 seconds to allow more time for the connection and response.

Workaround

Catch asyncio.TimeoutError exceptions around the client call and retry the request after a short delay or fallback to a cached response if available.

Prevention

Configure appropriate timeout values based on network conditions and implement robust retry and circuit breaker patterns to handle transient network or server delays.

Python 3.9+ · mistral >=0.1.0 · tested on 0.2.5
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.