This commit is contained in:
Mateo Wang 2026-10-27 16:03:25 -07:00 • committed by GitHub
commit e434a72cc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 73 additions and 54 deletions

View file

@ -162,8 +162,8 @@ COPY --from=builder /app/docker /app/docker
COPY --from=builder /app/schema.prisma /app/schema.prisma
COPY --from=builder /app/litellm/proxy/prisma_migration.py /app/litellm/proxy/prisma_migration.py
# enterprise/ is imported by source path at runtime (proxy_cli puts the
# working directory on sys.path; litellm/proxy/hooks resolves
# enterprise.enterprise_hooks from it)
# working directory on sys.path; the moderation and blocked user list
# callbacks import enterprise.enterprise_hooks from it)
COPY --from=builder /app/enterprise /app/enterprise
COPY --from=builder /app/litellm-proxy-extras /app/litellm-proxy-extras
# Prisma CLI + engines are baked under /opt/prisma, a fixed path every

View file

@ -142,8 +142,8 @@ COPY --from=builder /app/docker /app/docker
COPY --from=builder /app/schema.prisma /app/schema.prisma
COPY --from=builder /app/litellm/proxy/prisma_migration.py /app/litellm/proxy/prisma_migration.py
# enterprise/ is imported by source path at runtime (proxy_cli puts the
# working directory on sys.path; litellm/proxy/hooks resolves
# enterprise.enterprise_hooks from it)
# working directory on sys.path; the moderation and blocked user list
# callbacks import enterprise.enterprise_hooks from it)
COPY --from=builder /app/enterprise /app/enterprise
COPY --from=builder /app/litellm-proxy-extras /app/litellm-proxy-extras
# Prisma CLI + engines are baked under /opt/prisma, a fixed path every

View file

@ -148,8 +148,8 @@ COPY --from=builder /app/docker /app/docker
COPY --from=builder /app/schema.prisma /app/schema.prisma
COPY --from=builder /app/litellm/proxy/prisma_migration.py /app/litellm/proxy/prisma_migration.py
# enterprise/ is imported by source path at runtime (proxy_cli puts the
# working directory on sys.path; litellm/proxy/hooks resolves
# enterprise.enterprise_hooks from it)
# working directory on sys.path; the moderation and blocked user list
# callbacks import enterprise.enterprise_hooks from it)
COPY --from=builder /app/enterprise /app/enterprise
COPY --from=builder /app/litellm-proxy-extras /app/litellm-proxy-extras
# Prisma CLI + engines are baked under /opt/prisma, a fixed path every runtime

View file

@ -1,33 +0,0 @@
from typing import Dict, Literal, Type, Union
from litellm_enterprise.proxy.hooks.managed_files import PROXY_LiteLLMManagedFiles
from litellm_enterprise.proxy.hooks.managed_vector_stores import (
PROXY_LiteLLMManagedVectorStores,
)
from litellm.integrations.custom_logger import CustomLogger
ENTERPRISE_PROXY_HOOKS: Dict[str, Type[CustomLogger]] = {
"managed_files": PROXY_LiteLLMManagedFiles,
"managed_vector_stores": PROXY_LiteLLMManagedVectorStores,
}
def get_enterprise_proxy_hook(
hook_name: Union[
Literal[
"managed_files",
"managed_vector_stores",
"max_parallel_requests",
],
str,
],
):
"""
Factory method to get a enterprise hook instance by name
"""
if hook_name not in ENTERPRISE_PROXY_HOOKS:
raise ValueError(
f"Unknown hook: {hook_name}. Available hooks: {list(ENTERPRISE_PROXY_HOOKS.keys())}"
)
return ENTERPRISE_PROXY_HOOKS[hook_name]

View file

@ -1,6 +1,9 @@
import os
from typing import Final, Literal
from litellm._logging import verbose_proxy_logger
from litellm.integrations.custom_logger import CustomLogger
from . import *
from .autorouter_baseline_cache import AutoRouterBaselineCache
from .cache_control_check import ( # noqa: F401 # backwards-compatible package export
@ -32,10 +35,10 @@ from .sensitive_data_routing import ( # noqa: F401 # backwards-compatible pack
)
# List of all available hooks that can be enabled.
# Defined before the enterprise import below so that any module re-imported
# transitively through `enterprise.enterprise_hooks` can resolve `PROXY_HOOKS`
# Defined before the litellm_enterprise hook imports below so that any module re-imported
# transitively through them can resolve `PROXY_HOOKS`
# and `get_proxy_hook` from this partially-initialized module without circling.
PROXY_HOOKS: Final = {
PROXY_HOOKS: Final[dict[str, type[CustomLogger]]] = { # mutable-ok: shared with cycle importers, extended below
"parallel_request_limiter": PROXY_MaxParallelRequestsHandler_v3,
"cache_control_check": PROXY_CacheControlCheck,
"responses_id_security": ResponsesIDSecurity,
@ -63,14 +66,14 @@ def get_proxy_hook(
return PROXY_HOOKS[hook_name]
### CHECK IF ENTERPRISE HOOKS ARE AVAILABLE ####
try:
from enterprise.enterprise_hooks import ENTERPRISE_PROXY_HOOKS
except ImportError:
ENTERPRISE_PROXY_HOOKS = {}
### update PROXY_HOOKS with ENTERPRISE_PROXY_HOOKS ###
PROXY_HOOKS.update(ENTERPRISE_PROXY_HOOKS)
from litellm_enterprise.proxy.hooks.managed_files import PROXY_LiteLLMManagedFiles
from litellm_enterprise.proxy.hooks.managed_vector_stores import PROXY_LiteLLMManagedVectorStores
except ImportError as litellm_enterprise_import_error:
verbose_proxy_logger.warning(
"Managed files and managed vector stores are disabled because litellm_enterprise failed to import: %s",
litellm_enterprise_import_error,
)
else:
PROXY_HOOKS["managed_files"] = PROXY_LiteLLMManagedFiles
PROXY_HOOKS["managed_vector_stores"] = PROXY_LiteLLMManagedVectorStores

View file

@ -137,7 +137,9 @@ class AutoRouterBaselineCache(CustomLogger):
return
try:
raw_metadata: Final = kwargs.get(get_metadata_variable_name_from_kwargs(kwargs))
metadata: Final = _METADATA.validate_python(raw_metadata) if isinstance(raw_metadata, Mapping) else {}
metadata: Final[Mapping[str, object]] = (
_METADATA.validate_python(raw_metadata) if isinstance(raw_metadata, Mapping) else {}
)
if metadata.get(INTERNAL_CALL_ORIGIN_METADATA_KEY):
return
if logging_obj.baseline_cache_context is not None:
@ -157,7 +159,7 @@ class AutoRouterBaselineCache(CustomLogger):
prices: Final = _PRICES.validate_python(
_effective_model_info(router, request.route.baseline_deployment_id, request.route.baseline_model)
)
params: Final = (
params: Final[Mapping[str, object]] = (
_METADATA.validate_python(deployment.litellm_params.model_dump(mode="json")) if deployment else {}
)
projected: Final = (

View file

@ -14,8 +14,15 @@ silently swallowed the ImportError in `hooks/__init__.py`, leaving
``managed_files`` unregistered and the /files endpoint returning 500.
"""
import importlib
import logging
import sys
from typing import Final
import pytest
import litellm.proxy
from litellm._logging import verbose_proxy_logger
from litellm.proxy.hooks import PROXY_HOOKS, get_proxy_hook
@ -49,3 +56,43 @@ def test_isolation_module_does_not_pull_in_proxy_utils():
importlib.import_module("litellm.llms.base_llm.managed_resources.isolation")
assert "litellm.proxy.utils" not in sys.modules
assert "litellm.proxy.management_endpoints.common_utils" not in sys.modules
def test_managed_hooks_register_from_litellm_enterprise_without_the_repo_only_enterprise_package(
monkeypatch: pytest.MonkeyPatch,
):
pytest.importorskip("litellm_enterprise")
from litellm_enterprise.proxy.hooks.managed_files import PROXY_LiteLLMManagedFiles
from litellm_enterprise.proxy.hooks.managed_vector_stores import PROXY_LiteLLMManagedVectorStores
monkeypatch.setitem(sys.modules, "enterprise", None)
monkeypatch.setitem(sys.modules, "enterprise.enterprise_hooks", None)
monkeypatch.delitem(sys.modules, "litellm.proxy.hooks")
monkeypatch.delattr(litellm.proxy, "hooks")
wheel_install_hooks: Final = importlib.import_module("litellm.proxy.hooks")
assert wheel_install_hooks.get_proxy_hook("managed_files") is PROXY_LiteLLMManagedFiles
assert wheel_install_hooks.get_proxy_hook("managed_vector_stores") is PROXY_LiteLLMManagedVectorStores
def test_proxy_hooks_still_import_and_warn_when_litellm_enterprise_cannot_load(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
):
monkeypatch.setitem(sys.modules, "litellm_enterprise.proxy.hooks.managed_files", None)
monkeypatch.delitem(sys.modules, "litellm.proxy.hooks")
monkeypatch.delattr(litellm.proxy, "hooks")
verbose_proxy_logger.addHandler(caplog.handler)
try:
with caplog.at_level(logging.WARNING, logger=verbose_proxy_logger.name):
hooks_without_enterprise: Final = importlib.import_module("litellm.proxy.hooks")
finally:
verbose_proxy_logger.removeHandler(caplog.handler)
assert "managed_files" not in hooks_without_enterprise.PROXY_HOOKS
assert "managed_vector_stores" not in hooks_without_enterprise.PROXY_HOOKS
assert "parallel_request_limiter" in hooks_without_enterprise.PROXY_HOOKS
assert any(
"Managed files and managed vector stores are disabled" in record.getMessage() for record in caplog.records
)