Skip to content

fix(fallback-generalizations): anchor bedrock-claude-ids and restrict routing rules to bare ids - #32990

Closed
yucheng-berri wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_anchor_bedrock_claude_ids
Closed

yucheng-berri wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_anchor_bedrock_claude_ids

Conversation

@yucheng-berri

Copy link
Copy Markdown
Contributor

Relevant issues

Every PR's "Unit Tests: Proxy Legacy Tests / auth-and-jwt" job started failing at 2026-07-12 00:43 UTC on two model-access deny params (bedrockz/anthropic.claude-3-5-sonnet-20240620 against key/team models ['bedrock/*']), for example https://github.com/BerriAI/litellm/actions/runs/29174424786/job/86600954535. The failures show up on branches whose diffs cannot affect auth, which is what pointed to shared state rather than any PR's changes

Linear ticket

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to 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

Live proxy on this branch, default cost-map behavior (no LITELLM_LOCAL_MODEL_COST_MAP, so the proxy fetched the current, still unanchored map from main at boot; the deny below is the new bare-id guard in get_llm_provider behaving exactly as an upgraded customer proxy would). Config is a bedrock/* wildcard deployment, the deployment shape this regression affects

model_list:
  - model_name: bedrock/*
    litellm_params:
      model: bedrock/*
      aws_region_name: us-east-1

general_settings:
  master_key: sk-1234
docker run -d --name litellm_db -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=litellm -p 5434:5432 postgres:16
DATABASE_URL="postgresql://postgres:postgres@localhost:5434/litellm" prisma db push --schema litellm/proxy/schema.prisma --skip-generate
DATABASE_URL="postgresql://postgres:postgres@localhost:5434/litellm" python litellm/proxy/proxy_cli.py --config bedrock_wildcard_config.yaml --port 4020 --use_v2_migration_resolver

curl -s http://localhost:4020/key/generate -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" \
  -d '{"models": ["bedrock/*"]}'

A key scoped to bedrock/* must not reach bedrockz/... (this returned a completion-path error before the fix because auth let it through; it now fails auth)

curl -s -w "\nHTTP %{http_code}\n" http://localhost:4020/v1/chat/completions -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"model": "bedrockz/anthropic.claude-3-5-sonnet-20240620", "messages": [{"role": "user", "content": "hi"}]}'
{"error":{"message":"key not allowed to access model. This key can only access models=['bedrock/*']. Tried to access bedrockz/anthropic.claude-3-5-sonnet-20240620","type":"key_model_access_denied","param":"model","code":"403"}}
HTTP 403

The same key still reaches real Bedrock through the wildcard (live AWS call, not mocked)

curl -s -w "\nHTTP %{http_code}\n" http://localhost:4020/v1/chat/completions -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"model": "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0", "messages": [{"role": "user", "content": "Reply with exactly: wildcard access works"}]}'
{"id":"chatcmpl-b18ba0b1-50c4-4326-9ff3-76ef2ede7c55","created":1783832717,"model":"bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0","object":"chat.completion","choices":[{"finish_reason":"stop","index":0,"message":{"content":"wildcard access works","role":"assistant"}}],"usage":{"completion_tokens":7,"prompt_tokens":15,"total_tokens":22}}
HTTP 200

Type

🐛 Bug Fix

Changes

The fallback_generalizations block introduced in #32873 ships a routing rule named bedrock-claude-ids with the unanchored pattern anthropic\.claude-. Rules match with re.search, so the rule also fires inside ids under a foreign provider namespace such as bedrockz/anthropic.claude-3-5-sonnet-20240620. get_llm_provider then resolves that id to provider bedrock, and _model_custom_llm_provider_matches_wildcard_pattern in auth_checks.py rewrites it as bedrock/anthropic.claude-3-5-sonnet-20240620, so keys and teams scoped to bedrock/* passed the model access check for ids they should never reach. The staging sync #32884 (merged 2026-07-12 00:42:56 UTC) published the rule to main's model_prices_and_context_window.json, which every proxy and CI job without LITELLM_LOCAL_MODEL_COST_MAP fetches at import; that is why auth-and-jwt went red on every PR at 00:43 with no related diff anywhere

The fix has two layers. The rule's pattern is now anchored as ^(?:[a-z-]+\.)?anthropic\.claude-, which still covers bare, region-prefixed (us./eu./au./jp./apac./us-gov.) and global.-prefixed Bedrock ids but no longer matches slash-namespaced ids; this is the layer that heals released proxies, which fetch main's copy remotely and have no code from this PR. Separately, get_llm_provider now consults routing generalizations only for bare ids: a slash means an explicit provider prefix that already failed every provider lookup, and that keeps raising BadRequestError exactly as it did before the rules feature landed. The guard is why this PR's own CI is green immediately, and why every other PR heals on the next rebase or merge-queue run once this lands, even while main's JSON copy stays unanchored until the next staging sync

Both layers are pinned by new tests in tests/test_litellm/litellm_core_utils/test_fallback_generalizations.py: one asserts the shipped rule no longer matches bedrockz/anthropic.claude-... and that get_llm_provider raises for it, the other asserts a slash-prefixed unknown provider raises even when a rule matches inside the id. Each test was verified to fail with its half of the fix reverted. The previously failing params in tests/proxy_unit_tests/test_auth_checks.py pass again both with the bundled map and against the current remote map

@greptile-apps

greptile-apps Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a routing regex in fallback_generalizations that caused re.search to substring-match bedrockz/anthropic.claude-... as a Bedrock ID, allowing keys scoped to bedrock/* to pass the model access check for foreign-namespace IDs they should never reach. The fix is layered: the bedrock-claude-ids pattern in both JSON cost maps is anchored to ^(?:[a-z-]+\.)?anthropic\.claude- (covering all current Bedrock region-prefix forms), and get_llm_provider now skips routing-generalization lookup entirely for any ID containing a slash.

  • JSON anchor (model_prices_and_context_window.json + backup): the pattern change takes effect in released proxies that fetch main's cost map remotely, independently of any code change reaching those deployments.
  • Python guard (get_llm_provider_logic.py): the "/" not in model check ensures that even an unanchored or future-incorrectly-authored rule can never cause a slash-prefixed unknown provider to be silently resolved.
  • Tests (test_fallback_generalizations.py): two new mocked-only regression tests pin both layers, with no real network calls.

Confidence Score: 5/5

Safe to merge — the change is tightly scoped to routing-generalization fallback and does not touch any other provider resolution path.

Both fix layers (JSON pattern anchoring and Python slash guard) are correct and complementary. The existing test_shipped_bedrock_syntax_claude_id_routes_to_bedrock test covers all current Bedrock region-prefix forms against the anchored pattern, so no regression in legitimate routing. The two new tests are mock-only and directly pin the fixed failure modes. No pre-existing tests are weakened.

No files require special attention.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/get_llm_provider_logic.py Adds a "/" not in model guard so routing-generalization fallback is only consulted for bare IDs, preventing slash-namespaced foreign providers from being incorrectly resolved via a matching rule.
model_prices_and_context_window.json Anchors the bedrock-claude-ids routing rule pattern from anthropic\.claude- to ^(?:[a-z-]+\.)?anthropic\.claude-, preventing it from matching slash-prefixed IDs under foreign namespaces via re.search.
litellm/model_prices_and_context_window_backup.json Mirror of the same bedrock-claude-ids pattern anchoring applied to the bundled/backup cost map.
tests/test_litellm/litellm_core_utils/test_fallback_generalizations.py Adds two regression tests: one verifying the slash guard in get_llm_provider raises for unknown slash-prefixed IDs even when a rule matches, and one verifying the anchored bedrock-claude-ids rule no longer matches foreign-namespace IDs like bedrockz/anthropic.claude-....

Reviews (1): Last reviewed commit: "fix(fallback-generalizations): anchor be..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@yucheng-berri
yucheng-berri deleted the litellm_anchor_bedrock_claude_ids branch July 13, 2026 17:36
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.

1 participant