fix(anthropic): honor messages request timeout#33418
Conversation
Greptile SummaryThis PR fixes the Anthropic
Confidence Score: 5/5Safe to merge; the change only affects requests that already had a configured timeout — those that had none continue to use the same 600s/5s-connect client default as before. The timeout resolution reuses No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/custom_httpx/llm_http_handler.py | Adds _resolve_anthropic_messages_timeout static method and threads the resolved timeout through _async_post_anthropic_messages_with_http_error_retry on first attempt and all retries; None is correctly returned when no timeout is configured anywhere, so the client keeps its httpx.Timeout(600, connect=5) default. |
| tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py | Adds three new tests covering timeout precedence (test_resolve_anthropic_messages_timeout), request_timeout forwarding, and stream_timeout forwarding; updates FakeAsyncClient.post signature to accept the new timeout kwarg without breaking the existing Bedrock signing retry test. |
Reviews (4): Last reviewed commit: "Merge remote-tracking branch 'origin/lit..." | Re-trigger Greptile
Greptile SummaryThis PR fixes the Anthropic
Confidence Score: 5/5Safe to merge — the change is narrowly scoped to the Anthropic messages pass-through path, reuses the shared timeout resolution infrastructure, and does not alter behavior when no timeout is configured. The resolver correctly short-circuits to None when no timeout is set anywhere, so the cached client httpx.Timeout(600, connect=5) default is preserved. Timeout precedence mirrors the existing completion() path. The FakeAsyncClient.post update adds timeout=None only to satisfy the new keyword argument — no assertion is removed or weakened. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/custom_httpx/llm_http_handler.py | Adds timeout parameter to _async_post_anthropic_messages_with_http_error_retry and a new _resolve_anthropic_messages_timeout static method that correctly resolves per-request/model/global timeouts, then wires the resolved value into the Anthropic messages HTTP call path. |
| tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py | Adds parametrized unit tests for _resolve_anthropic_messages_timeout covering timeout precedence and the None fallback, two async integration tests confirming request_timeout and stream_timeout reach the HTTP POST call, and updates the existing FakeAsyncClient.post stub to accept the new timeout keyword argument. |
Reviews (2): Last reviewed commit: "fix(anthropic): keep client default conn..." | Re-trigger Greptile
…dler timeout tests
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
bugbot run |
|
The failing documentation check is pre-existing on the base branch: tests/documentation_tests/test_api_docs.py requires every UpdateTeamRequest field to be documented in the update_team docstring, and mcp_rpm_limit is missing from that docstring on litellm_internal_staging itself (litellm/proxy/management_endpoints/team_endpoints.py:1582; the field is documented for new_team at line 928 but was never added to update_team). This PR does not touch that file. A separate one-line docstring fix is being opened against litellm_internal_staging; once it merges this branch will be updated and the check re-run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit d8e31ce. Configure here.
…itellm_anthropic_messages_timeout
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit c3eb9dc. Configure here.
Relevant issues
Fixes #26752
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Proof below was captured by the original author on the original PR's commits. The upstream is a local Anthropic-compatible server that sleeps for 1.25s while the model config sets
request_timeout: 0.3, which keeps the proof deterministicConfig used for both runs:
Before fix, at base commit
bf02a4a47f, the request waited for the slow upstream and returned200, showing that the model-level timeout was not applied to the Anthropic/v1/messagesHTTP request:After fix, at original commit
63019add86, the same request returns a LiteLLM timeout. The response body shows the configured0.3stimeout reached the HTTP request path:Type
Bug Fix
Changes
Anthropic
/v1/messagesnow resolves per-request and configured timeouts through the shared completion timeout resolver, then forwards the resolved value into the async HTTP POST helper, on the first attempt and on every retryOn top of the original change, the follow-up commit makes the resolver return
Nonewhen no timeout was configured anywhere (no model-leveltimeoutorstream_timeout, no deploymentrequest_timeout, no explicitly setlitellm.request_timeout). The POST then falls back to the cached client's defaulthttpx.Timeout(600, connect=5)instead of an explicit bare600.0, which would have set the connect timeout to 600s as wellRegression coverage checks timeout precedence (model
timeoutbeats deploymentrequest_timeout,stream_timeoutonly applies to streaming requests), the unconfigured fallback returningNone, and the full async/v1/messageshandler wiring into the HTTP client for both streaming and non-streaming requests. Each new assertion was mutation-checked: always readingstream_timeout, hard-codingstream=Falseat the call site, and dropping theNonefallback each fail exactly one of the new testsBehavior changes
Requests to
/v1/messagesthat set a timeout (request body, deployment config, or an explicitlitellm.request_timeout) now honor it; previously it was ignored and the client default of 600s applied. When the resolved timeout is a bare number, httpx applies it to connect/read/write/pool alike, matching the existing chat completions behavior. When no timeout is configured anywhere, behavior is unchanged: the client default of 600s read with a 5s connect timeout still appliesCredit
Adopted from #32827 by @MelvinOrichiSocana-hs. Mirrored onto a
litellm_branch so CircleCI and the internal lint workflow runFinal Attestation
Note
Low Risk
Scoped to Anthropic messages HTTP wiring; unconfigured requests keep existing client defaults, while configured timeouts may surface 408s where slow calls previously succeeded.
Overview
Anthropic
/v1/messagesasync HTTP calls now apply the same timeout resolution as chat completions instead of always using the client’s long default.A new
_resolve_anthropic_messages_timeouthelper picksstream_timeouton streaming requests, otherwisetimeout, then deploymentrequest_timeout, then an explicit globallitellm.request_timeout, viaCompletionTimeout.resolve. That value is passed into_async_post_anthropic_messages_with_http_error_retryon the initial POST and retries. If nothing is configured at any layer, the resolver returnsNoneso httpx keeps the cached client default (600s read / 5s connect) rather than forcing a bare numeric timeout that would widen connect.Tests cover precedence, the unconfigured
Nonepath, and end-to-end forwarding for non-streaming and streaming handlers.Reviewed by Cursor Bugbot for commit c3eb9dc. Bugbot is set up for automated code reviews on this repo. Configure here.