TimeoutError
asyncio.exceptions.TimeoutError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in upload_run
await client.runs.upload(run)
File "langsmith/client.py", line 210, in upload
await asyncio.wait_for(self._upload_run_data(run), timeout=30)
File "/usr/lib/python3.9/asyncio/tasks.py", line 481, in wait_for
raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError Why it happens
The LangSmith client uses asyncio.wait_for to limit the time allowed for uploading run data. If the upload process takes longer than the specified timeout (default 30 seconds), asyncio raises a TimeoutError. This can happen due to network latency, large payloads, or server-side delays.
Detection
Monitor your async upload calls for TimeoutError exceptions and log the duration of each upload to detect slow or stalled uploads before they fail.
Causes & fixes
Large run data payload causing slow upload exceeding the timeout limit
Reduce the size of the run data payload or increase the timeout parameter in the upload call to allow more time.
Network latency or connectivity issues delaying the upload process
Ensure stable network connectivity and consider retrying uploads with exponential backoff on timeout errors.
LangSmith server-side processing delays causing slow response
Contact LangSmith support to check server status or increase client-side timeout to accommodate slower responses.
Code: broken vs fixed
import asyncio
from langsmith import LangSmithClient
client = LangSmithClient(api_key=os.environ["LANGSMITH_API_KEY"])
async def upload_run(run):
# This line triggers TimeoutError if upload takes longer than 30 seconds
await client.runs.upload(run)
# asyncio.run(upload_run(my_run)) import os
import asyncio
from langsmith import LangSmithClient
client = LangSmithClient(api_key=os.environ["LANGSMITH_API_KEY"])
async def upload_run(run):
# Increased timeout to 60 seconds to avoid TimeoutError
await asyncio.wait_for(client.runs.upload(run), timeout=60)
# asyncio.run(upload_run(my_run))
print("Run upload completed successfully.") Workaround
Catch asyncio.exceptions.TimeoutError around the upload call, then retry the upload after a short delay or fallback to uploading smaller chunks of run data.
Prevention
Implement upload retries with exponential backoff and monitor upload durations; also optimize run data size and use stable network connections to avoid hitting timeouts.