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
Open
fix(sdk-py): wrap httpx exceptions with typed API errors in HTTP client#6964Balaji Seshadri (gitbalaji) wants to merge 2 commits intolangchain-ai:mainfrom
Balaji Seshadri (gitbalaji) wants to merge 2 commits intolangchain-ai:mainfrom
Conversation
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>
c5fd2af to
1a42e0a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The SDK HTTP clients (
HttpClientandSyncHttpClient) currently allow rawhttpxexceptions (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 likeAPITimeoutErrorandAPIConnectionErrorinlanggraph_sdk.errors.This PR wraps all
httpxtransport-level exceptions with the appropriate SDK error types:httpx.TimeoutException→APITimeoutErrorhttpx.HTTPError(other transport errors) →APIConnectionErrorThis ensures users can catch SDK-specific error types instead of needing to depend on
httpxinternals.Changes
libs/sdk-py/langgraph_sdk/_async/http.py: Wrap all HTTP method calls (get,post,put,patch,delete) with try/except blocks that converthttpxexceptions to typed SDK errors.libs/sdk-py/langgraph_sdk/_sync/http.py: Same changes for the synchronous HTTP client.Why
httpxto handle common network errors like timeouts and connection failures.APITimeoutErrorandAPIConnectionErrorbut was not using them at the transport layer.