feat(model_info): opt-in field-level backfill from fallback generalization rules

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Devin AI 2026-09-13 04:04:33 +00:00 committed by kerry
parent cae4a65545
commit cee7215b24
5 changed files with 166 additions and 3 deletions

View file

@ -34,6 +34,11 @@ rules never mix the two and never use ``extends``. A rule whose
Rules are only consulted after exact and case-insensitive lookups miss, so an
exact cost-map entry always takes precedence over any rule.
Rules flagged with ``backfill_exact_entries: true`` also fill only keys missing
from an exact cost-map entry, while values already present on the entry win on
conflict. Only flagged capability rules participate in this backfill; routing
rules never do.
Patterns are matched case-insensitively with ``re.search`` and are not implicitly
anchored: a rule must include ``^`` and ``$`` to bind to the whole model name,
otherwise it matches as a substring. Keeping anchoring in the regex makes the rule
@ -57,6 +62,7 @@ PATTERN_FIELD: Final = "pattern"
MODEL_INFO_FIELD: Final = "model_info"
PROVIDER_KEY: Final = "litellm_provider"
LEGACY_EXTENDS_FIELD: Final = "extends"
BACKFILL_FIELD: Final = "backfill_exact_entries"
def _resolve_legacy_extends(rules: list) -> list:
@ -98,6 +104,7 @@ class _RoutingRule:
class _CapabilityRule:
pattern: re.Pattern
model_info: dict
backfill_exact_entries: bool
_CompiledRule = _RoutingRule | _CapabilityRule
@ -125,8 +132,9 @@ def _compile_rule(rule: object) -> tuple[_CompiledRule, ...]:
e,
)
return ()
backfill: Final = rule.get(BACKFILL_FIELD) is True
if PROVIDER_KEY not in model_info:
return (_CapabilityRule(pattern=compiled, model_info=model_info),)
return (_CapabilityRule(pattern=compiled, model_info=model_info, backfill_exact_entries=backfill),)
provider: Final = model_info[PROVIDER_KEY]
if not isinstance(provider, str):
verbose_logger.warning(
@ -140,7 +148,7 @@ def _compile_rule(rule: object) -> tuple[_CompiledRule, ...]:
return (_RoutingRule(pattern=compiled, provider=provider),)
return (
_RoutingRule(pattern=compiled, provider=provider),
_CapabilityRule(pattern=compiled, model_info=model_info),
_CapabilityRule(pattern=compiled, model_info=model_info, backfill_exact_entries=backfill),
)
@ -151,6 +159,7 @@ class _FallbackGeneralizations:
self.rules: list = []
self.routing_rules: tuple = ()
self.capability_rules: tuple = ()
self.backfill_rules: tuple = ()
def set_rules(self, rules: list | None) -> None:
installed: Final = rules if isinstance(rules, list) else []
@ -158,6 +167,7 @@ class _FallbackGeneralizations:
self.rules = installed
self.routing_rules = tuple(rule for rule in compiled if isinstance(rule, _RoutingRule))
self.capability_rules = tuple(rule for rule in compiled if isinstance(rule, _CapabilityRule))
self.backfill_rules = tuple(rule for rule in self.capability_rules if rule.backfill_exact_entries)
def match_routing(self, model: str) -> str | None:
if not model:
@ -175,6 +185,14 @@ class _FallbackGeneralizations:
return None
return {key: value for model_info in matched for key, value in model_info.items()}
def match_backfill(self, model: str) -> dict | None:
if not model:
return None
matched = tuple(rule.model_info for rule in self.backfill_rules if rule.pattern.search(model) is not None)
if not matched:
return None
return {key: value for model_info in matched for key, value in model_info.items()}
_registry: Final = _FallbackGeneralizations()
@ -210,3 +228,12 @@ def match_capability_generalizations(model: str) -> dict | None:
capability rule matches. O(number of rules); only call once exact lookups have missed.
"""
return _registry.match_capabilities(model)
def match_backfill_generalizations(model: str) -> dict | None:
"""Return the union of flagged capability rules matching ``model``.
Later rules override earlier ones on key conflicts. Returns ``None`` when no
flagged rule matches. O(number of rules); only call once exact lookups have matched.
"""
return _registry.match_backfill(model)

View file

@ -57717,6 +57717,7 @@
{
"name": "claude-adaptive-thinking",
"pattern": "claude-[a-z]+-(?:4[-._](?:[6-9]|[1-9]\\d)(?!\\d)|(?:[5-9]|[1-9]\\d)(?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
"backfill_exact_entries": true,
"description": "Claude at version 4.6 or higher, in any id shape that contains claude-<family>-: minors 4.6 through 4.99, any later major-minor, and bare 5+ majors so a new family shaped like claude-fable-5 matches. Requiring the claude- prefix keeps non-Claude names such as team-sonnet-5-1 out. The minor is capped at two digits so an 8-digit date suffix such as claude-opus-4-20250514 is never read as a >= 4.6 minor. Turns on adaptive thinking for new versions and new families with no code change.",
"model_info": {
"supports_adaptive_thinking": true
@ -57725,6 +57726,7 @@
{
"name": "claude-legacy-thinking",
"pattern": "claude-[a-z]+-4[-._]6(?!\\d)",
"backfill_exact_entries": true,
"description": "Claude at version 4.6 exactly, in any id shape that contains claude-<family>-4-6 (dotted and underscored minors included, dated releases such as claude-sonnet-4-6-20260219 too). The 4.6 family is adaptive-thinking yet still accepts legacy thinking.type=enabled with budget_tokens, so the caller's hard budget cap is forwarded verbatim instead of being rewritten to an uncapped output_config.effort. The lookahead keeps two-digit minors such as 4-60 from matching. 4.7+ and 5+ majors reject the legacy shape and stay on the adaptive translation.",
"model_info": {
"supports_legacy_thinking": true
@ -57741,6 +57743,7 @@
{
"name": "claude-mid-conversation-system",
"pattern": "claude-[a-z]+-(?:4[-._](?:[89]|[1-9]\\d)(?!\\d)|(?:[5-9]|[1-9]\\d)(?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
"backfill_exact_entries": true,
"description": "Claude at version 4.8 or higher, in any id shape that contains claude-<family>-: minors 4.8 through 4.99, any later major-minor, and bare 5+ majors so a new family like claude-fable-5 matches. Anthropic introduced mid-conversation system messages with Opus 4.8 and every newer Claude keeps them; 4.7 and below reject the system role inside messages.",
"model_info": {
"supports_mid_conversation_system": true
@ -57757,6 +57760,7 @@
{
"name": "openai-reasoning-family-baseline",
"pattern": "^(?!.*search-api)(?:[a-z0-9_.-]+/)*(?:ft:)?(?:o[1-9]\\d*(?![a-z0-9])|gpt-[5-9](?:\\.\\d+)?(?![0-9.])|(?:gpt-\\d+(?:\\.\\d+)?(?:-[a-z0-9]+)*-)?(?:codex|deep-research|chat-latest)(?![a-z0-9]))",
"backfill_exact_entries": true,
"description": "OpenAI reasoning families by id shape, under any provider namespace and with an optional ft: prefix: the o-series (o1, o3-pro, o4-mini), gpt-5 through gpt-9 majors including dotted minors and suffixed variants (gpt-5.5-cyber, gpt-6-astra), and the codex, deep-research and chat-latest lines when standalone or on a gpt base. gpt-5-search-api is excluded because it is a search-only surface. Every model here is a reasoning model, and the Responses API drops the caller's reasoning param for any mapped OpenAI model whose info lacks supports_reasoning, so an id the registry has not named yet keeps its reasoning settings instead of silently losing them. Rules lose to exact entries. Carries no mode and no pricing, so cost stays on the standard unpriced behavior.",
"model_info": {
"supports_reasoning": true

View file

@ -83,6 +83,7 @@ from litellm.constants import (
)
from litellm.litellm_core_utils.core_helpers import normalize_drop_params
from litellm.litellm_core_utils.fallback_generalizations import (
match_backfill_generalizations,
match_capability_generalizations,
)
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_payload
@ -5819,6 +5820,11 @@ def _get_model_info_helper(
):
_model_info = None
if _model_info is not None and key is not None:
backfill: Final = match_backfill_generalizations(key)
if backfill is not None:
_model_info = {**{k: v for k, v in backfill.items() if k not in _model_info}, **_model_info}
if _model_info is None:
generalization: Final = _get_model_info_from_generalization(
model=model,

View file

@ -57717,6 +57717,7 @@
{
"name": "claude-adaptive-thinking",
"pattern": "claude-[a-z]+-(?:4[-._](?:[6-9]|[1-9]\\d)(?!\\d)|(?:[5-9]|[1-9]\\d)(?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
"backfill_exact_entries": true,
"description": "Claude at version 4.6 or higher, in any id shape that contains claude-<family>-: minors 4.6 through 4.99, any later major-minor, and bare 5+ majors so a new family shaped like claude-fable-5 matches. Requiring the claude- prefix keeps non-Claude names such as team-sonnet-5-1 out. The minor is capped at two digits so an 8-digit date suffix such as claude-opus-4-20250514 is never read as a >= 4.6 minor. Turns on adaptive thinking for new versions and new families with no code change.",
"model_info": {
"supports_adaptive_thinking": true
@ -57725,6 +57726,7 @@
{
"name": "claude-legacy-thinking",
"pattern": "claude-[a-z]+-4[-._]6(?!\\d)",
"backfill_exact_entries": true,
"description": "Claude at version 4.6 exactly, in any id shape that contains claude-<family>-4-6 (dotted and underscored minors included, dated releases such as claude-sonnet-4-6-20260219 too). The 4.6 family is adaptive-thinking yet still accepts legacy thinking.type=enabled with budget_tokens, so the caller's hard budget cap is forwarded verbatim instead of being rewritten to an uncapped output_config.effort. The lookahead keeps two-digit minors such as 4-60 from matching. 4.7+ and 5+ majors reject the legacy shape and stay on the adaptive translation.",
"model_info": {
"supports_legacy_thinking": true
@ -57741,6 +57743,7 @@
{
"name": "claude-mid-conversation-system",
"pattern": "claude-[a-z]+-(?:4[-._](?:[89]|[1-9]\\d)(?!\\d)|(?:[5-9]|[1-9]\\d)(?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
"backfill_exact_entries": true,
"description": "Claude at version 4.8 or higher, in any id shape that contains claude-<family>-: minors 4.8 through 4.99, any later major-minor, and bare 5+ majors so a new family like claude-fable-5 matches. Anthropic introduced mid-conversation system messages with Opus 4.8 and every newer Claude keeps them; 4.7 and below reject the system role inside messages.",
"model_info": {
"supports_mid_conversation_system": true
@ -57757,6 +57760,7 @@
{
"name": "openai-reasoning-family-baseline",
"pattern": "^(?!.*search-api)(?:[a-z0-9_.-]+/)*(?:ft:)?(?:o[1-9]\\d*(?![a-z0-9])|gpt-[5-9](?:\\.\\d+)?(?![0-9.])|(?:gpt-\\d+(?:\\.\\d+)?(?:-[a-z0-9]+)*-)?(?:codex|deep-research|chat-latest)(?![a-z0-9]))",
"backfill_exact_entries": true,
"description": "OpenAI reasoning families by id shape, under any provider namespace and with an optional ft: prefix: the o-series (o1, o3-pro, o4-mini), gpt-5 through gpt-9 majors including dotted minors and suffixed variants (gpt-5.5-cyber, gpt-6-astra), and the codex, deep-research and chat-latest lines when standalone or on a gpt base. gpt-5-search-api is excluded because it is a search-only surface. Every model here is a reasoning model, and the Responses API drops the caller's reasoning param for any mapped OpenAI model whose info lacks supports_reasoning, so an id the registry has not named yet keeps its reasoning settings instead of silently losing them. Rules lose to exact entries. Carries no mode and no pricing, so cost stays on the standard unpriced behavior.",
"model_info": {
"supports_reasoning": true

View file

@ -11,11 +11,11 @@ import logging
import pytest
import litellm
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.fallback_generalizations import (
get_fallback_generalization_rules,
match_backfill_generalizations,
match_capability_generalizations,
match_routing_generalization,
set_fallback_generalizations,
@ -116,6 +116,42 @@ def test_capability_union_is_last_wins_in_file_order(restore_generalizations):
}
def test_backfill_requires_per_rule_opt_in(restore_generalizations):
restore_generalizations(
[
{"name": "base", "pattern": r"^acme-", "model_info": {"supports_reasoning": True}},
{
"name": "opt-in",
"pattern": r"^acme-",
"backfill_exact_entries": True,
"model_info": {"supports_vision": True},
},
]
)
assert match_backfill_generalizations("acme-1") == {"supports_vision": True}
assert match_capability_generalizations("acme-1") == {
"supports_reasoning": True,
"supports_vision": True,
}
restore_generalizations(
[{"name": "base", "pattern": r"^acme-", "model_info": {"supports_reasoning": True}}]
)
assert match_backfill_generalizations("acme-1") is None
restore_generalizations(
[
{
"name": "route",
"pattern": r"^acme-",
"backfill_exact_entries": True,
"model_info": {"litellm_provider": "openai"},
}
]
)
assert match_backfill_generalizations("acme-1") is None
def test_routing_rules_are_excluded_from_capability_results(restore_generalizations):
restore_generalizations(
[
@ -299,6 +335,58 @@ def test_exact_entry_takes_precedence_over_rule(restore_generalizations):
assert info["input_cost_per_token"] != 999.0
def test_exact_entries_backfill_only_missing_fields(restore_generalizations, monkeypatch):
monkeypatch.setattr(
litellm,
"model_cost",
{
**litellm.model_cost,
"acme-full": {
"input_cost_per_token": 1e-6,
"output_cost_per_token": 2e-6,
"litellm_provider": "openai",
"mode": "chat",
"max_tokens": 7,
"supports_reasoning": False,
},
"acme-bare": {
"input_cost_per_token": 3e-6,
"output_cost_per_token": 4e-6,
"litellm_provider": "openai",
"mode": "chat",
},
},
)
restore_generalizations(
[
{
"name": "acme-backfill",
"pattern": r"^acme-",
"backfill_exact_entries": True,
"model_info": {"supports_reasoning": True, "max_tokens": 5},
}
]
)
litellm.get_model_info.cache_clear()
full = litellm.get_model_info("acme-full", custom_llm_provider="openai")
assert full["supports_reasoning"] is False
assert full["max_tokens"] == 7
bare = litellm.get_model_info("acme-bare", custom_llm_provider="openai")
assert bare["supports_reasoning"] is True
assert bare["max_tokens"] == 5
assert bare["input_cost_per_token"] == 3e-6
assert bare["key"] == "acme-bare"
restore_generalizations(
[{"name": "acme-backfill", "pattern": r"^acme-", "model_info": {"supports_reasoning": True, "max_tokens": 5}}]
)
litellm.get_model_info.cache_clear()
unflagged = litellm.get_model_info("acme-bare", custom_llm_provider="openai")
assert unflagged.get("supports_reasoning") is None
# --------------------------------------------------------------------------- #
# Shipped rules (bundled cost map)
# --------------------------------------------------------------------------- #
@ -605,6 +693,10 @@ def test_shipped_wandb_rule_loses_to_mapped_non_reasoning_entries(shipped_cost_m
assert litellm.supports_reasoning(model=model, custom_llm_provider="wandb") is False, model
def test_shipped_wandb_rule_does_not_backfill_mapped_entries(shipped_cost_map):
assert match_backfill_generalizations("wandb/meta-llama/Llama-3.1-8B-Instruct") is None
def test_shipped_wandb_rule_is_anchored_to_the_wandb_namespace(shipped_cost_map):
"""``^wandb/`` is anchored, so it cannot leak onto another provider's ids."""
assert match_capability_generalizations("wandb/some-new-model") == {"supports_reasoning": True}
@ -722,3 +814,33 @@ def test_shipped_openai_reasoning_rule_skips_non_reasoning_gpt_ids(shipped_cost_
def test_shipped_openai_reasoning_rule_loses_to_mapped_entries(shipped_cost_map):
assert "gpt-5-search-api" in litellm.model_cost
assert litellm.supports_reasoning(model="gpt-5-search-api", custom_llm_provider="openai") is False
@pytest.mark.parametrize(
"model,provider",
[
("azure/us/o1-2024-12-17", "azure"),
("github_copilot/gpt-5", "github_copilot"),
],
)
def test_shipped_openai_reasoning_rule_backfills_mapped_entries(shipped_cost_map, model, provider):
assert model in litellm.model_cost
raw_entry = litellm.model_cost[model]
assert "supports_reasoning" not in raw_entry
model_without_provider = model.removeprefix(f"{provider}/")
assert litellm.supports_reasoning(model=model_without_provider, custom_llm_provider=provider) is True
info = litellm.get_model_info(model=model_without_provider, custom_llm_provider=provider)
assert info["input_cost_per_token"] == raw_entry.get("input_cost_per_token", 0)
def test_shipped_claude_thinking_rules_backfill_without_family_limits(shipped_cost_map):
model = "perplexity/anthropic/claude-sonnet-4-6"
assert model in litellm.model_cost
raw_entry = litellm.model_cost[model]
assert "supports_adaptive_thinking" not in raw_entry
assert "max_input_tokens" not in raw_entry
info = litellm.get_model_info(model="anthropic/claude-sonnet-4-6", custom_llm_provider="perplexity")
assert info["supports_adaptive_thinking"] is True
assert info["supports_legacy_thinking"] is True
assert info.get("max_input_tokens") is None