High severity intermediate · Fix: 5-10 min

ConnectionRefusedError

builtins.ConnectionRefusedError

What this error means
LangSmith client fails to connect because the server refused the network connection, blocking API calls.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    client = LangSmithClient(api_key=os.environ["LANGSMITH_API_KEY"])
  File "/usr/local/lib/python3.9/site-packages/langsmith/client.py", line 88, in __init__
    self._connect()
  File "/usr/local/lib/python3.9/site-packages/langsmith/client.py", line 102, in _connect
    raise ConnectionRefusedError("Connection refused by LangSmith server")
ConnectionRefusedError: Connection refused by LangSmith server
QUICK FIX
Verify your LANGSMITH_API_KEY and endpoint URL environment variables, then retry the connection.

Why it happens

This error occurs when the LangSmith client attempts to establish a network connection to the LangSmith server but the server actively refuses the connection. Common causes include incorrect endpoint URLs, network firewall restrictions, or the LangSmith service being down or unreachable.

Detection

Monitor connection attempts and catch ConnectionRefusedError exceptions when initializing the LangSmith client; log the endpoint and network status to detect connectivity issues early.

Causes & fixes

1

Incorrect or missing LangSmith API endpoint URL in client configuration

✓ Fix

Verify and set the correct LangSmith API endpoint URL in your client initialization or environment variables.

2

Network firewall or proxy blocking outbound connections to LangSmith servers

✓ Fix

Ensure your network firewall or proxy allows outbound traffic to the LangSmith server IPs and ports.

3

LangSmith service is temporarily down or unreachable due to maintenance or outage

✓ Fix

Check LangSmith service status page and retry connection after confirming service availability.

4

Invalid or missing LANGSMITH_API_KEY environment variable causing client to fail early

✓ Fix

Set a valid LANGSMITH_API_KEY in your environment before initializing the client.

Code: broken vs fixed

Broken - triggers the error
python
import os
from langsmith import LangSmithClient

client = LangSmithClient(api_key=os.environ["LANGSMITH_API_KEY"])
# This line raises ConnectionRefusedError if server refuses connection
client.connect()
Fixed - works correctly
python
import os
from langsmith import LangSmithClient

# Ensure environment variables are set correctly
os.environ["LANGSMITH_API_KEY"] = os.getenv("LANGSMITH_API_KEY", "")
os.environ["LANGSMITH_API_ENDPOINT"] = os.getenv("LANGSMITH_API_ENDPOINT", "https://api.langsmith.com")

client = LangSmithClient(api_key=os.environ["LANGSMITH_API_KEY"], endpoint=os.environ["LANGSMITH_API_ENDPOINT"])
client.connect()  # Fixed: specify correct endpoint and ensure API key is set
print("LangSmith client connected successfully")
Added explicit endpoint configuration and ensured API key is set from environment variables to prevent connection refusal.

Workaround

Wrap the client connection call in try/except ConnectionRefusedError, log the error, and implement exponential backoff retries to handle transient network issues.

Prevention

Use environment validation on startup to confirm API keys and endpoints are set, and implement network connectivity checks before client initialization to avoid runtime connection refusals.

Python 3.9+ · langsmith >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

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