Merge branch 'main' of https://github.com/BerriAI/litellm into pr-47905

This commit is contained in:
Zhong Zhiyi 2026-11-03 11:00:31 +08:00
commit 03757815f5
15 changed files with 711 additions and 73 deletions

View file

@ -16,6 +16,7 @@ from openai.types.responses.response_input_param import (
ResponseCustomToolCallOutputParam,
ResponseCustomToolCallParam,
)
from openai.types.responses.tool_choice_allowed_param import ToolChoiceAllowedParam
from openai.types.responses.tool_choice_custom_param import ToolChoiceCustomParam
from openai.types.responses.tool_choice_function_param import ToolChoiceFunctionParam
from openai.types.responses.tool_param import FunctionToolParam
@ -34,6 +35,10 @@ from litellm.completion_extras.litellm_responses_transformation.history import (
requested_phase,
)
from litellm.integrations.anthropic_cache_control_hook import supports_openai_prompt_cache_breakpoint
from litellm.litellm_core_utils.allowed_tools_tool_choice import (
parse_chat_allowed_tools_tool_choice,
responses_allowed_tools_tool_choice,
)
from litellm.litellm_core_utils.core_helpers import drops_unsupported_params
from litellm.litellm_core_utils.hidden_params import get_hidden_params, get_or_create_hidden_params
from litellm.litellm_core_utils.prompt_templates.common_utils import (
@ -783,13 +788,28 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
def _normalize_tool_choice_for_responses_api(
self, tool_choice: _ToolChoiceT
) -> _ToolChoiceT | ToolChoiceFunctionParam | ToolChoiceCustomParam | Literal["auto", "none", "required"]:
) -> (
_ToolChoiceT
| ToolChoiceFunctionParam
| ToolChoiceCustomParam
| ToolChoiceAllowedParam
| Literal["auto", "none", "required"]
):
"""Chat tool_choice nests the name under function/custom; Responses API expects top-level name."""
if not isinstance(tool_choice, dict):
return tool_choice
choice_type: Final = tool_choice.get("type")
if isinstance(choice_type, str) and choice_type in ("auto", "none", "required"):
return choice_type
if choice_type == "allowed_tools":
parsed: Final = parse_chat_allowed_tools_tool_choice(
cast( # cast-ok: widening an untyped dict to object is always sound, the helper validates it with pydantic
object, tool_choice
)
)
flat: Final = responses_allowed_tools_tool_choice(parsed) if parsed is not None else None
if flat is not None:
return flat
if choice_type not in ("function", "custom"):
return tool_choice
if isinstance(tool_choice.get("name"), str) and tool_choice.get("name"):

View file

@ -0,0 +1,111 @@
from collections.abc import Mapping
from typing import Final, Literal
from openai.types.responses.tool_choice_allowed_param import ToolChoiceAllowedParam
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError
from litellm.exceptions import BadRequestError
ALLOWED_TOOLS_TOOL_CHOICE_PROVIDERS: Final = frozenset({"openai", "azure", "litellm_proxy"})
CHAT_ALLOWED_TOOLS_LAYOUT: Final = (
'{"type": "allowed_tools", "allowed_tools": {"mode": "auto" | "required", "tools": [<at least one tool>]}}'
)
class ChatAllowedTools(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
mode: Literal["auto", "required"]
tools: tuple[Mapping[str, object], ...] = Field(min_length=1)
class ChatAllowedToolsToolChoice(BaseModel):
model_config = ConfigDict(frozen=True, extra="forbid")
type: Literal["allowed_tools"]
allowed_tools: ChatAllowedTools
_CHAT_ALLOWED_TOOLS_TOOL_CHOICE: Final = TypeAdapter(ChatAllowedToolsToolChoice)
_SELECTOR: Final = TypeAdapter(dict[str, object])
class _ToolChoiceType(BaseModel):
model_config = ConfigDict(frozen=True)
type: str
_TOOL_CHOICE_TYPE: Final = TypeAdapter(_ToolChoiceType)
def is_allowed_tools_tool_choice(tool_choice: object) -> bool:
try:
return _TOOL_CHOICE_TYPE.validate_python(tool_choice).type == "allowed_tools"
except ValidationError:
return False
def raise_if_allowed_tools_tool_choice_unsupported(
tool_choice: object,
custom_llm_provider: str,
model: str,
) -> None:
"""Providers outside the set would silently send the request with no tool restriction at all, so the call stops
here with a 400. ``drop_params`` is deliberately not consulted: dropping a restriction widens what the model may call."""
if not is_allowed_tools_tool_choice(tool_choice) or custom_llm_provider in ALLOWED_TOOLS_TOOL_CHOICE_PROVIDERS:
return
raise BadRequestError(
message=(
"tool_choice type 'allowed_tools' is only supported for "
f"{', '.join(sorted(ALLOWED_TOOLS_TOOL_CHOICE_PROVIDERS))}, got custom_llm_provider={custom_llm_provider}. "
"drop_params does not drop it, since that would let the model call tools the request excluded. "
"Send only the allowed tools in `tools` with tool_choice='auto' or 'required' instead."
),
model=model,
llm_provider=custom_llm_provider,
)
def parse_chat_allowed_tools_tool_choice(tool_choice: object) -> ChatAllowedToolsToolChoice | None:
try:
return _CHAT_ALLOWED_TOOLS_TOOL_CHOICE.validate_python(tool_choice)
except ValidationError:
return None
def _has_name(selector: Mapping[str, object]) -> bool:
name: Final = selector.get("name")
return isinstance(name, str) and name != ""
def _responses_allowed_tool(tool: Mapping[str, object]) -> Mapping[str, object] | None:
tool_type: Final = tool.get("type")
if tool_type not in ("function", "custom"):
return tool
if tool_type not in tool:
return tool if _has_name(tool) else None
try:
nested: Final = _SELECTOR.validate_python(tool[tool_type])
except ValidationError:
return None
outer: Final = {key: value for key, value in tool.items() if key != tool_type}
if not _has_name(nested) or outer.keys() & nested.keys():
return None
return {**outer, **nested}
def responses_allowed_tools_tool_choice(choice: ChatAllowedToolsToolChoice) -> ToolChoiceAllowedParam | None:
"""The flat Responses API layout of a chat ``allowed_tools`` choice, or ``None`` when an entry is malformed or
carries conflicting keys, so the caller sends the original rather than a rewrite that could mean something looser."""
flattened: Final = tuple(_responses_allowed_tool(tool) for tool in choice.allowed_tools.tools)
if any(tool is None for tool in flattened):
return None
return ToolChoiceAllowedParam(
type="allowed_tools",
mode=choice.allowed_tools.mode,
tools=[dict(tool) for tool in flattened if tool is not None],
)

View file

@ -1,11 +1,17 @@
from collections.abc import Mapping, Sequence
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Final
from typing import (
TYPE_CHECKING,
Any,
Final,
cast, # noqa: TID251 # non_default_params reaches the mapper as an untyped dict
)
from httpx._models import Headers, Response
import litellm
from litellm.integrations.anthropic_cache_control_hook import deployment_prompt_cache_breakpoint_support
from litellm.litellm_core_utils.allowed_tools_tool_choice import is_allowed_tools_tool_choice
from litellm.litellm_core_utils.core_helpers import drops_unsupported_params
from litellm.litellm_core_utils.prompt_templates.common_utils import (
chat_completion_prompt_cache_breakpoints_for_wire,
@ -245,12 +251,12 @@ class AzureOpenAIConfig(BaseConfig):
or (api_version_year == "2023" and api_version_month < "12")
or (api_version_year == "2023" and api_version_month == "12" and api_version_day < "01")
):
if litellm.drop_params is True or (drop_params is not None and drop_params is True):
pass
else:
if is_allowed_tools_tool_choice(
cast(object, value) # cast-ok: widening an untyped dict value to object is always sound
) or not (litellm.drop_params is True or (drop_params is not None and drop_params is True)):
raise UnsupportedParamsError(
status_code=400,
message=f"""Azure does not support 'tool_choice', for api_version={api_version}. Bump your API version to '2023-12-01-preview' or later. This parameter requires 'api_version="2023-12-01-preview"' or later. Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions""",
message=f"""Azure does not support 'tool_choice', for api_version={api_version}. Bump your API version to '2023-12-01-preview' or later. This parameter requires 'api_version="2023-12-01-preview"' or later. drop_params never drops an allowed_tools tool_choice, since that would let the model call tools the request excluded. Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions""",
)
elif value == "required" and (
api_version_year == "2024" and api_version_month <= "05"

View file

@ -70,6 +70,9 @@ from litellm.constants import (
)
from litellm.exceptions import LiteLLMUnknownProvider
from litellm.integrations.custom_logger import CustomLogger
from litellm.litellm_core_utils.allowed_tools_tool_choice import (
raise_if_allowed_tools_tool_choice_unsupported,
)
from litellm.litellm_core_utils.asyncify import asyncify, run_async_function
from litellm.litellm_core_utils.audio_utils.utils import (
calculate_request_duration,
@ -5956,6 +5959,14 @@ def completion(
if not is_claude_tool_target(custom_llm_provider=custom_llm_provider, model=model):
tools = _drop_anthropic_only_tool_keys(tools=tools)
raise_if_allowed_tools_tool_choice_unsupported(
tool_choice=cast( # cast-ok: widening an untyped dict to object is always sound, the helper validates it with pydantic
object, tool_choice
),
custom_llm_provider=custom_llm_provider,
model=model,
)
if provider_specific_header is not None:
headers.update(
ProviderSpecificHeaderUtils.get_provider_specific_headers(

View file

@ -34809,6 +34809,7 @@
"gpt-4.1": {
"cache_read_input_token_cost": 5e-07,
"cache_read_input_token_cost_priority": 8.75e-07,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 2e-06,
"input_cost_per_token_batches": 1e-06,
"input_cost_per_token_priority": 3.5e-06,
@ -34894,6 +34895,7 @@
"gpt-4.1-mini": {
"cache_read_input_token_cost": 1e-07,
"cache_read_input_token_cost_priority": 1.75e-07,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 4e-07,
"input_cost_per_token_batches": 2e-07,
"input_cost_per_token_priority": 7e-07,
@ -35053,6 +35055,7 @@
"gpt-4o": {
"cache_read_input_token_cost": 1.25e-06,
"cache_read_input_token_cost_priority": 2.125e-06,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 2.5e-06,
"input_cost_per_token_batches": 1.25e-06,
"input_cost_per_token_priority": 4.25e-06,
@ -35397,6 +35400,7 @@
"gpt-4o-mini": {
"cache_read_input_token_cost": 7.5e-08,
"cache_read_input_token_cost_priority": 1.25e-07,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 1.5e-07,
"input_cost_per_token_batches": 7.5e-08,
"input_cost_per_token_priority": 2.5e-07,

View file

@ -1,14 +1,15 @@
from __future__ import annotations
from collections.abc import Mapping, Sequence
from collections.abc import Callable, Mapping, Sequence
from dataclasses import dataclass
from datetime import date, datetime, timezone
from itertools import groupby
from types import MappingProxyType
from typing import TYPE_CHECKING, Final
from typing import TYPE_CHECKING, Final, TypeAlias
import litellm
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.get_model_cost_map import AliasedCostEntry, GetModelCostMap
from litellm.types.proxy.model_deprecation import (
DEFAULT_DEPRECATION_WARN_DAYS,
DeprecationStatus,
@ -20,6 +21,9 @@ if TYPE_CHECKING:
from litellm.router import Router
_NO_MODEL_METADATA: Final[Mapping[str, object]] = MappingProxyType({})
_SHARED_SCHEDULE_PROVIDER: Final[Mapping[str, str]] = MappingProxyType({"azure_ai": "azure"})
LoadedCatalog: TypeAlias = Callable[[], Mapping[str, Mapping[str, object]]]
@dataclass(frozen=True, slots=True)
@ -27,6 +31,7 @@ class _ResolvedDeprecation:
deprecation_date: date
litellm_model: str | None
litellm_provider: str | None
copied_from_another_row: bool = False
def _parse_deprecation_date(raw_value: object) -> date | None:
@ -47,8 +52,8 @@ def _successor_model(raw_value: object) -> str | None:
return raw_value.strip() or None if isinstance(raw_value, str) else None
def _cost_map_lookup(model_key: object) -> _ResolvedDeprecation | None:
if not isinstance(model_key, str) or not model_key:
def _cost_map_lookup(model_key: str | None, catalog: Mapping[str, object]) -> _ResolvedDeprecation | None:
if model_key is None:
return None
entry: Final = litellm.model_cost.get(model_key)
if not isinstance(entry, Mapping):
@ -61,6 +66,51 @@ def _cost_map_lookup(model_key: object) -> _ResolvedDeprecation | None:
deprecation_date=parsed,
litellm_model=model_key,
litellm_provider=provider if isinstance(provider, str) else None,
copied_from_another_row=isinstance(entry, AliasedCostEntry)
and "deprecation_date" not in entry.registered_fields
and model_key not in catalog,
)
def _first_own_row(model_keys: Sequence[str | None], catalog: Mapping[str, object]) -> _ResolvedDeprecation | None:
found: Final = tuple(
resolved for resolved in (_cost_map_lookup(key, catalog) for key in model_keys) if resolved is not None
)
return next((resolved for resolved in found if not resolved.copied_from_another_row), next(iter(found), None))
def _deployment_provider(litellm_params: Mapping[str, object]) -> str | None:
declared: Final = litellm_params.get("custom_llm_provider")
if isinstance(declared, str) and declared:
return declared
model: Final = litellm_params.get("model")
prefix: Final = model.split("/", 1)[0] if isinstance(model, str) and "/" in model else None
return prefix if prefix in litellm.provider_list else None
def _provider_qualified(provider: str | None, model_key: str | None) -> str | None:
if provider is None or model_key is None or model_key.startswith(f"{provider}/"):
return None
return f"{provider}/{model_key}"
def _as_model_key(raw_value: object) -> str | None:
return raw_value if isinstance(raw_value, str) and raw_value else None
def _without_provider(provider: str | None, model_name: str | None) -> str | None:
if provider is None or model_name is None:
return model_name
return model_name.removeprefix(f"{provider}/")
def _schedule_keys(provider: str | None, model_name: str | None) -> tuple[str | None, ...]:
shared_provider: Final = _SHARED_SCHEDULE_PROVIDER.get(provider) if provider is not None else None
unprefixed: Final = _without_provider(provider, model_name)
return (
_provider_qualified(provider, unprefixed),
_provider_qualified(shared_provider, unprefixed),
model_name,
)
@ -70,34 +120,37 @@ def _mapping_field(deployment: Mapping[str, object], key: str) -> Mapping[str, o
def _resolve_deployment_deprecation(
deployment: Mapping[str, object],
deployment: Mapping[str, object], catalog: Mapping[str, object]
) -> _ResolvedDeprecation | None:
"""Resolve a deployment's deprecation date, preferring its explicit override"""
model_info: Final = _mapping_field(deployment, "model_info")
raw_model: Final = _mapping_field(deployment, "litellm_params").get("model")
litellm_params: Final = _mapping_field(deployment, "litellm_params")
model_key: Final = _as_model_key(litellm_params.get("model"))
override: Final = _parse_deprecation_date(model_info.get("deprecation_date"))
if override is not None:
provider: Final = model_info.get("litellm_provider")
override_provider: Final = model_info.get("litellm_provider")
return _ResolvedDeprecation(
deprecation_date=override,
litellm_model=raw_model if isinstance(raw_model, str) else None,
litellm_provider=provider if isinstance(provider, str) else None,
litellm_model=model_key,
litellm_provider=override_provider if isinstance(override_provider, str) else None,
)
unprefixed: Final = raw_model.split("/", 1)[1] if isinstance(raw_model, str) and "/" in raw_model else None
return next(
(
resolved
for resolved in (
_cost_map_lookup(model_info.get("base_model")),
_cost_map_lookup(raw_model),
_cost_map_lookup(unprefixed),
)
if resolved is not None
),
None,
provider: Final = _deployment_provider(litellm_params)
base_model: Final = _as_model_key(model_info.get("base_model"))
model_name: Final = model_key.split("/", 1)[1] if model_key is not None and "/" in model_key else model_key
picks: Final = tuple(
pick
for pick in (
_first_own_row(_schedule_keys(provider, base_model), catalog),
_first_own_row(
(_provider_qualified(provider, model_key), model_key, *_schedule_keys(provider, model_name)), catalog
),
)
if pick is not None
)
own_rows: Final = tuple(pick for pick in picks if not pick.copied_from_another_row)
return min(own_rows or picks, key=lambda pick: pick.deprecation_date, default=None)
def _classify(days_until: int, warn_within_days: int) -> DeprecationStatus:
@ -124,13 +177,17 @@ def _checked_successor(model_name: str, model_info: Mapping[str, object], llm_ro
def _build_info(
deployment: Mapping[str, object], llm_router: Router, today: date, warn_within_days: int
deployment: Mapping[str, object],
llm_router: Router,
today: date,
warn_within_days: int,
catalog: Mapping[str, object],
) -> ModelDeprecationInfo | None:
model_name: Final = deployment.get("model_name")
if not isinstance(model_name, str) or not model_name:
return None
resolved: Final = _resolve_deployment_deprecation(deployment)
resolved: Final = _resolve_deployment_deprecation(deployment, catalog)
if resolved is None:
return None
@ -182,15 +239,12 @@ def _bucket(models: Sequence[ModelDeprecationInfo], status: DeprecationStatus) -
)
def _collect_from_router(llm_router: Router, today: date, warn_within_days: int) -> tuple[ModelDeprecationInfo, ...]:
def _collect_from_router(
llm_router: Router, today: date, warn_within_days: int, catalog: Mapping[str, object]
) -> tuple[ModelDeprecationInfo, ...]:
deployments: Final = llm_router.registry().deployments() or ()
deduped: Final = _dedupe(
tuple(
info
for info in (_build_info(deployment, llm_router, today, warn_within_days) for deployment in deployments)
if info is not None
)
)
infos: Final = (_build_info(deployment, llm_router, today, warn_within_days, catalog) for deployment in deployments)
deduped: Final = _dedupe(tuple(info for info in infos if info is not None))
verbose_logger.debug(
"model_deprecation: %d/%d deployments carry a deprecation date",
@ -204,12 +258,15 @@ def collect_model_deprecations(
llm_router: Router | None,
warn_within_days: int = DEFAULT_DEPRECATION_WARN_DAYS,
today: date | None = None,
loaded_catalog: LoadedCatalog = GetModelCostMap.loaded_model_cost_map,
) -> ModelDeprecationResponse:
"""Bucket every deployment carrying a deprecation date by how urgent it is"""
snapshot_time: Final = datetime.now(timezone.utc)
effective_today: Final = today or snapshot_time.date()
deduped: Final = (
_collect_from_router(llm_router, effective_today, warn_within_days) if llm_router is not None else ()
_collect_from_router(llm_router, effective_today, warn_within_days, loaded_catalog())
if llm_router is not None
else ()
)
return ModelDeprecationResponse(

View file

@ -83,6 +83,10 @@ from litellm.constants import (
PROVIDERS_THAT_AUTHENTICATE_ON_PROVIDER_INFO,
TOOL_CHOICE_OBJECT_TOKEN_COUNT,
)
from litellm.litellm_core_utils.allowed_tools_tool_choice import (
CHAT_ALLOWED_TOOLS_LAYOUT,
parse_chat_allowed_tools_tool_choice,
)
from litellm.litellm_core_utils.core_helpers import (
bind_budget_reservation_to_callbacks,
normalize_drop_params,
@ -9470,9 +9474,28 @@ def validate_chat_completion_tool_choice(
tool_choice_type = tool_choice.get("type")
if tool_choice_type in ("auto", "none", "required") and "function" not in tool_choice:
return tool_choice_type
if (
tool_choice_type == "allowed_tools"
and (
parse_chat_allowed_tools_tool_choice(
cast( # cast-ok: widening an untyped dict to object is always sound, the helper validates it with pydantic
object, tool_choice
)
)
is None
)
):
raise BadRequestError(
message=(
f"Invalid tool choice, tool_choice={tool_choice}. On chat completions an allowed_tools "
f"tool_choice must be {CHAT_ALLOWED_TOOLS_LAYOUT}"
),
model=model,
llm_provider="",
)
# Standard OpenAI format: {"type": "function", "function": {...}}
if tool_choice.get("type") is None or tool_choice.get("function") is None:
if tool_choice_type != "allowed_tools" and (tool_choice_type is None or tool_choice.get("function") is None):
raise BadRequestError(
message=f"Invalid tool choice, tool_choice={tool_choice}. Please ensure tool_choice follows the OpenAI spec",
model=model,

View file

@ -34809,6 +34809,7 @@
"gpt-4.1": {
"cache_read_input_token_cost": 5e-07,
"cache_read_input_token_cost_priority": 8.75e-07,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 2e-06,
"input_cost_per_token_batches": 1e-06,
"input_cost_per_token_priority": 3.5e-06,
@ -34894,6 +34895,7 @@
"gpt-4.1-mini": {
"cache_read_input_token_cost": 1e-07,
"cache_read_input_token_cost_priority": 1.75e-07,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 4e-07,
"input_cost_per_token_batches": 2e-07,
"input_cost_per_token_priority": 7e-07,
@ -35053,6 +35055,7 @@
"gpt-4o": {
"cache_read_input_token_cost": 1.25e-06,
"cache_read_input_token_cost_priority": 2.125e-06,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 2.5e-06,
"input_cost_per_token_batches": 1.25e-06,
"input_cost_per_token_priority": 4.25e-06,
@ -35397,6 +35400,7 @@
"gpt-4o-mini": {
"cache_read_input_token_cost": 7.5e-08,
"cache_read_input_token_cost_priority": 1.25e-07,
"deprecation_date": "2027-01-14",
"input_cost_per_token": 1.5e-07,
"input_cost_per_token_batches": 7.5e-08,
"input_cost_per_token_priority": 2.5e-07,

View file

@ -3064,6 +3064,38 @@ def test_map_optional_params_tool_choice_chat_nested_to_responses_api():
{"type": "custom", "name": "ApplyPatch"},
),
({"type": "custom"}, {"type": "custom"}),
(
{
"type": "allowed_tools",
"allowed_tools": {
"mode": "required",
"tools": [
{"type": "function", "function": {"name": "get_time"}},
{"type": "custom", "custom": {"name": "apply_patch"}},
{"type": "mcp", "server_label": "docs"},
{"type": "function", "name": "already_flat"},
],
},
},
{
"type": "allowed_tools",
"mode": "required",
"tools": [
{"type": "function", "name": "get_time"},
{"type": "custom", "name": "apply_patch"},
{"type": "mcp", "server_label": "docs"},
{"type": "function", "name": "already_flat"},
],
},
),
(
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "function"}]}},
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "function"}]}},
),
(
{"type": "allowed_tools", "mode": "auto", "tools": [{"type": "function", "name": "get_time"}]},
{"type": "allowed_tools", "mode": "auto", "tools": [{"type": "function", "name": "get_time"}]},
),
],
)
def test_normalize_tool_choice_for_responses_api(tool_choice, expected):
@ -4105,6 +4137,16 @@ def test_output_item_done_with_stream_map_keeps_empty_delta():
({"type": "required"}, "required"),
("auto", "auto"),
({"type": "function", "function": {"name": "get_weather"}}, {"type": "function", "name": "get_weather"}),
(
{
"type": "allowed_tools",
"allowed_tools": {
"mode": "required",
"tools": [{"type": "function", "function": {"name": "get_weather"}}],
},
},
{"type": "allowed_tools", "mode": "required", "tools": [{"type": "function", "name": "get_weather"}]},
),
],
)
async def test_acompletion_bridge_normalizes_tool_choice_on_the_wire(
@ -4177,6 +4219,7 @@ async def test_acompletion_bridge_normalizes_tool_choice_on_the_wire(
post_kwargs = mock_post.call_args.kwargs
request_body = post_kwargs["json"] if "json" in post_kwargs else json.loads(post_kwargs["data"])
assert request_body["tool_choice"] == expected_wire_tool_choice
assert [tool["name"] for tool in request_body["tools"]] == ["get_weather"]
def _make_incomplete_responses_api_response(

View file

@ -0,0 +1,100 @@
import copy
from collections.abc import Mapping
from typing import Final
import pytest
from litellm.litellm_core_utils.allowed_tools_tool_choice import (
ChatAllowedToolsToolChoice,
is_allowed_tools_tool_choice,
parse_chat_allowed_tools_tool_choice,
responses_allowed_tools_tool_choice,
)
_CHAT_LAYOUT: Final[dict[str, object]] = {
"type": "allowed_tools",
"allowed_tools": {
"mode": "auto",
"tools": [
{"type": "function", "function": {"name": "get_time"}},
{"type": "custom", "custom": {"name": "apply_patch", "x-vendor": 1}, "x-trace": "a"},
{"type": "mcp", "server_label": "docs", "server_url": "https://docs.example"},
{"type": "function", "name": "already_flat"},
],
},
}
def _parsed(tool_choice: Mapping[str, object]) -> ChatAllowedToolsToolChoice:
parsed: Final = parse_chat_allowed_tools_tool_choice(tool_choice)
assert parsed is not None
return parsed
def test_responses_layout_lifts_mode_and_tools_and_flattens_names_in_order():
assert responses_allowed_tools_tool_choice(_parsed(_CHAT_LAYOUT)) == {
"type": "allowed_tools",
"mode": "auto",
"tools": [
{"type": "function", "name": "get_time"},
{"type": "custom", "name": "apply_patch", "x-vendor": 1, "x-trace": "a"},
{"type": "mcp", "server_label": "docs", "server_url": "https://docs.example"},
{"type": "function", "name": "already_flat"},
],
}
def test_responses_layout_leaves_the_caller_dict_untouched():
original: Final = copy.deepcopy(_CHAT_LAYOUT)
flat: Final = responses_allowed_tools_tool_choice(_parsed(_CHAT_LAYOUT))
assert flat is not None
next(iter(flat["tools"]))["name"] = "mutated"
assert _CHAT_LAYOUT == original
@pytest.mark.parametrize(
"tool",
[
{"type": "function"},
{"type": "function", "name": ""},
{"type": "function", "function": {}},
{"type": "function", "function": "get_time"},
{"type": "custom", "custom": {"name": ""}},
{"type": "function", "name": "get_time", "function": {"name": "get_weather"}},
{"type": "function", "function": {"name": "get_time", "type": "custom"}},
],
)
def test_responses_layout_refuses_to_rewrite_a_malformed_or_conflicting_entry(tool: dict[str, object]):
tool_choice: Final[dict[str, object]] = {
"type": "allowed_tools",
"allowed_tools": {"mode": "required", "tools": [tool]},
}
assert responses_allowed_tools_tool_choice(_parsed(tool_choice)) is None
@pytest.mark.parametrize(
"tool_choice",
[
{"type": "allowed_tools", "mode": "auto", "tools": [{"type": "function", "name": "get_time"}]},
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": []}},
{"type": "allowed_tools", "allowed_tools": {"mode": "any", "tools": [{"type": "function"}]}},
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "function"}]}, "extra": 1},
{"type": "function", "function": {"name": "get_time"}},
],
)
def test_parse_rejects_anything_but_the_chat_layout(tool_choice: dict[str, object]):
assert parse_chat_allowed_tools_tool_choice(tool_choice) is None
@pytest.mark.parametrize(
("tool_choice", "expected"),
[
(_CHAT_LAYOUT, True),
({"type": "allowed_tools"}, True),
({"type": "function"}, False),
("auto", False),
(None, False),
],
)
def test_is_allowed_tools_tool_choice(tool_choice: Mapping[str, object] | str | None, expected: bool):
assert is_allowed_tools_tool_choice(tool_choice) is expected

View file

@ -75,6 +75,42 @@ def test_map_openai_params_with_preview_api_version():
)
def _map_tool_choice_on_pre_tool_choice_api_version(tool_choice: dict[str, object]) -> dict[str, object]:
return AzureOpenAIConfig().map_openai_params(
{"tool_choice": tool_choice, "tools": [{"type": "function", "function": {"name": "get_time"}}]},
{},
"gpt-4o",
drop_params=True,
api_version="2023-07-01-preview",
)
def test_map_openai_params_drop_params_still_drops_a_named_tool_choice_on_old_api_version():
mapped: Final = _map_tool_choice_on_pre_tool_choice_api_version(
{"type": "function", "function": {"name": "get_time"}}
)
assert "tool_choice" not in mapped
def test_map_openai_params_drop_params_never_drops_allowed_tools_on_old_api_version():
with pytest.raises(litellm.UnsupportedParamsError, match="allowed_tools") as exc:
_map_tool_choice_on_pre_tool_choice_api_version(
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": [{"type": "function"}]}}
)
assert exc.value.status_code == 400
def test_map_openai_params_forwards_allowed_tools_on_current_api_version():
tool_choice: Final[dict[str, object]] = {
"type": "allowed_tools",
"allowed_tools": {"mode": "required", "tools": [{"type": "function", "function": {"name": "get_time"}}]},
}
mapped: Final = AzureOpenAIConfig().map_openai_params(
{"tool_choice": tool_choice}, {}, "gpt-4o", drop_params=True, api_version="2025-04-01-preview"
)
assert mapped["tool_choice"] == tool_choice
def test_transform_request_hoists_tool_message_image():
"""Azure builds its request via convert_to_azure_openai_messages without the
OpenAIGPTConfig._transform_messages pipeline, so transform_request must hoist

View file

@ -5,14 +5,17 @@ Slack integration — so they can run without the full proxy stack.
"""
import logging
from collections.abc import Mapping
from datetime import date, datetime, timezone
from typing import Final
from unittest.mock import MagicMock
import pytest
import litellm
from litellm import Router
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.get_model_cost_map import AliasedCostEntry
from litellm.proxy.common_utils.model_deprecation import (
_classify,
_parse_deprecation_date,
@ -37,12 +40,7 @@ class TestParseDeprecationDate:
def test_should_return_none_for_documentation_sentinel(self):
# The JSON map ships a sentinel string under the "sample_spec" key.
assert (
_parse_deprecation_date(
"date when the model becomes deprecated in the format YYYY-MM-DD"
)
is None
)
assert _parse_deprecation_date("date when the model becomes deprecated in the format YYYY-MM-DD") is None
def test_should_return_none_for_none(self):
assert _parse_deprecation_date(None) is None
@ -51,9 +49,7 @@ class TestParseDeprecationDate:
assert _parse_deprecation_date(12345) is None
def test_should_narrow_datetime_to_date(self):
assert _parse_deprecation_date(
datetime(2026, 12, 31, 23, 59, tzinfo=timezone.utc)
) == date(2026, 12, 31)
assert _parse_deprecation_date(datetime(2026, 12, 31, 23, 59, tzinfo=timezone.utc)) == date(2026, 12, 31)
class TestClassify:
@ -134,9 +130,7 @@ class TestCollectModelDeprecations:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=today
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=today)
assert [m.model_name for m in snapshot.deprecated] == ["deprecated-alias"]
assert [m.model_name for m in snapshot.imminent] == ["imminent-alias"]
@ -166,9 +160,7 @@ class TestCollectModelDeprecations:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=today
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=today)
assert len(snapshot.imminent) == 1
assert snapshot.imminent[0].deprecation_date == date(2026, 6, 10)
@ -195,9 +187,7 @@ class TestCollectModelDeprecations:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=today
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=today)
assert len(snapshot.imminent) == 1
@ -217,9 +207,7 @@ class TestCollectModelDeprecations:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=date(2026, 6, 1)
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=date(2026, 6, 1))
assert [m.litellm_model for m in snapshot.imminent] == ["gpt-4o"]
@ -244,9 +232,7 @@ class TestCollectModelDeprecations:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=date(2026, 6, 1)
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=date(2026, 6, 1))
assert len(snapshot.imminent) == 1
assert len(snapshot.upcoming) == 1
@ -268,14 +254,136 @@ class TestCollectModelDeprecations:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=today
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=today)
assert len(snapshot.imminent) == 1
assert snapshot.imminent[0].litellm_model == "base-thing"
class TestProviderScopedResolution:
CATALOG = {
"gpt-4o": {"deprecation_date": "2027-01-14", "litellm_provider": "openai"},
"azure/gpt-4o": {"deprecation_date": "2027-04-14", "litellm_provider": "azure"},
"github_copilot/gpt-4o": {"litellm_provider": "github_copilot"},
"azure_ai/gpt-4.1": AliasedCostEntry(
{"deprecation_date": "2027-05-01", "litellm_provider": "azure_ai", "mode": "chat"}, ()
),
"custom-base": {"deprecation_date": "2026-12-01"},
"gpt-4.1": {"deprecation_date": "2027-01-14", "litellm_provider": "openai"},
"azure/gpt-4.1": {"deprecation_date": "2027-04-14", "litellm_provider": "azure"},
"gpt-4o-2024-05-13": {"deprecation_date": "2026-10-23", "litellm_provider": "openai"},
"azure/gpt-4o-2024-05-13": {"deprecation_date": "2026-12-09", "litellm_provider": "azure"},
"azure/aliased-base": AliasedCostEntry(
{"deprecation_date": "2026-11-20"}, ("azure/aliased-base", "azure/aliased-base-v1")
),
"aliased-base": {"deprecation_date": "2027-02-01"},
}
COST_MAP = {
**CATALOG,
"azure_ai/gpt-4o": AliasedCostEntry(
{"deprecation_date": "2027-01-14", "litellm_provider": "azure", "mode": "chat"}, ()
),
"registered-base": AliasedCostEntry({"deprecation_date": "2026-12-15"}, (), frozenset({"deprecation_date"})),
}
@pytest.mark.parametrize(
("litellm_params", "model_info", "expected"),
[
({"model": "gpt-4o"}, {}, ("gpt-4o", date(2027, 1, 14), "openai")),
({"model": "openai/gpt-4o"}, {}, ("gpt-4o", date(2027, 1, 14), "openai")),
(
{"model": "gpt-4o", "custom_llm_provider": "azure"},
{},
("azure/gpt-4o", date(2027, 4, 14), "azure"),
),
({"model": "azure/gpt-4o"}, {}, ("azure/gpt-4o", date(2027, 4, 14), "azure")),
(
{"model": "azure/prod-chat"},
{"base_model": "gpt-4o"},
("azure/gpt-4o", date(2027, 4, 14), "azure"),
),
(
{"model": "gpt-4o", "custom_llm_provider": "github_copilot"},
{},
("gpt-4o", date(2027, 1, 14), "openai"),
),
({"model": "azure_ai/gpt-4o"}, {}, ("azure/gpt-4o", date(2027, 4, 14), "azure")),
(
{"model": "azure_ai/prod-chat"},
{"base_model": "gpt-4o"},
("azure/gpt-4o", date(2027, 4, 14), "azure"),
),
(
{"model": "azure_ai/gpt-4o"},
{"base_model": "azure_ai/gpt-4o"},
("azure/gpt-4o", date(2027, 4, 14), "azure"),
),
(
{"model": "azure_ai/prod-chat"},
{"base_model": "azure_ai/gpt-4o"},
("azure/gpt-4o", date(2027, 4, 14), "azure"),
),
({"model": "azure_ai/gpt-4.1"}, {}, ("azure_ai/gpt-4.1", date(2027, 5, 1), "azure_ai")),
({"model": "azure/gpt-4o"}, {"base_model": "custom-base"}, ("custom-base", date(2026, 12, 1), None)),
(
{"model": "azure/gpt-4o"},
{"base_model": "registered-base"},
("registered-base", date(2026, 12, 15), None),
),
(
{"model": "azure/prod-chat"},
{"base_model": "aliased-base"},
("azure/aliased-base", date(2026, 11, 20), None),
),
(
{"model": "openai/gpt-4o-2024-05-13"},
{"base_model": "gpt-4o"},
("gpt-4o-2024-05-13", date(2026, 10, 23), "openai"),
),
(
{"model": "azure/gpt-4o-2024-05-13"},
{"base_model": "gpt-4o"},
("azure/gpt-4o-2024-05-13", date(2026, 12, 9), "azure"),
),
],
ids=[
"bare",
"openai-prefix",
"declared-azure",
"azure-prefix",
"azure-base-model",
"undated-own-row",
"azure-ai-reads-azure",
"azure-ai-base-model-reads-azure",
"azure-ai-copied-base-model-yields-to-azure",
"azure-ai-prefixed-base-model-on-custom-deployment-reads-azure",
"azure-ai-own-row-wins",
"custom-base-model-row-wins",
"registered-base-model-row-wins",
"aliased-base-model-row-wins",
"openai-snapshot-shuts-down-before-its-base-model",
"azure-snapshot-shuts-down-before-its-base-model",
],
)
def test_should_prefer_the_deployments_own_provider_date(
self,
monkeypatch: pytest.MonkeyPatch,
litellm_params: Mapping[str, str],
model_info: Mapping[str, str],
expected: tuple[str, date, str | None],
) -> None:
monkeypatch.setattr(litellm, "model_cost", self.COST_MAP)
router: Final = _make_router(
[{"model_name": "alias", "litellm_params": litellm_params, "model_info": {"id": "1", **model_info}}]
)
snapshot: Final = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=date(2026, 6, 1), loaded_catalog=lambda: self.CATALOG
)
assert [(m.litellm_model, m.deprecation_date, m.litellm_provider) for m in snapshot.upcoming] == [expected]
class TestFormatDeprecationAlertMessage:
def test_should_return_none_when_nothing_to_alert(self):
snapshot = collect_model_deprecations(llm_router=None)
@ -321,9 +429,7 @@ class TestFormatDeprecationAlertMessage:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=today
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=today)
message = format_deprecation_alert_message(snapshot)
assert message is not None
@ -350,9 +456,7 @@ class TestFormatDeprecationAlertMessage:
]
)
snapshot = collect_model_deprecations(
llm_router=router, warn_within_days=30, today=today
)
snapshot = collect_model_deprecations(llm_router=router, warn_within_days=30, today=today)
message = format_deprecation_alert_message(snapshot)
assert message is not None

View file

@ -553,6 +553,63 @@ def test_completion_strips_eager_input_streaming_before_openai(respx_mock: respx
assert sent_tool["function"]["name"] == "write_file"
_ALLOWED_TOOLS_TOOL_CHOICE: Final = {
"type": "allowed_tools",
"allowed_tools": {"mode": "required", "tools": [{"type": "function", "function": {"name": "get_time"}}]},
}
_TWO_TOOLS: Final = [
{"type": "function", "function": {"name": "get_time", "parameters": {"type": "object", "properties": {}}}},
{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {}}}},
]
@pytest.mark.parametrize("model", ["openai/gpt-5.5", "azure/my-gpt-deployment"])
def test_completion_forwards_allowed_tools_tool_choice_to_openai_compatible_chat(
respx_mock: respx.MockRouter, openai_api_response: dict[str, object], model: str
):
mock_route: Final = respx_mock.post(url__regex=r"http://localhost:12346/.*chat/completions.*").mock(
return_value=httpx.Response(status_code=200, json=openai_api_response)
)
litellm.completion(
model=model,
messages=[{"role": "user", "content": "What time is it?"}],
tools=_TWO_TOOLS,
tool_choice=_ALLOWED_TOOLS_TOOL_CHOICE,
api_base="http://localhost:12346/v1",
api_key="fake_api_key",
api_version="2025-04-01-preview",
)
assert mock_route.called
sent: Final = json.loads(respx_mock.calls[0].request.content)
assert sent["tool_choice"] == _ALLOWED_TOOLS_TOOL_CHOICE
assert [tool["function"]["name"] for tool in sent["tools"]] == ["get_time", "get_weather"]
@pytest.mark.parametrize("drop_params", [False, True])
@pytest.mark.parametrize("model", ["anthropic/claude-sonnet-4-5", "gemini/gemini-2.5-flash", "bedrock/converse/x"])
def test_completion_allowed_tools_tool_choice_is_a_400_where_the_provider_cannot_enforce_it(
respx_mock: respx.MockRouter, model: str, drop_params: bool
):
with pytest.raises(
litellm.BadRequestError, match=r"allowed_tools.*only supported for azure, litellm_proxy, openai"
) as exc:
litellm.completion(
model=model,
messages=[{"role": "user", "content": "What time is it?"}],
tools=_TWO_TOOLS,
tool_choice=_ALLOWED_TOOLS_TOOL_CHOICE,
drop_params=drop_params,
api_key="fake_api_key",
aws_access_key_id="fake",
aws_secret_access_key="fake",
aws_region_name="us-east-1",
)
assert exc.value.status_code == 400
assert not respx_mock.calls
_CLAUDE_TOOL_SEARCH_CALL: Final = {
"id": "srvtoolu_01search",
"type": "function",

View file

@ -388,6 +388,34 @@ def test_vertex_gemini_flash_rows_carry_the_documented_retirement_date(prices: d
assert prices[f"{prefix}{model}"]["deprecation_date"] == VERTEX_GEMINI_FLASH_RETIREMENT_DATES[model]
# https://developers.openai.com/api/docs/deprecations, the 2026-10-14 GPT-4 series notice
OPENAI_GPT_4_SERIES_SNAPSHOTS: Final = MappingProxyType(
{
"gpt-4o": ("gpt-4o-2024-08-06", "gpt-4o-2024-11-20"),
"gpt-4o-mini": ("gpt-4o-mini-2024-07-18",),
"gpt-4.1": ("gpt-4.1-2025-04-14",),
"gpt-4.1-mini": ("gpt-4.1-mini-2025-04-14",),
}
)
class _ShutdownRow(BaseModel):
model_config = ConfigDict(frozen=True, extra="ignore")
deprecation_date: str | None = None
@pytest.mark.parametrize("path", (PRICES_PATH, BACKUP_PRICES_PATH), ids=("main", "backup"))
@pytest.mark.parametrize("alias", sorted(OPENAI_GPT_4_SERIES_SNAPSHOTS))
def test_openai_gpt_4_series_aliases_retire_with_their_snapshots(path: Path, alias: str) -> None:
prices: Final[dict[str, JsonValue]] = TypeAdapter(dict[str, JsonValue]).validate_json(path.read_bytes())
snapshot_dates: Final = frozenset(
_ShutdownRow.model_validate(prices[snapshot]).deprecation_date
for snapshot in OPENAI_GPT_4_SERIES_SNAPSHOTS[alias]
)
assert snapshot_dates == {_ShutdownRow.model_validate(prices[alias]).deprecation_date}
def _sampling_flags_across_provider_twins(prices: Mapping[str, Mapping[str, object]], key: str) -> frozenset[object]:
bare: Final = key.split("/")[-1]
twins: Final = tuple(twin for twin in (bare, f"vertex_ai/{bare}", f"gemini/{bare}") if twin in prices)

View file

@ -8651,6 +8651,40 @@ def test_validate_tool_choice_invalid_dict_is_a_400(tool_choice):
assert exc_info.value.model == MODEL
@pytest.mark.usefixtures("_vcr_outcome_gate", "setup_and_teardown")
@pytest.mark.parametrize("mode", ["auto", "required"])
def test_validate_tool_choice_allowed_tools_chat_layout_passes_through(mode: str):
tool_choice: Final = {
"type": "allowed_tools",
"allowed_tools": {
"mode": mode,
"tools": [{"type": "function", "function": {"name": "get_time"}}, {"type": "mcp", "server_label": "docs"}],
},
}
assert validate_chat_completion_tool_choice(tool_choice, model=MODEL) is tool_choice
@pytest.mark.usefixtures("_vcr_outcome_gate", "setup_and_teardown")
@pytest.mark.parametrize(
"tool_choice",
[
{"type": "allowed_tools", "mode": "required", "tools": [{"type": "function", "name": "get_time"}]},
{"type": "allowed_tools"},
{"type": "allowed_tools", "allowed_tools": {"mode": "required"}},
{"type": "allowed_tools", "allowed_tools": {"mode": "required", "tools": []}},
{"type": "allowed_tools", "allowed_tools": {"mode": "none", "tools": [{"type": "function"}]}},
{"type": "allowed_tools", "allowed_tools": {"tools": [{"type": "function", "function": {"name": "a"}}]}},
{"type": "allowed_tools", "allowed_tools": {"mode": "auto", "tools": "get_time"}},
],
)
def test_validate_tool_choice_allowed_tools_malformed_is_a_400(tool_choice: dict[str, object]):
with pytest.raises(litellm.BadRequestError, match="allowed_tools") as exc_info:
validate_chat_completion_tool_choice(tool_choice, model=MODEL)
assert exc_info.value.status_code == 400
assert exc_info.value.model == MODEL
assert '"mode": "auto" | "required"' in str(exc_info.value)
@pytest.mark.usefixtures("_vcr_outcome_gate", "setup_and_teardown")
@pytest.mark.parametrize("tool_choice", [123, []])
def test_validate_tool_choice_invalid_type_is_a_400(tool_choice):