Skip to content

fix(router): inspect image content without translating routed requests - #49371

Merged
joshua-berri merged 3 commits into
mainfrom
litellm_fix_router_image_detection
Oct 31, 2026
Merged

joshua-berri merged 3 commits into
mainfrom
litellm_fix_router_image_detection

Conversation

@joshua-berri

@joshua-berri joshua-berri commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Routed moderation and embedding requests can fail with 500 before reaching their provider because image detection translates unrelated request bodies. This fix inspects native image content directly, preserves valid image routing, and lets malformed Messages requests reach the provider's existing 400 response.

Details

TLDR

Problem this solves:

  • Image detection crashes on unrelated or malformed request bodies.
  • Five scheduled integration cases fail before provider handling.

How it solves it:

  • Inspect native content without invoking guardrail translators.
  • Skip non-message values; preserve nested image detection.
  • Extend existing router and shared-scanner regression tests.

User Flow

Before: ordinary requests fail with an internal server error.

  1. Send POST https://litellm-domain/v1/moderations with a list of strings, or embeddings with empty messages.
  2. Observe HTTP 500 (str.get or missing model).
  3. Send malformed POST https://litellm-domain/v1/messages; observe HTTP 500 for streaming and non-streaming requests.

After: requests receive their expected provider response.

  1. Send the same moderation or embedding request.
  2. Observe HTTP 200 with the request payload preserved.
  3. Send the same malformed Messages request; observe HTTP 400 for both stream modes.

Relevant context

File changes

  • litellm/router.py: inspect original messages/input instead of resolving through guardrail translators.
  • litellm/litellm_core_utils/prompt_templates/common_utils.py: tolerate non-message values and malformed nested parts; recognize chat, Responses, Anthropic, and tool-output images.
  • tests/unit/proxy/hooks/test_unit_test_max_model_budget_limiter.py: inject a fixed clock and explicit expired calendar windows, covering October 31 and leap/non-leap February. After main incorporated test(budgets): age the monthly window past any calendar month so the reset test passes on the 31st #49372, this retains its corrected helper and extends the test with deterministic calendar coverage; production budget code is unchanged.
  • Existing router/scanner unit tests: cover sync/async routing, metadata variants, request preservation, malformed values, and supported image formats.

Screenshots / Proof of Fix

Baseline: 1595adfb2f678c54c6161d870956c366fbd134b5.
Current tip: 49f479e31183bd7f6e77a296c79b917319a0b014.
Local candidate HTTP evidence below was refreshed at the current tip using a native extension built from matching Rust source. Both local baseline refresh and independent final-tip verification completed.

Executed the unchanged integration cases against real local proxy, PostgreSQL, Redis, and scripted HTTP upstream processes. Native extension built from pinned repository source. Baseline reused the candidate checkout with both changed production files restored byte-for-byte using git show BASE:path; all other production files and integration tests are identical. Restored the candidate before the final run; final checkout is clean.

Existing integration case Baseline Candidate
moderation wire inputs 500, str has no get Pass, 200 and exact upstream payload
malformed /v1/messages 500 / 500 Pass, 400 / 400
omni-moderations-next-list cost case 500, str has no get Pass, expected successful response and billing
Azure E1-moderation 500, missing model Pass, 200
Azure E1-shield 500, missing model Pass, 200

Baseline: 5 failed in 35.95s. Candidate: 5 passed in 31.89s.

