mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-16 23:41:43 +00:00
refactor(model_info): rename backfill_exact_entries to fill_missing_fields
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
42c708670d
commit
fb2057fde7
5 changed files with 44 additions and 41 deletions
|
|
@ -34,10 +34,10 @@ 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
|
||||
Rules flagged with ``fill_missing_fields: 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.
|
||||
conflict. Only flagged capability rules participate in this fill; 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,
|
||||
|
|
@ -62,7 +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"
|
||||
FILL_MISSING_FIELDS_FIELD: Final = "fill_missing_fields"
|
||||
|
||||
|
||||
def _resolve_legacy_extends(rules: list) -> list:
|
||||
|
|
@ -104,7 +104,7 @@ class _RoutingRule:
|
|||
class _CapabilityRule:
|
||||
pattern: re.Pattern
|
||||
model_info: dict
|
||||
backfill_exact_entries: bool
|
||||
fill_missing_fields: bool
|
||||
|
||||
|
||||
_CompiledRule = _RoutingRule | _CapabilityRule
|
||||
|
|
@ -132,9 +132,9 @@ def _compile_rule(rule: object) -> tuple[_CompiledRule, ...]:
|
|||
e,
|
||||
)
|
||||
return ()
|
||||
backfill: Final = rule.get(BACKFILL_FIELD) is True
|
||||
fill_missing_fields: Final = rule.get(FILL_MISSING_FIELDS_FIELD) is True
|
||||
if PROVIDER_KEY not in model_info:
|
||||
return (_CapabilityRule(pattern=compiled, model_info=model_info, backfill_exact_entries=backfill),)
|
||||
return (_CapabilityRule(pattern=compiled, model_info=model_info, fill_missing_fields=fill_missing_fields),)
|
||||
provider: Final = model_info[PROVIDER_KEY]
|
||||
if not isinstance(provider, str):
|
||||
verbose_logger.warning(
|
||||
|
|
@ -148,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, backfill_exact_entries=backfill),
|
||||
_CapabilityRule(pattern=compiled, model_info=model_info, fill_missing_fields=fill_missing_fields),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -159,7 +159,7 @@ class _FallbackGeneralizations:
|
|||
self.rules: list = []
|
||||
self.routing_rules: tuple = ()
|
||||
self.capability_rules: tuple = ()
|
||||
self.backfill_rules: tuple = ()
|
||||
self.fill_missing_rules: tuple = ()
|
||||
|
||||
def set_rules(self, rules: list | None) -> None:
|
||||
installed: Final = rules if isinstance(rules, list) else []
|
||||
|
|
@ -167,7 +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)
|
||||
self.fill_missing_rules = tuple(rule for rule in self.capability_rules if rule.fill_missing_fields)
|
||||
|
||||
def match_routing(self, model: str) -> str | None:
|
||||
if not model:
|
||||
|
|
@ -185,16 +185,16 @@ 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:
|
||||
def match_fill_missing(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)
|
||||
matched = tuple(rule.model_info for rule in self.fill_missing_rules if rule.pattern.search(model) is not None)
|
||||
if not matched:
|
||||
return None
|
||||
backfill: Final = {
|
||||
fill_missing: Final = {
|
||||
key: value for model_info in matched for key, value in model_info.items() if key != PROVIDER_KEY
|
||||
}
|
||||
return backfill or None
|
||||
return fill_missing or None
|
||||
|
||||
|
||||
_registry: Final = _FallbackGeneralizations()
|
||||
|
|
@ -233,10 +233,10 @@ def match_capability_generalizations(model: str) -> dict | None:
|
|||
return _registry.match_capabilities(model)
|
||||
|
||||
|
||||
def match_backfill_generalizations(model: str) -> dict | None:
|
||||
def match_fill_missing_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)
|
||||
return _registry.match_fill_missing(model)
|
||||
|
|
|
|||
|
|
@ -57717,7 +57717,7 @@
|
|||
{
|
||||
"name": "claude-adaptive-thinking",
|
||||
"pattern": "claude-[a-z]+-(?:4[-._](?:[6-9]|[1-9]\\d)(?!\\d)|[5-9](?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
|
||||
"backfill_exact_entries": true,
|
||||
"fill_missing_fields": 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. Two-digit majors are deliberately not matched so ids like claude-opus-41 (4.1) are not read as major 41. Turns on adaptive thinking for new versions and new families with no code change.",
|
||||
"model_info": {
|
||||
"supports_adaptive_thinking": true
|
||||
|
|
@ -57726,7 +57726,7 @@
|
|||
{
|
||||
"name": "claude-legacy-thinking",
|
||||
"pattern": "claude-[a-z]+-4[-._]6(?!\\d)",
|
||||
"backfill_exact_entries": true,
|
||||
"fill_missing_fields": 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
|
||||
|
|
@ -57743,7 +57743,7 @@
|
|||
{
|
||||
"name": "claude-mid-conversation-system",
|
||||
"pattern": "claude-[a-z]+-(?:4[-._](?:[89]|[1-9]\\d)(?!\\d)|[5-9](?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
|
||||
"backfill_exact_entries": true,
|
||||
"fill_missing_fields": 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. Two-digit majors are deliberately not matched so ids like claude-opus-41 (4.1) are not read as major 41. 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
|
||||
|
|
@ -57760,7 +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,
|
||||
"fill_missing_fields": 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
|
||||
|
|
|
|||
|
|
@ -83,8 +83,8 @@ 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,
|
||||
match_fill_missing_generalizations,
|
||||
)
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_payload
|
||||
|
||||
|
|
@ -5822,9 +5822,12 @@ def _get_model_info_helper(
|
|||
_model_info = None
|
||||
|
||||
if _model_info is not None and key is not None and _model_info.get("mode", "chat") in _BACKFILL_MODES:
|
||||
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}
|
||||
fill_missing: Final = match_fill_missing_generalizations(key)
|
||||
if fill_missing is not None:
|
||||
_model_info = {
|
||||
**{k: v for k, v in fill_missing.items() if k not in _model_info},
|
||||
**_model_info,
|
||||
}
|
||||
|
||||
if _model_info is None:
|
||||
generalization: Final = _get_model_info_from_generalization(
|
||||
|
|
|
|||
|
|
@ -57717,7 +57717,7 @@
|
|||
{
|
||||
"name": "claude-adaptive-thinking",
|
||||
"pattern": "claude-[a-z]+-(?:4[-._](?:[6-9]|[1-9]\\d)(?!\\d)|[5-9](?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
|
||||
"backfill_exact_entries": true,
|
||||
"fill_missing_fields": 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. Two-digit majors are deliberately not matched so ids like claude-opus-41 (4.1) are not read as major 41. Turns on adaptive thinking for new versions and new families with no code change.",
|
||||
"model_info": {
|
||||
"supports_adaptive_thinking": true
|
||||
|
|
@ -57726,7 +57726,7 @@
|
|||
{
|
||||
"name": "claude-legacy-thinking",
|
||||
"pattern": "claude-[a-z]+-4[-._]6(?!\\d)",
|
||||
"backfill_exact_entries": true,
|
||||
"fill_missing_fields": 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
|
||||
|
|
@ -57743,7 +57743,7 @@
|
|||
{
|
||||
"name": "claude-mid-conversation-system",
|
||||
"pattern": "claude-[a-z]+-(?:4[-._](?:[89]|[1-9]\\d)(?!\\d)|[5-9](?!\\d)(?:[-._]\\d{1,2}(?!\\d))?)",
|
||||
"backfill_exact_entries": true,
|
||||
"fill_missing_fields": 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. Two-digit majors are deliberately not matched so ids like claude-opus-41 (4.1) are not read as major 41. 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
|
||||
|
|
@ -57760,7 +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,
|
||||
"fill_missing_fields": 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
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ 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_fill_missing_generalizations,
|
||||
match_routing_generalization,
|
||||
set_fallback_generalizations,
|
||||
)
|
||||
|
|
@ -116,19 +116,19 @@ def test_capability_union_is_last_wins_in_file_order(restore_generalizations):
|
|||
}
|
||||
|
||||
|
||||
def test_backfill_requires_per_rule_opt_in(restore_generalizations):
|
||||
def test_fill_missing_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,
|
||||
"fill_missing_fields": True,
|
||||
"model_info": {"supports_vision": True},
|
||||
},
|
||||
]
|
||||
)
|
||||
assert match_backfill_generalizations("acme-1") == {"supports_vision": True}
|
||||
assert match_fill_missing_generalizations("acme-1") == {"supports_vision": True}
|
||||
assert match_capability_generalizations("acme-1") == {
|
||||
"supports_reasoning": True,
|
||||
"supports_vision": True,
|
||||
|
|
@ -137,31 +137,31 @@ def test_backfill_requires_per_rule_opt_in(restore_generalizations):
|
|||
restore_generalizations(
|
||||
[{"name": "base", "pattern": r"^acme-", "model_info": {"supports_reasoning": True}}]
|
||||
)
|
||||
assert match_backfill_generalizations("acme-1") is None
|
||||
assert match_fill_missing_generalizations("acme-1") is None
|
||||
|
||||
restore_generalizations(
|
||||
[
|
||||
{
|
||||
"name": "mixed",
|
||||
"pattern": r"^acme-",
|
||||
"backfill_exact_entries": True,
|
||||
"fill_missing_fields": True,
|
||||
"model_info": {"litellm_provider": "openai", "supports_vision": True},
|
||||
}
|
||||
]
|
||||
)
|
||||
assert match_backfill_generalizations("acme-1") == {"supports_vision": True}
|
||||
assert match_fill_missing_generalizations("acme-1") == {"supports_vision": True}
|
||||
|
||||
restore_generalizations(
|
||||
[
|
||||
{
|
||||
"name": "route",
|
||||
"pattern": r"^acme-",
|
||||
"backfill_exact_entries": True,
|
||||
"fill_missing_fields": True,
|
||||
"model_info": {"litellm_provider": "openai"},
|
||||
}
|
||||
]
|
||||
)
|
||||
assert match_backfill_generalizations("acme-1") is None
|
||||
assert match_fill_missing_generalizations("acme-1") is None
|
||||
|
||||
|
||||
def test_routing_rules_are_excluded_from_capability_results(restore_generalizations):
|
||||
|
|
@ -347,7 +347,7 @@ 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):
|
||||
def test_exact_entries_fill_only_missing_fields(restore_generalizations, monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
litellm,
|
||||
"model_cost",
|
||||
|
|
@ -380,7 +380,7 @@ def test_exact_entries_backfill_only_missing_fields(restore_generalizations, mon
|
|||
{
|
||||
"name": "acme-backfill",
|
||||
"pattern": r"^acme-",
|
||||
"backfill_exact_entries": True,
|
||||
"fill_missing_fields": True,
|
||||
"model_info": {"supports_reasoning": True, "max_tokens": 5},
|
||||
}
|
||||
]
|
||||
|
|
@ -726,8 +726,8 @@ 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_does_not_fill_missing_mapped_entries(shipped_cost_map):
|
||||
assert match_fill_missing_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):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue