Repository navigation
perf: move Anthropic, Vertex Anthropic, Ollama and HF template fetches off the event loop - #40311
Conversation
…s off the event loop Direct Anthropic http image inlining, Vertex AI Anthropic forced base64 conversion, Ollama completion image download and the watsonx GPT-OSS Hugging Face chat template lookup all ran synchronous HTTP inside the async request path. Each provider config now transforms through async_inline_remote_media on the async path, the Anthropic handler awaits the config's async_transform_request before dispatch and in the Rust fallback, and watsonx text exposes async_transform_request and always awaits ahf_chat_template Resolves LIT-7028 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
|
Greptile SummaryThis PR moves remote-media and Hugging Face template retrieval off async request event-loop paths.
Confidence Score: 5/5The PR appears safe to merge, with no outstanding findings after the previous test-design concern was withdrawn. The asynchronous transformations preserve provider-specific media behavior while moving blocking fetches off the event loop, and the relevant dispatch paths are covered by mocked regression tests. The only previous thread was manually resolved after Greptile accepted the author’s rationale and withdrew the finding.
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/prompt_templates/image_handling.py | Adds media-part classification so asynchronous transforms can selectively inline image URLs. |
| litellm/llms/anthropic/chat/handler.py | Moves Anthropic async request transformation ahead of dispatch while preserving synchronous and Rust fallback behavior. |
| litellm/llms/anthropic/chat/transformation.py | Adds asynchronous inlining of HTTP image URLs for direct Anthropic requests. |
| litellm/llms/ollama/completion/transformation.py | Enables asynchronous transformation and remote-image inlining for Ollama completion requests. |
| litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py | Configures Vertex AI Anthropic to inline all remote image URLs asynchronously. |
| litellm/llms/watsonx/chat/transformation.py | Ensures GPT-OSS template application consistently uses the asynchronous Hugging Face path. |
| litellm/llms/watsonx/completion/transformation.py | Opts watsonx text completion into the standard asynchronous request-transform interface. |
Reviews (2): Last reviewed commit: "perf: move Anthropic, Vertex Anthropic, ..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@greptileai the only open finding was accepted in its thread above, please re-review de87469 with that resolved |
0f886d9
into
litellm_internal_staging
TLDR
Problem this solves:
How it solves it:
async_transform_requestbuilt onasync_inline_remote_mediauses_async_transform_request, GPT-OSS always awaitsahf_chat_templateUser Flow
Before: a team sends chat requests with image URLs through the proxy and every other request slows to a crawl
image_urlthat takes 8 seconds to downloadAfter: the same burst finishes in one wave and the text-only request stays fast
image_urlthat takes 8 seconds to downloadRelevant issues
Follow-up to #39839
Linear ticket
Resolves LIT-7028
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Shared setup. One proxy process (
--num_workers 1) on port 47028 withPYTHONPATHpointing at the checkout under test, the sameconfig.yamlfor both arms:anthropic-haikuisanthropic/claude-haiku-4-5with a real key,vertex-claudeisvertex_ai/claude-sonnet-4-6inus-east5with real credentials,ollama-llavaisollama/llavawithapi_basepointing at a local stand-in on 127.0.0.1:47029 (there is no Ollama server on this box, the stand-in logs theimagesarray it receives and answers a fixed completion). The same local server also servesGET /slow.png?delay=8, a one pixel PNG returned after 8 seconds, anduser_url_allowed_hostslists it. Each leg fires 30 concurrent requests, then 1.5 seconds later one text-onlyanthropic-haikurequest and oneGET /health/liveliness. The image body is byte-identical across armsThe same loop runs with
"model":"vertex-claude"and"model":"ollama-llava". Timings below are%{time_total}per request, grouped ascount status secondsBefore (35451ec)
anthropic-haiku, 30 concurrent http image requests
12 200 9s; 12 200 17s; 6 200 25s(three waves 8s apart, 12 downloads at a time, matching the image server log below)anthropic-haikuduring the fan-out:200 15.104831sGET /health/livelinessduring the fan-out:200 0.001985sWhite./White.(real Anthropic answer)vertex-claude, 30 concurrent http image requests
12 200 10s; 2 200 17s; 9 200 18s; 1 200 20s; 2 200 25s; 2 200 26s; 1 200 27s; 1 200 28santhropic-haikuduring the fan-out:200 15.110801sGET /health/livelinessduring the fan-out:200 0.001804s**Green**/**Green**(real Vertex AI answer)ollama-llava, 30 concurrent http image requests
12 200 8s; 12 200 16s; 6 200 24santhropic-haikuduring the fan-out:200 15.368942sGET /health/livelinessduring the fan-out:200 0.001764s12 21:59:20; 12 21:59:28; 6 21:59:36(downloads ran 12 at a time)POST /ollama/api/generate model=llava ... images=['iVBORw0KGgoAAAANSUhEUgAA...'], responseGreen (sink saw 1 base64 image(s))After (de87469)
anthropic-haiku, 30 concurrent http image requests
30 200 9s(one wave)anthropic-haikuduring the fan-out:200 0.571313sGET /health/livelinessduring the fan-out:200 0.002038sWhite./White(real Anthropic answer, image still delivered as base64 since Anthropic cannot fetch 127.0.0.1)vertex-claude, 30 concurrent http image requests
9 200 12s; 16 200 13s; 2 200 14s; 3 200 15s(8s download plus Vertex latency, no 17s to 28s tail)anthropic-haikuduring the fan-out:200 1.403497sGET /health/livelinessduring the fan-out:200 0.002189s**Green**/**Green**(real Vertex AI answer)ollama-llava, 30 concurrent http image requests
30 200 8s(one wave)anthropic-haikuduring the fan-out:200 0.547705sGET /health/livelinessduring the fan-out:200 0.001998s30 22:25:49(all 30 downloads began together)POST /ollama/api/generate model=llava ... images=['iVBORw0KGgoAAAANSUhEUgAA...'], responseGreen (sink saw 1 base64 image(s))Which branch each leg exercised: all three legs are
acompletion=True, non-streaming. Anthropic and Vertex go throughAnthropicChatCompletion.completionwith the Rust core disabled by default, so they took the newacompletion_dispatchclosure (Vertex withis_vertex_request=True, which is what flips the predicate to inline https as well). Ollama went throughBaseLLMHTTPHandler, which only callsasync_transform_requestbecauseOllamaConfig.uses_async_transform_requestis now true. The synclitellm.completionpath, streaming, and the Rust-enabled path are covered by the existing Anthropic handler and rust_bridge suites (570 tests intests/test_litellm/llms/anthropic/chat,tests/test_litellm/rust_bridgeplus the new ones pass locally) rather than by this live run. The watsonx GPT-OSS template path is covered by the new parameterized test only, since there are no watsonx credentials on this boxType
🐛 Bug Fix
Caveats (if any)
Low
AnthropicConfigitself does not setuses_async_transform_request, on purpose: its subclasses routed throughBaseLLMHTTPHandler(Azure AI Anthropic, Databricks, Bedrock Claude platform) keep their current sync transform; the Anthropic handler awaitsasync_transform_requestunconditionally insteadcompletionthat still transforms synchronously; out of scope hereollama_chat/) never fetched remote images, only the Ollama completion path did, so only that one changedFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/54d438b6c4af4cd7a4bd26c1f3fbcb30
Open in Devin Desktop: https://app.devin.ai/desktop/session/54d438b6c4af4cd7a4bd26c1f3fbcb30?variant=devin
Requested by: @yassin-berriai