Exact test selection (with the repository's owned_upstream, owned_proxy, and cost_map_environment harness):

python -m pytest \
 tests/integration/providers/test_moderations_wire.py::test_openai_sdk_moderation_inputs_reach_upstream_as_exact_model_and_input \
 tests/integration/messages_endpoint/providers/anthropic/test_passthrough_migration_wire.py::test_passthrough_bad_request_returns_400_v1_messages \
 'tests/integration/cost_calculation/test_cost_tracking.py::test_case_bills_expected_cost[omni-moderations-next-list]' \
 tests/integration/observability/test_azure_content_safety_dispatch.py::test_e1_empty_messages_matches_base_guardrail_response \
 -q --tb=short

Screenshot routing controls at 49f479e3118: message-content screenshots, function/custom tool-output screenshots, and computer-output url aliases each returned 200, selected the explicitly vision-capable deployment, and preserved the upstream input. The group had two openai/gpt-4o deployments: text-only (model_info.supports_vision=false, order=0) and vision (supports_vision=true, order=1). Synthetic provider keys identified the selected deployment in recorded HTTP headers.

Reproduction request, repeated for each native input shape:

curl -X POST "$PROXY_URL/v1/responses" \
  -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' \
  -d '{"model":"screenshot-verification","input":[{"role":"user","content":[{"type":"computer_screenshot","image_url":"https://example.invalid/screenshot.png"}]}]}'

The other inputs are function_call_output and custom_tool_call_output items with that screenshot in output: [...], and computer_call_output with output: {"type":"computer_screenshot","url":"https://example.invalid/screenshot.png"}. Each recorded upstream authorization was Bearer synthetic-vision; each recorded input exactly matched the request.

Local affected tests at current tip: 493 passed, including all 85 budget-limiter tests. The old budget reset test fails on October 31 UTC; the replacement passes all three fixed dates and asserts both spend reset and the exact next reset timestamp. Canonical make lint-checks BASE_REF=1595adfb2f678c54c6161d870956c366fbd134b5 passed, including production/test Ruff, formatting, strict-rule, type-discipline, test-quality, basedpyright budget, E2E typing, and import checks. Existing Pydantic/legacy-alias warnings are unrelated.

Local coverage: 17/17 changed executable lines (100%); 14/14 measured touched-function branch edges. These are focused change metrics, not repository-wide coverage. The completed Codecov report confirms 17/17 patch lines covered (100%) after both current-tip uploads merged. Repository-wide coverage is 89.57%; that is separate from the focused line/branch metrics.

Independent Devin final-tip report and artifacts completed for baseline 1595adfb2f678c54c6161d870956c366fbd134b5 and final 49f479e31183bd7f6e77a296c79b917319a0b014: five baseline failures became five passes. Independent real-HTTP controls preserved chat/Responses/Anthropic image routing, all four screenshot formats, unknown-capability and all-text-only fallback, cooldown behavior, and upstream request bodies. Sync/async SDK controls also passed. Separate source worktrees used a fresh native build from identical baseline/head Rust sources.

Devin additionally ran all 85 budget-file tests with both real time and frozen October 31, including all three fixed-date cases. The historical test failed at e2632a8e; the new main baseline already passes after #49372. No change-caused regressions or blockers were found; temporary services, builds and worktrees were cleaned up.

Pre-Submission checklist

  • Meaningful regression tests added.
  • Affected local tests and canonical lint/type gates pass.
  • Scope limited to image detection and the approved budget-test correction.
  • All five configured required CI checks pass at 49f479e3118.
  • No separate contributor-agreement check is reported by this repository; no attestation was submitted or bypassed.
  • Completed Codecov report reviewed at current tip, including upload processing state and changed lines.
  • Greptile 5/5 and Cursor Bugbot completed at 49f479e3118, with zero unresolved actionable findings and zero Bugbot annotations. Veria also passed.
  • Independent Devin source-build verification completed at 49f479e3118.

Main already fixes the old budget failure through #49372. This PR now adds deterministic coverage on top of that fix. Historical budget test reproduction: with TZ=UTC, the previous test computes a 30-day age on October 31, placing the start on October 1. That calendar window ends November 1 and correctly retains spend. The replacement uses a fixed clock and actually expired starts, preserving the original limit/reset assertions. This failure also appears in the baseline unit run.

CI results at final tip

All five configured required checks pass: unit passed, lint passed, smoke passed, cost-map-guard, and spend-logs-guard. The previously failing budgets shard passes. Existing human approval remains recorded.

All original five integration regressions pass in the current integration run: moderation and malformed Messages in providers, omni-moderations-next-list in cost, and both Azure E1 cases in extensions.

Unrelated integration failures remain outside this PR: Cohere token counts (cost), unpriced response-cost header (accounting), a PyPI network failure while building test infrastructure (management), and existing provider/extension assertions. Provider failures are a subset of the main run, with no PR-only failures; likewise extensions. These failed checks have not been represented as passed or bypassed.

Type

🐛 Bug Fix

@joshua-berri

Copy link
Copy Markdown
Contributor Author

@greptileai please review the image-routing fix at 2d7ac70, including malformed payload handling and preservation of valid image routing.

@joshua-berri

Copy link
Copy Markdown
Contributor Author

bugbot run — please review 2d7ac70 for image-routing regressions, malformed payload handling, and preservation of supported native image formats.

@greptile-apps

greptile-apps Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge, with no blocking issue found

Summary

The router now checks native request content for images instead of translating requests through guardrail handlers

  • The router checks native request content before choosing deployments.
  • The monthly budget reset test uses fixed calendar dates.

No actionable issues were found. The previous-review merge bases differed, so this review used the full PR diff. No previous Greptile threads were supplied

Reviews (4) · Last reviewed commit: "test: make monthly budget reset coverage..." · Reviewed by Greptile

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@codspeed

codspeed Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing litellm_fix_router_image_detection (49f479e) with main (bfef2e1)

Open in CodSpeed

@joshua-berri

Copy link
Copy Markdown
Contributor Author

@greptileai please review e2632a8, including the screenshot-content, tool-output, and URL-alias regression fix. Local tests and canonical gates pass.

@joshua-berri

Copy link
Copy Markdown
Contributor Author

bugbot run — please review e2632a8 and confirm the screenshot-routing finding is resolved. Four targeted HTTP probes now select vision.

@codecov

codecov Bot commented Oct 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@joshua-berri

Copy link
Copy Markdown
Contributor Author

@greptileai please review aac32c6f7b7, including the deterministic monthly budget regression test. All 492 affected local tests and canonical lint gates pass.

@joshua-berri

Copy link
Copy Markdown
Contributor Author

bugbot run on aac32c6f7b7 please. The followup only fixes calendar-dependent budget test setup; production budget behavior remains unchanged.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@joshua-berri
joshua-berri force-pushed the litellm_fix_router_image_detection branch from aac32c6 to 49f479e Compare October 31, 2026 16:53
@joshua-berri

Copy link
Copy Markdown
Contributor Author

@greptileai please review rebased tip 49f479e. Main budget helper changes are preserved; deterministic coverage, 493 affected tests, and canonical gates pass.

@joshua-berri

Copy link
Copy Markdown
Contributor Author

bugbot run on rebased tip 49f479e please. Image patches are unchanged; the monthly budget regression now uses explicit dates.

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ 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 49f479e. Configure here.

@joshua-berri
joshua-berri merged commit 6e27da8 into main Oct 31, 2026
105 of 113 checks passed
@joshua-berri
joshua-berri deleted the litellm_fix_router_image_detection branch October 31, 2026 17:44

This branch was previously deployed

1 inactive deployment
integration-license — 49f479e3 Deployed Oct 31, 2026 by joshua-berri via integration mcp #23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants