Skip to content

fix(sdk-py): wrap httpx exceptions with typed API errors in HTTP client#6964

Open
Balaji Seshadri (gitbalaji) wants to merge 2 commits intolangchain-ai:mainfrom
gitbalaji:fix/sdk-http-error-wrapping
Open

fix(sdk-py): wrap httpx exceptions with typed API errors in HTTP client#6964
Balaji Seshadri (gitbalaji) wants to merge 2 commits intolangchain-ai:mainfrom
gitbalaji:fix/sdk-http-error-wrapping

Conversation

@gitbalaji

Description

The SDK HTTP clients (HttpClient and SyncHttpClient) currently allow raw httpx exceptions (e.g., httpx.TimeoutException, httpx.ConnectError) to propagate directly to users. This is inconsistent with the SDK's error model, which defines typed error classes like APITimeoutError and APIConnectionError in langgraph_sdk.errors.

This PR wraps all httpx transport-level exceptions with the appropriate SDK error types:

  • httpx.TimeoutExceptionAPITimeoutError
  • httpx.HTTPError (other transport errors) → APIConnectionError

This ensures users can catch SDK-specific error types instead of needing to depend on httpx internals.

Changes

  • libs/sdk-py/langgraph_sdk/_async/http.py: Wrap all HTTP method calls (get, post, put, patch, delete) with try/except blocks that convert httpx exceptions to typed SDK errors.
  • libs/sdk-py/langgraph_sdk/_sync/http.py: Same changes for the synchronous HTTP client.

Why

  • Users should not need to import or know about httpx to handle common network errors like timeouts and connection failures.
  • The SDK already defines APITimeoutError and APIConnectionError but was not using them at the transport layer.
  • This matches the pattern used by other Python SDK clients (e.g., OpenAI, Anthropic).
All direct HTTP methods (get, post, put, patch, delete) in both
HttpClient (async) and SyncHttpClient now catch httpx.TimeoutException
and httpx.HTTPError and re-raise them as APITimeoutError and
APIConnectionError respectively.

Previously, raw httpx exceptions could leak to SDK consumers. The
stream and request_reconnect methods already have their own error
handling and are not affected.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Extend transport error wrapping to stream() and request_reconnect()
methods in both async and sync HTTP clients. Connection-setup errors
(ConnectError, ReadError, etc.) now surface as APIConnectionError and
timeouts as APITimeoutError instead of leaking raw httpx exceptions.

Add parametrized tests covering ReadError, ConnectError, ConnectTimeout,
and ReadTimeout for GET, POST, and stream() in both HttpClient and
SyncHttpClient.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants