Skip to content

fix(custom_httpx): preserve and rewind files on AsyncHTTPHandler ConnectError retry (#54177) - #54186

Open
MilindChatrabhuji wants to merge 1 commit into
BerriAI:mainfrom
MilindChatrabhuji:fix/async-http-handler-files-connect-error-54177
Open

MilindChatrabhuji wants to merge 1 commit into
BerriAI:mainfrom
MilindChatrabhuji:fix/async-http-handler-files-connect-error-54177

Conversation

@MilindChatrabhuji

Copy link
Copy Markdown

Fixes #54177.

Problem

When the initial attempt of AsyncHTTPHandler.post() fails with httpx.ConnectError (for instance, on a dropped/refused TCP connection or reset pooled session), the handler catches the exception and retries the request using single_connection_request.

However:

  1. single_connection_request did not accept a iles parameter, nor did it pass iles to client.build_request().
  2. As a consequence, on any multipart request (e.g. /v1/images/edits, audio transcriptions, video generation/edits), the retry request dropped the uploaded files and was built with data= alone.
  3. httpx automatically encodes data-only requests as �pplication/x-www-form-urlencoded. Providers like OpenAI reject this with:
    ext 400 Unsupported content type: application/x-www-form-urlencoded. This API method only accepts multipart/form-data or application/json requests.
  4. Additionally, if the request file content was wrapped in a seekable stream/buffer (io.BytesIO or file handle), an initial read attempt could leave the file position at EOF without being rewound before the second attempt.

Solution

  1. Added _rewind_files(files) helper: Safely seeks any seekable file objects or stream tuples back to offset 0 before retrying the connection.
  2. Updated single_connection_request(): Added optional iles: RequestFiles | None = None parameter and forwarded iles=files to client.build_request().
  3. Updated AsyncHTTPHandler.post(): Passes iles=files and calls _rewind_files(files) inside the except httpx.ConnectError: block.
  4. Added regression tests in ests/unit/llms/custom_httpx/test_http_handler.py:
    • est_connection_error_retry_forwards_files: Confirms that iles are preserved on retry, content-type remains multipart/form-data, and the multipart body contains both data fields and file bytes.
    • est_connection_error_retry_rewinds_files: Confirms that seekable file streams read prior to the error are rewound and their full content is re-transmitted.

@veria-ai

veria-ai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: AsyncHTTPHandler.post drops files on its ConnectError resend, multipart requests go out as x-www-form-urlencoded

1 participant