Skip to content

fix: map async HTTP client transport errors to PaddleOCRAPIError - #18267

Open
bujna94 wants to merge 2 commits into
PaddlePaddle:mainfrom
bujna94:fix/async-http-transport-errors
Open

bujna94 wants to merge 2 commits into
PaddlePaddle:mainfrom
bujna94:fix/async-http-transport-errors

Conversation

@bujna94

@bujna94 bujna94 commented Jul 20, 2026

Copy link
Copy Markdown

Problem

The synchronous HTTPClient (_http.py) maps transport failures to the SDK's
error types — requests.Timeout → RequestTimeoutError, requests.ConnectionError → NetworkError — so callers can rely on the documented contract that every
failure is a PaddleOCRAPIError subclass.

The asynchronous AsyncHTTPClient (_async_http.py) did not. It imports
NetworkError and RequestTimeoutError (lines 28–29) but never used them — a
clear sign the wrapping was intended but missing. All five request sites
(submit_url, submit_file, get_job_status, get_batch_status,
fetch_jsonl) issued aiohttp requests with no transport-error handling.

As a result, a timeout or connection failure on an async call escapes as a raw
asyncio.TimeoutError / aiohttp.ClientConnectionError instead of a
PaddleOCRAPIError. User code doing except PaddleOCRAPIError (or
except RequestTimeoutError) around async calls silently fails to catch it,
diverging from the sync client.

Demonstrated on the current code (server unreachable / slow):

scenario sync client async client (before)
connection refused NetworkError aiohttp.ClientConnectorError (leaks)
timeout RequestTimeoutError builtins.TimeoutError (leaks)

Fix

Add a small _request() async context manager that enters the aiohttp request
and maps asyncio.TimeoutError → RequestTimeoutError and
aiohttp.ClientConnectionError → NetworkError (mirroring the sync client), then
route all five request sites through it. After the fix both scenarios above
raise the SDK error types, matching the sync contract.

Tests

Adds tests/api_client/test_async_http.py covering the connection-refused and
timeout paths for both get_job_status and fetch_jsonl. The tests drive the
async client with asyncio.run(...), so they add no new test dependency.
Verified the new tests fail on the pre-fix code and pass with the fix.

The sync HTTPClient wraps requests.Timeout / requests.ConnectionError as
RequestTimeoutError / NetworkError, but AsyncHTTPClient did not wrap anything --
it imported NetworkError and RequestTimeoutError yet never used them, so a
timeout or connection failure escaped as a raw asyncio.TimeoutError /
aiohttp.ClientConnectionError. Callers catching PaddleOCRAPIError around async
calls silently missed these, diverging from the sync contract.

Add a _request() async context manager that maps asyncio.TimeoutError ->
RequestTimeoutError and aiohttp.ClientConnectionError -> NetworkError, and route
all five request sites (submit_url, submit_file, get_job_status,
get_batch_status, fetch_jsonl) through it. Add tests/api_client/test_async_http.py
covering the connection-refused and timeout paths (uses asyncio.run, no new
test dependency).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@paddle-bot

paddle-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

Thanks for your contribution!

@CLAassistant

CLAassistant commented Jul 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Bobholamovic
Bobholamovic previously approved these changes Jul 22, 2026

@Bobholamovic Bobholamovic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Bobholamovic

Copy link
Copy Markdown
Collaborator

Please sign the CLA

The pre-commit black hook collapses the submit_file request call onto a
single line (it fits in 88 chars). Formatting only -- the AST is
unchanged from the reviewed commit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@bujna94

bujna94 commented Jul 22, 2026

Copy link
Copy Markdown
Author

Sorry for the churn — I pushed c31156e to fix the check-code-style failure. It's black formatting only (collapsing one call onto a single line); the AST is byte-identical to the approved 398eb95, so no behavior changed. Re-requesting review when you have a moment. Thanks!

@bujna94
bujna94 requested a review from Bobholamovic July 22, 2026 07:48
@bujna94

bujna94 commented Jul 24, 2026

Copy link
Copy Markdown
Author

Gentle ping — approved, CLA signed, style + Python checks green. The test-pr-gpu failures were ModuleNotFoundError: No module named 'paddlex' at collection time (runner env, not this diff); that looks resolved now, as recent GPU runs on other commits pass. There's a pending GPU run awaiting maintainer approval — a re-run should go green. Anything else needed from me?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants