mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-20 10:02:54 +00:00
Merge d7e32a21db into dc0237ccd7
This commit is contained in:
commit
5cfdc615cc
8 changed files with 153 additions and 6 deletions
|
|
@ -1438,6 +1438,7 @@ from .exceptions import (
|
|||
Timeout,
|
||||
APIConnectionError,
|
||||
UnsupportedParamsError,
|
||||
UnsupportedEndpointError as UnsupportedEndpointError,
|
||||
APIResponseValidationError,
|
||||
UnprocessableEntityError,
|
||||
InternalServerError,
|
||||
|
|
|
|||
|
|
@ -317,6 +317,10 @@ class MalformedRequestFieldError(BadRequestError):
|
|||
pass
|
||||
|
||||
|
||||
class UnsupportedEndpointError(BadRequestError):
|
||||
pass
|
||||
|
||||
|
||||
class UnprocessableEntityError(openai.UnprocessableEntityError):
|
||||
def __init__(
|
||||
self,
|
||||
|
|
@ -1054,6 +1058,7 @@ LITELLM_EXCEPTION_TYPES: Final = [
|
|||
BadRequestError,
|
||||
UnprocessableEntityError,
|
||||
UnsupportedParamsError,
|
||||
UnsupportedEndpointError,
|
||||
Timeout,
|
||||
PermissionDeniedError,
|
||||
RateLimitError,
|
||||
|
|
|
|||
|
|
@ -8335,7 +8335,7 @@ def transcription(
|
|||
custom_llm_provider=custom_llm_provider,
|
||||
)
|
||||
|
||||
response: TranscriptionResponse | Coroutine[object, object, TranscriptionResponse] | None = None
|
||||
response: TranscriptionResponse | Coroutine[object, object, TranscriptionResponse]
|
||||
|
||||
provider_config: Final = ProviderConfigManager.get_provider_audio_transcription_config(
|
||||
model=model,
|
||||
|
|
@ -8497,10 +8497,16 @@ def transcription(
|
|||
provider_config=provider_config,
|
||||
shared_session=shared_session,
|
||||
)
|
||||
else:
|
||||
raise litellm.UnsupportedEndpointError(
|
||||
message=f"{custom_llm_provider}/{model} does not support audio transcription.",
|
||||
model=model,
|
||||
llm_provider=custom_llm_provider,
|
||||
)
|
||||
|
||||
# Store duration in _hidden_params for cost calculation without
|
||||
# exposing it in the response body (see sync path comment above).
|
||||
if response is not None and not isinstance(response, Coroutine):
|
||||
if not isinstance(response, Coroutine):
|
||||
existing_duration: Final = getattr(response, "duration", None)
|
||||
if existing_duration is None:
|
||||
calculated_duration: Final = calculate_request_duration(file)
|
||||
|
|
@ -8510,8 +8516,6 @@ def transcription(
|
|||
)
|
||||
response_hidden_params["audio_transcription_duration"] = calculated_duration
|
||||
|
||||
if response is None:
|
||||
raise ValueError("Unmapped provider passed in. Unable to get the response.")
|
||||
return response
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -612,6 +612,7 @@ _DEPLOYMENT_PARAMS_ADAPTER: Final[TypeAdapter[Mapping[str, object] | GenericLite
|
|||
_OPAQUE_VALUE_ADAPTER: Final = TypeAdapter(object)
|
||||
_DEPLOYMENT_SELECTED_EVENT: Final = "litellm.request.deployment_selected"
|
||||
_CONTENT_BLOCKS_ADAPTER: Final = TypeAdapter(list[dict[str, object]])
|
||||
_DEPLOYMENTS_ADAPTER: Final = TypeAdapter(Sequence[Mapping[str, object]])
|
||||
|
||||
|
||||
def _extend_trailing_text_block(blocks: Sequence[Mapping[str, object]], text: str) -> Sequence[Mapping[str, object]]:
|
||||
|
|
@ -673,6 +674,12 @@ def _configured_model_info(deployment: Mapping[str, object]) -> Mapping[str, obj
|
|||
return _MODEL_INFO_ADAPTER.validate_python(deployment.get("model_info") or {})
|
||||
|
||||
|
||||
def _every_deployment_refused(
|
||||
deployments: Sequence[Mapping[str, object]], refused_deployment_ids: tuple[str, ...]
|
||||
) -> bool:
|
||||
return all(_configured_model_info(deployment).get("id") in refused_deployment_ids for deployment in deployments)
|
||||
|
||||
|
||||
def _as_retry_skipped_deployment_ids(value: object) -> tuple[str, ...]:
|
||||
return tuple(item for item in value if isinstance(item, str)) if isinstance(value, tuple) else ()
|
||||
|
||||
|
|
@ -8972,6 +8979,10 @@ class Router:
|
|||
)
|
||||
if first_skipped_ids:
|
||||
kwargs["_retry_skipped_deployment_ids"] = first_skipped_ids # rebind-ok: the next attempt reads it
|
||||
if isinstance(original_exception, litellm.UnsupportedEndpointError) and _every_deployment_refused(
|
||||
_DEPLOYMENTS_ADAPTER.validate_python(_healthy_deployments), first_skipped_ids
|
||||
):
|
||||
raise
|
||||
else:
|
||||
raise
|
||||
|
||||
|
|
@ -9051,6 +9062,10 @@ class Router:
|
|||
)
|
||||
if skipped_ids:
|
||||
kwargs["_retry_skipped_deployment_ids"] = skipped_ids # rebind-ok: the next attempt reads it
|
||||
if isinstance(e, litellm.UnsupportedEndpointError) and _every_deployment_refused(
|
||||
_DEPLOYMENTS_ADAPTER.validate_python(_healthy_deployments), skipped_ids
|
||||
):
|
||||
raise
|
||||
_timeout = self._time_to_sleep_before_retry(
|
||||
e=e,
|
||||
remaining_retries=remaining_retries,
|
||||
|
|
@ -9147,7 +9162,7 @@ class Router:
|
|||
status_code: Final = getattr(error, "status_code", None)
|
||||
if status_code is not None and not litellm.should_retry(status_code):
|
||||
# 401/403 are special cases - allow retry if multiple deployments exist (handled below)
|
||||
if status_code not in (401, 403):
|
||||
if status_code not in (401, 403) and not isinstance(error, litellm.UnsupportedEndpointError):
|
||||
raise error
|
||||
|
||||
if isinstance(error, litellm.NotFoundError):
|
||||
|
|
|
|||
|
|
@ -261,7 +261,10 @@ def is_bad_request_without_fallback(exception: Exception, fallback_on_unsupporte
|
|||
return False
|
||||
if isinstance(exception, litellm.UnsupportedParamsError):
|
||||
return True
|
||||
if isinstance(exception, (litellm.ContextWindowExceededError, litellm.ContentPolicyViolationError)):
|
||||
if isinstance(
|
||||
exception,
|
||||
(litellm.ContextWindowExceededError, litellm.ContentPolicyViolationError, litellm.UnsupportedEndpointError),
|
||||
):
|
||||
return False
|
||||
return exception.status_code == 400 and bool(getattr(exception, "llm_provider", ""))
|
||||
|
||||
|
|
|
|||
|
|
@ -95,6 +95,7 @@ ignored_function_names = [
|
|||
"arm_routing_read_prefetch", # Tested in tests/unit/caching/test_request_redis_batch_pre_call.py (file lacks "router" in name)
|
||||
"_deployment_params_base_model", # Tested through log_retry in test_router_helper_utils.py
|
||||
"_configured_model_info", # Tested through get_configured_service_tiers in test_router.py
|
||||
"_every_deployment_refused", # Tested through the unsupported transcription retry tests in test_router.py
|
||||
"_routable_deployments", # Tested through get_configured_service_tiers and get_routable_upstream_model in test_router.py
|
||||
"_async_get_available_deployment", # Body of the `route {model}` phase wrapper, exercised through async_get_available_deployment in test_router.py
|
||||
"_async_get_available_deployment_for_pass_through", # Same, through async_get_available_deployment_for_pass_through in test_router.py
|
||||
|
|
|
|||
|
|
@ -9779,6 +9779,37 @@ async def test_atranscription_answers_a_non_string_provider_hint_with_a_bad_requ
|
|||
assert not respx_mock.calls
|
||||
|
||||
|
||||
def test_transcription_rejects_an_unsupported_provider_without_an_http_call(respx_mock: respx.MockRouter) -> None:
|
||||
model: Final = "test-transcription-model"
|
||||
provider: Final = "anthropic"
|
||||
|
||||
with pytest.raises(litellm.UnsupportedEndpointError) as raised:
|
||||
litellm.transcription(model=f"{provider}/{model}", file=("speech.wav", b"RIFF-audio", "audio/wav"))
|
||||
|
||||
assert raised.value.status_code == 400
|
||||
assert raised.value.message == f"litellm.BadRequestError: {provider}/{model} does not support audio transcription."
|
||||
assert raised.value.model == model
|
||||
assert raised.value.llm_provider == provider
|
||||
assert not respx_mock.calls
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_atranscription_rejects_an_unsupported_provider_without_an_http_call(
|
||||
respx_mock: respx.MockRouter,
|
||||
) -> None:
|
||||
model: Final = "test-transcription-model"
|
||||
provider: Final = "anthropic"
|
||||
|
||||
with pytest.raises(litellm.UnsupportedEndpointError) as raised:
|
||||
await litellm.atranscription(model=f"{provider}/{model}", file=("speech.wav", b"RIFF-audio", "audio/wav"))
|
||||
|
||||
assert raised.value.status_code == 400
|
||||
assert raised.value.message == f"litellm.BadRequestError: {provider}/{model} does not support audio transcription."
|
||||
assert raised.value.model == model
|
||||
assert raised.value.llm_provider == provider
|
||||
assert not respx_mock.calls
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_aspeech_infers_the_provider_from_base_url(respx_mock: respx.MockRouter) -> None:
|
||||
base_url: Final = "https://api.groq.com/openai/v1"
|
||||
|
|
|
|||
|
|
@ -21429,6 +21429,93 @@ def test_router_retry_policy_400_moves_off_the_refusing_deployment_on_every_sync
|
|||
assert accepting.call_count == 1
|
||||
|
||||
|
||||
_TRANSCRIPTION_AUDIO: Final = ("speech.wav", b"RIFF", "audio/wav")
|
||||
_TRANSCRIPTION_API_BASE: Final = "https://transcriber.local/v1"
|
||||
|
||||
|
||||
def _transcription_deployment(model_name: str, deployment_id: str, litellm_model: str, weight: int) -> dict:
|
||||
return {
|
||||
"model_name": model_name,
|
||||
"litellm_params": {
|
||||
"model": litellm_model,
|
||||
"api_key": "sk-fake",
|
||||
"api_base": _TRANSCRIPTION_API_BASE,
|
||||
"weight": weight,
|
||||
},
|
||||
"model_info": {"id": deployment_id},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_retries_a_transcription_off_a_deployment_whose_provider_cannot_transcribe(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
||||
router: Final = litellm.Router(
|
||||
model_list=[
|
||||
_transcription_deployment("transcriber", "cannot-transcribe", "anthropic/claude-sonnet-5-5", weight=1),
|
||||
_transcription_deployment("transcriber", "can-transcribe", "openai/gpt-transcribe", weight=0),
|
||||
],
|
||||
num_retries=1,
|
||||
disable_cooldowns=True,
|
||||
)
|
||||
|
||||
with respx.mock as respx_mock:
|
||||
upstream: Final = respx_mock.post(f"{_TRANSCRIPTION_API_BASE}/audio/transcriptions").mock(
|
||||
return_value=httpx.Response(200, json={"text": "hi"})
|
||||
)
|
||||
response: Final = await router.atranscription(model="transcriber", file=_TRANSCRIPTION_AUDIO)
|
||||
|
||||
assert response.text == "hi"
|
||||
assert upstream.call_count == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("deployment_count", [1, 2, 3])
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_tries_each_deployment_that_cannot_transcribe_once_then_answers_400(deployment_count: int):
|
||||
router: Final = litellm.Router(
|
||||
model_list=[
|
||||
_transcription_deployment("transcriber", f"cannot-transcribe-{index}", "anthropic/claude-sonnet-5-5", 1)
|
||||
for index in range(deployment_count)
|
||||
],
|
||||
num_retries=5,
|
||||
disable_cooldowns=True,
|
||||
)
|
||||
|
||||
with respx.mock as respx_mock, pytest.raises(litellm.UnsupportedEndpointError) as raised:
|
||||
await router.atranscription(model="transcriber", file=_TRANSCRIPTION_AUDIO)
|
||||
|
||||
assert raised.value.status_code == 400
|
||||
assert router.fail_calls["transcriber"] == deployment_count
|
||||
assert not respx_mock.calls
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_falls_back_from_a_group_that_cannot_transcribe_when_unsupported_params_do_not(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
||||
router: Final = litellm.Router(
|
||||
model_list=[
|
||||
_transcription_deployment("chat-only", "cannot-transcribe", "anthropic/claude-sonnet-5-5", weight=1),
|
||||
_transcription_deployment("transcriber", "can-transcribe", "openai/gpt-transcribe", weight=1),
|
||||
],
|
||||
fallbacks=[{"chat-only": ["transcriber"]}],
|
||||
fallback_on_unsupported_params=False,
|
||||
num_retries=0,
|
||||
disable_cooldowns=True,
|
||||
)
|
||||
|
||||
with respx.mock as respx_mock:
|
||||
upstream: Final = respx_mock.post(f"{_TRANSCRIPTION_API_BASE}/audio/transcriptions").mock(
|
||||
return_value=httpx.Response(200, json={"text": "hi"})
|
||||
)
|
||||
response: Final = await router.atranscription(model="chat-only", file=_TRANSCRIPTION_AUDIO)
|
||||
|
||||
assert response.text == "hi"
|
||||
assert upstream.call_count == 1
|
||||
|
||||
|
||||
def _make_failure_logging_obj():
|
||||
return LiteLLMLogging(
|
||||
model="gpt-5.6",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue