import asyncio import copy import enum import gc import importlib import inspect import io import itertools import logging import os import random import re import secrets import shutil import socket import subprocess import sys import threading import time import traceback import warnings from collections.abc import ( AsyncGenerator, AsyncIterator, Awaitable, Callable, Collection, Container, Iterable, Mapping, MutableMapping, Sequence, ) from collections.abc import ( Set as AbstractSet, ) from dataclasses import dataclass, replace from datetime import datetime, timedelta, timezone from itertools import chain from types import MappingProxyType, TracebackType, UnionType from typing import ( TYPE_CHECKING, Annotated, Any, Final, Literal, NamedTuple, NoReturn, Optional, Protocol, TypeAlias, TypeVar, Union, cast, get_args, get_origin, get_type_hints, ) from urllib.parse import urlparse import anyio import anyio.lowlevel import websockets import websockets.exceptions from openai.types.audio import TranscriptionStreamEvent from pydantic import BaseModel, ConfigDict, Json, JsonValue, TypeAdapter, ValidationError, with_config from pydantic.fields import PydanticUndefined from pydantic.main import IncEx from pydantic_core import PydanticSerializationError, to_jsonable_python from typing_extensions import NotRequired, ReadOnly, TypedDict, assert_never from litellm._uuid import uuid from litellm.constants import ( AIOHTTP_CONNECTOR_LIMIT, AIOHTTP_CONNECTOR_LIMIT_PER_HOST, AIOHTTP_KEEPALIVE_TIMEOUT, AIOHTTP_NEEDS_CLEANUP_CLOSED, AIOHTTP_TTL_DNS_CACHE, AUDIO_SPEECH_CHUNK_SIZE, AWS_ERROR_TYPE_HEADER, BACKGROUND_HEALTH_CHECK_DB_SAVE_JOB_NAME, BASE_MCP_ROUTE, DAILY_TAG_SPEND_BATCH_MULTIPLIER, DEFAULT_MAX_RECURSE_DEPTH, DEFAULT_SHARED_HEALTH_CHECK_LOCK_TTL, DEFAULT_SHARED_HEALTH_CHECK_TTL, DEFAULT_SLACK_ALERTING_THRESHOLD, GRACEFUL_SHUTDOWN_TEARDOWN_HEADROOM_SECONDS, INTERNAL_CALLBACK_NAMES, LANGFUSE_SHUTDOWN_FLUSH_TIMEOUT_MILLIS, LITELLM_EMBEDDING_PROVIDERS_SUPPORTING_INPUT_ARRAY_OF_TOKENS, LITELLM_SETTINGS_SAFE_DB_OVERRIDES, LITELLM_UI_ALLOW_HEADERS, LITELLM_UI_SESSION_DURATION, OPENAI_LIVE_SESSION_START_TIMEOUT_SECONDS, RUNTIME_UPDATABLE_ROUTER_SETTINGS, SPEND_LOG_QUEUE_MAX_BACKOFF_SECONDS, VLLM_CHOICE_FIELDS, ) from litellm.cost_calculator import base_model_for_pricing from litellm.litellm_core_utils.access_windows import access_windows_config_error from litellm.litellm_core_utils.add_retry_fallback_headers import ( get_fallback_errors_from_headers, get_hidden_params_dict, ) from litellm.litellm_core_utils.asyncify import asyncify from litellm.litellm_core_utils.bug_report import ( allowlisted, bug_report_notice, should_report_bug, ) from litellm.litellm_core_utils.get_llm_provider_logic import declared_authenticating_provider from litellm.litellm_core_utils.litellm_logging import ( _init_custom_logger_compatible_class, initialize_sentry, ) from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER, LoggingWorker from litellm.litellm_core_utils.prompt_templates.chat_template_tools import ( ChatTemplateToolSection, chat_template_tool_choice_tokens, chat_template_tool_section, ) from litellm.litellm_core_utils.routing_common_utils import ( deployments_the_caller_may_use, resolve_model_group_alias, ) from litellm.litellm_core_utils.routing_groups import parse_routing_groups from litellm.litellm_core_utils.safe_json_dumps import safe_dumps from litellm.litellm_core_utils.safe_json_loads import safe_json_loads from litellm.litellm_core_utils.token_counter import ( UnsupportedContentTypeError, deployment_openai_model, offload_token_count, sends_function_tools, ) from litellm.llms.bedrock.count_tokens.kimi_k3 import ( HUGGING_FACE_SOURCE, KimiK3Encodings, kimi_k3_anthropic_tokens, kimi_k3_template_tokens, kimi_k3_tokens, ) from litellm.proxy._types import ( UI_TEAM_ID, AwsErrorTypeProxyException, CallbackDelete, CallInfo, CommonProxyErrors, ConfigFieldDelete, ConfigFieldInfo, ConfigFieldUpdate, ConfigGeneralSettings, ConfigList, ConfigYAML, CoordinationRedisParams, EnterpriseLicenseData, FieldDetail, InvitationClaim, InvitationDelete, InvitationModel, InvitationNew, InvitationUpdate, LiteLLM_EndUserTable, Litellm_EntityType, LiteLLM_JWTAuth, LiteLLM_TagTable, LiteLLM_TeamTable, LiteLLM_TeamTableCachedObj, LiteLLM_UserTable, LitellmUserRoles, PassThroughGenericEndpoint, ProxyErrorTypes, ProxyException, ProxyLifespanState, SpecialModelNames, SSOUserDefinedValues, SupportedDBObjectType, TeamDefaultSettings, TokenCountRequest, TransformRequestBody, UserAPIKeyAuth, ) from litellm.proxy.common_utils.callback_utils import ( is_sensitive_callback_key, normalize_callback_names, process_callback, ) from litellm.proxy.common_utils.realtime_utils import ( # noqa: F401, RUF100 # legacy module exports _realtime_request_body, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export realtime_request_body, ) from litellm.proxy.common_utils.token_count_settings import ( TOKENIZER_LOAD_MAX_RUNNING, TOKENIZER_LOAD_REUSE_SECONDS, TOKENIZER_LOAD_WAIT_SECONDS, ProviderCountFailure, TokenCountSettings, cost_map_entry, resolve_token_count_settings, ) from litellm.proxy.management_helpers.auto_router_availability import AutoRouterCatalogEntry, build_auto_router_catalog from litellm.proxy.management_helpers.model_group_custom_fields import model_group_custom_fields from litellm.proxy.management_helpers.utils import management_endpoint_span_wrapper from litellm.repositories import database as database_runtime from litellm.router_strategy.auto_router.auto_router_model_naming import ( GATED_AUTO_ROUTER_CAPABILITIES, STRATEGY_ROUTER_PARAM_FIELDS, capability_limit_violation, carries_complexity_router_settings, count_capability_routers, validate_complexity_router_config_placement, ) from litellm.router_strategy.auto_router.auto_router_tuning_baseline import ( TUNING_BASELINE_PARAM_NAME, mutable_tuned_identities, snapshot_tuning_baselines, tuning_limit_violation, ) from litellm.rust_bridge.catalog import NATIVE_MESSAGES_TARGETS, Target from litellm.types.utils import ( PRICING_OVERRIDES_KEY, CallTypes, Choices, CountContextManagement, LlmProviders, Message, ModelResponse, ModelResponseStream, TextCompletionResponse, TokenCountMessagesFormat, TokenCountResponse, TranscriptionResponse, echoed_cost_map_pricing_fields, is_server_derived_pricing_key, pricing_override_fields, ) from litellm.utils import ( HuggingFaceTokenizerKind, acredential_updates, cost_map_omits_token_price, native_structured_output_support, with_named_credential, ) if TYPE_CHECKING: from aiohttp import ClientSession from fastapi.routing import APIRoute from opentelemetry.trace import Span as _Span from prisma import models as prisma_models from litellm.integrations.opentelemetry import OpenTelemetry from litellm.proxy.common_utils.key_expiry_alert_manager import KeyExpiryAlertManager from litellm.proxy.health_check_utils.shared_health_check_manager import SharedHealthCheckManager from litellm.proxy.hooks.mcp_semantic_filter.hook import SemanticToolFilterConfig from litellm.types.proxy.model_listing import ModelInfoResponse Span = _Span else: Span = Any OpenTelemetry = Any REALTIME_REQUEST_SCOPE_TEMPLATE: Final[dict[str, object]] = { "type": "http", "method": "POST", "path": "/v1/realtime", } def showwarning(message, category, filename, lineno, file=None, line=None): traceback_info: Final = f"{filename}:{lineno}: {category.__name__}: {message}\n" if file is not None: file.write(traceback_info) warnings.showwarning = showwarning warnings.filterwarnings("default", category=UserWarning) # Your client code here messages: Final[list] = [] sys.path.insert(0, os.path.abspath("../..")) # Adds the parent directory to the system path - for litellm local dev try: import logging import backoff import fastapi import orjson import yaml from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.base import STATE_STOPPED except ImportError as e: raise ImportError(f"Missing dependency {e}. Run `pip install 'litellm[proxy]'`") list_of_messages: Final = [ "'The thing I wish you improved is...'", "'A feature I really want is...'", "'The worst thing about this product is...'", "'This product would be better if...'", "'I don't like how this works...'", "'It would help me if you could add...'", "'This feature doesn't meet my needs because...'", "'I get frustrated when the product...'", ] def generate_feedback_box(): box_width: Final = 60 # Select a random message message: Final = random.choice(list_of_messages) print_startup_message( "\n".join( ( "", "\033[1;37m" + "#" + "-" * box_width + "#\033[0m", "\033[1;37m" + "#" + " " * box_width + "#\033[0m", "\033[1;37m" + f"# {message:^59} #\033[0m", "\033[1;37m" + "# {:^59} #\033[0m".format("https://github.com/BerriAI/litellm/issues/new"), "\033[1;37m" + "#" + " " * box_width + "#\033[0m", "\033[1;37m" + "#" + "-" * box_width + "#\033[0m", "", " Thank you for using LiteLLM! - Krrish & Ishaan", "", "", "", "\033[1;31mGive Feedback / Get Help: https://github.com/BerriAI/litellm/issues/new\033[0m", "", "", ) ) ) import contextlib from collections import defaultdict from contextlib import AsyncExitStack, asynccontextmanager from functools import lru_cache, partial import litellm import litellm._redis from litellm import Router from litellm._internal_context import service_target, with_service_target from litellm._logging import ( STARTUP_OUTPUT_SETTINGS, print_startup_message, redact_internal_details_from_client_message, redact_string, verbose_proxy_logger, verbose_router_logger, ) from litellm.caching.caching import DualCache, RedisCache from litellm.caching.in_memory_cache import InMemoryCache from litellm.caching.redis_batch import drain_post_call_redis_batches from litellm.caching.redis_cache import RedisCircuitBreakerOpenError, is_redis_timeout_failure from litellm.caching.redis_cluster_cache import RedisClusterCache from litellm.constants import ( _REALTIME_BODY_CACHE_SIZE, APSCHEDULER_COALESCE, APSCHEDULER_MAX_INSTANCES, APSCHEDULER_MISFIRE_GRACE_TIME, APSCHEDULER_REPLACE_EXISTING, CLI_SSO_SESSION_TTL_SECONDS, DAYS_IN_A_MONTH, DEFAULT_HEALTH_CHECK_INTERVAL, DEFAULT_MANAGEMENT_OBJECT_IN_MEMORY_CACHE_TTL, DEFAULT_MAX_LRU_CACHE_SIZE, DEFAULT_MODEL_CREATED_AT_TIME, LITELLM_PROXY_ADMIN_NAME, LITELLM_PROXY_BUDGET_NAME, MONTHLY_SPEND_REPORT_JOB_ID, PROMETHEUS_FALLBACK_STATS_JOB_ID, PROMETHEUS_FALLBACK_STATS_SEND_TIME_HOURS, PROXY_BATCH_POLLING_ENABLED, PROXY_BATCH_POLLING_INTERVAL, PROXY_BATCH_WRITE_AT, PROXY_BUDGET_RESCHEDULER_MAX_TIME, PROXY_BUDGET_RESCHEDULER_MIN_TIME, PROXY_CONFIG_RELOAD_INTERVAL_SECONDS, REALTIME_SESSION_FAILURE_LOGGED_KEY, REALTIME_SESSION_SUCCESS_LOGGED_KEY, ROUTER_SETTINGS_MANAGED_OUTSIDE_CONFIG, SCHEDULED_JOB_POLL_INTERVAL_SECONDS, SPEND_CAPTURE_RATE_CHECK_JOB_ID, USER_SPEND_ALERTS_JOB_ID, WEEKLY_SPEND_REPORT_JOB_ID, XAI_API_BASE, ) from litellm.exceptions import RejectedRequestError from litellm.integrations.custom_guardrail import CustomGuardrail, ModifyResponseException from litellm.integrations.custom_logger import CustomLogger from litellm.integrations.generic_api.generic_api_callback import GenericAPILogger from litellm.integrations.lens.proxy.adapter import router as lens_router from litellm.integrations.lens.proxy.pages import router as lens_pages_router from litellm.integrations.SlackAlerting.slack_alerting import SlackAlerting from litellm.litellm_core_utils.agentic_loop_settings import ( validated_max_agentic_loops, ) from litellm.litellm_core_utils.audio_utils.transcription_streaming import ( TRANSCRIPTION_STREAM_DONE_FRAME, GuardedTranscriptionReplay, guarded_transcription_events, ) from litellm.litellm_core_utils.audio_utils.utils import resolve_speech_media_type from litellm.litellm_core_utils.core_helpers import ( as_object_list, as_str_mapping, attribute_or_none, dict_from_pairs, drop_params_flag, finite_cost, get_litellm_metadata_from_kwargs, get_parent_otel_span_from_kwargs, has_attributes, iter_items, json_contains, normalize_drop_params, require_attributes, unpackable_mapping, without, ) from litellm.litellm_core_utils.credential_accessor import CredentialAccessor from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.litellm_core_utils.realtime_errors import ( close_after_upstream_handshake_refusal, realtime_error_event, websocket_close_reason, ) from litellm.litellm_core_utils.sensitive_data_masker import ( SensitiveDataMasker, mask_sensitive_keys, ) from litellm.litellm_core_utils.stream_usage_chunks import is_injected_stream_usage_artifact from litellm.llms.anthropic.common_utils import system_without_claude_code_billing_header from litellm.llms.anthropic.messages.context_management.inference_view import ( InferenceRequestView, InferenceRequestViewError, count_context_edits, inference_request_view, ) from litellm.llms.anthropic.messages.convertible_blocks import ( deployment_drops_params, drop_non_string_tool_result_texts, malformed_tool_result_count_error, ) from litellm.llms.custom_httpx.aiohttp_pool_diagnostics import build_pool_wait_trace_config from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler, exclude_rsa_key_exchange_and_chacha20 from litellm.llms.custom_httpx.httpx_handler import HTTPHandler as JwksHTTPHandler from litellm.llms.openai.live import parse_live_session_start from litellm.llms.openai.live.handler import LiveAttach, LiveProviderFrameGate from litellm.llms.openai_like.model_info import MODEL_INFO_REFRESH_SECONDS from litellm.llms.vertex_ai.vertex_llm_base import VertexBase from litellm.messages.dispatch import messages_target from litellm.proxy._lazy_features import attach_lazy_features, reserve_lazy_slot from litellm.proxy._types import * from litellm.proxy.agent_endpoints.auth.agent_access_groups import CeilingResolver, resolve_agent_access_group_ceiling from litellm.proxy.alerting_endpoints.alerts import router as alerts_router from litellm.proxy.alerting_endpoints.model_deprecation_alerts import ( router as model_deprecation_alerts_router, ) from litellm.proxy.analytics_endpoints.analytics_endpoints import ( router as analytics_router, ) from litellm.proxy.anthropic_endpoints.answered_status import is_provider_overload from litellm.proxy.auth.auth_checks import ( ROLE_BASED_PERMISSIONS_ADAPTER, ExperimentalUIJWTToken, can_agent_call_model, can_key_call_resolved_model, get_team_object, log_db_metrics, personal_key_team_model_access_applies, ) from litellm.proxy.auth.auth_object_prefetch import AUTH_OBJECTS_TARGET from litellm.proxy.auth.auth_utils import ( MAX_REQUEST_SIZE_MB_SETTING, check_response_size_is_safe, is_request_body_safe, log_model_access_denial, log_once_if_budget_reservation_disabled, warn_once_if_custom_auth_skips_common_checks, ) from litellm.proxy.auth.fallback_budget import RouterFallbackBudgetCheck, router_fallback_budget_check from litellm.proxy.auth.fallback_model_access import router_fallback_access_check from litellm.proxy.auth.fallback_rate_limit import RouterFallbackRateLimitCheck from litellm.proxy.auth.legacy_empty_models_check import warn_about_legacy_empty_models from litellm.proxy.auth.litellm_license import AUTO_ROUTER_LICENSE_REMEDY, LicenseCheck from litellm.proxy.auth.login_throttle import ( LoginThrottle, declared_proxy_ranges, warn_login_counters_are_per_worker, warn_source_login_limit_is_off, ) from litellm.proxy.auth.master_key_boot_check import ( MASTER_KEY_ENV_VAR, SALT_KEY_ENV_VAR, WEAK_OR_UNSET_MASTER_KEY_OVERRIDE_ENV_VAR, announce_on_stderr_at_exit, enforce_master_key_boot_verdict, master_key_boot_verdict, with_stored_secrets_counted, ) from litellm.proxy.auth.model_checks import ( GLOBAL_ALIASES_ONLY, OtherDeploymentsBill, RoutedElsewhere, expand_wildcard_deployments_for_model_info, get_all_fallbacks, get_complete_model_list, get_key_models, get_mcp_server_ids, get_team_models, model_info_cost_map_entry, run_model_listing, wildcard_row_priced_model, ) from litellm.proxy.auth.password_policy import validate_password_not_breached, validate_password_policy from litellm.proxy.auth.user_api_key_auth import ( # noqa: F401, RUF100 # legacy module exports _fetch_global_spend_with_event_coordination, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export fetch_global_spend_with_event_coordination, user_api_key_auth, user_api_key_auth_accepted_websocket_for_model, user_api_key_auth_live_websocket, user_api_key_auth_realtime_websocket, user_api_key_auth_websocket, ) from litellm.proxy.batches_endpoints.endpoints import router as batches_router from litellm.proxy.bug_report_config import build_proxy_bug_report ## Import All Misc routes here ## from litellm.proxy.caching_routes import router as caching_router from litellm.proxy.common_request_processing import ( # noqa: F401, RUF100 # legacy module exports KNOWN_PROXY_ROUTES, ProxyBaseLLMRequestProcessing, _is_azure_model_router_request, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _should_return_raw_model_name, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export close_guarded_stream, create_response, is_azure_model_router_request, log_llm_api_exception, open_sse_before_first_byte, proxy_exception_from_http_exception, proxy_exception_from_route_error, refuse_s3_media_the_key_may_not_read, resolve_litellm_call_id, resolve_per_request_model_group_alias, saved_router_weights, should_return_raw_model_name, sse_error_payload, ttft_keepalive_interval, withheld_provider_output, ) from litellm.proxy.common_utils.anthropic_error_payload import ( anthropic_error_json_response, anthropic_internal_server_error_response, uses_anthropic_error_envelope, ) from litellm.proxy.common_utils.cache_invalidation_pubsub import publish_cache_invalidation from litellm.proxy.common_utils.callback_lifecycle import ( CALLBACK_CLOSE_TIMEOUT_SECONDS, close_proxy_callbacks, setup_proxy_callbacks, ) from litellm.proxy.common_utils.callback_utils import initialize_callbacks_on_proxy from litellm.proxy.common_utils.codex_catalog_source import codex_catalog_source from litellm.proxy.common_utils.codex_model_catalog import CodexCatalogRow, codex_catalog_rows, codex_model_list_body from litellm.proxy.common_utils.config_includes import resolve_include_file_path, resolve_includes from litellm.proxy.common_utils.config_sync_pubsub import ConfigSyncSubscriber from litellm.proxy.common_utils.credential_hydration import decrypted_or_stored from litellm.proxy.common_utils.custom_openapi_spec import JsonObject from litellm.proxy.common_utils.debug_utils import init_verbose_loggers from litellm.proxy.common_utils.debug_utils import router as debugging_endpoints_router from litellm.proxy.common_utils.discoverable_model_filter import ( discoverable_rows, undiscoverable_model_groups, undiscoverable_model_names, ) from litellm.proxy.common_utils.encrypt_decrypt_utils import ( decrypt_value_helper, encrypt_value_helper, ) from litellm.proxy.common_utils.error_body_call_id import JSON_OBJECT, error_body_call_id, with_call_id from litellm.proxy.common_utils.fips import enforce_fips_mode, fips_mode_setting from litellm.proxy.common_utils.healthy_model_filter import ( get_hidden_unhealthy_model_names, is_healthy_only_listing_default, ) from litellm.proxy.common_utils.hook_cache import HookCache from litellm.proxy.common_utils.html_forms.default_credentials_hint import should_hide_default_credentials_hint from litellm.proxy.common_utils.http_parsing_utils import ( # noqa: F401, RUF100 # legacy module exports _read_request_body, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _safe_get_request_headers, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export check_file_size_under_limit, get_form_data, read_request_body, resolve_inference_model, safe_get_request_headers, ) from litellm.proxy.common_utils.http_parsing_utils import ( resolve_realtime_route_model as _resolve_realtime_route_model, ) from litellm.proxy.common_utils.load_config_utils import get_config_from_bucket from litellm.proxy.common_utils.model_deprecation import collect_model_deprecations from litellm.proxy.common_utils.model_listing_utils import ( CallerAliases, ClaudeCodeRoutingNames, DeployedModelGroups, TeamModelNameTranslator, alias_listing_entries, alias_target, caller_alias_maps, claude_code_view_ids, configured_display_names, dispatched_model, label_entries, unlisted_alias_targets, ) from litellm.proxy.common_utils.openai_endpoint_utils import ( remove_sensitive_info_from_deployment, ) from litellm.proxy.common_utils.openai_error_payload import ( LITELLM_CALL_ID_HEADER, attribute_of, error_status_code, headers_with_litellm_call_id, litellm_call_id_headers, openai_error_param, openai_error_type, proxy_exception_for, route_failure_proxy_exception, with_litellm_call_id, ) from litellm.proxy.common_utils.periodic_reload_schedule import ( MODEL_COST_MAP_RELOAD_PARAM_NAME, clear_reload_interval, pod_reload_is_due, read_reload_schedule, record_manual_reload, record_reload_run, reload_schedule_status, utc_now, write_reload_interval, ) from litellm.proxy.common_utils.proxy_state import ProxyState from litellm.proxy.common_utils.registry_read_through import resync_model_deployments_by_ids from litellm.proxy.common_utils.reset_budget_job import ResetBudgetJob from litellm.proxy.common_utils.responses_stream_errors import ResponsesStreamErrorState, responses_chunk_data from litellm.proxy.common_utils.scheduled_job_stagger import ( apply_scheduled_job_stagger, attach_job_timing_logger, parse_stagger_settings, ) from litellm.proxy.common_utils.slow_gc_logger import log_slow_gc_passes from litellm.proxy.common_utils.state_cache import StateCache from litellm.proxy.common_utils.swagger_utils import ERROR_RESPONSES from litellm.proxy.common_utils.timezone_utils import ( get_budget_reset_settings, get_budget_reset_time, ) from litellm.proxy.common_utils.token_count_log import ( answered_with_litellm_call_id, deployment_model_id, logged_token_count, rejected_token_count, ) from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache, get_management_object_ttl from litellm.proxy.common_utils.validation_error_body import public_validation_errors from litellm.proxy.config_resolvers import ( FieldSource, SettingsStore, config_ownership_message, resolve_fields, source_for, ) from litellm.proxy.config_resolvers.alerting import ( EMAIL_DESCRIPTORS, MS_TEAMS_DESCRIPTORS, SLACK_DESCRIPTORS, ) from litellm.proxy.config_resolvers.changed_section_keys import changed_section_keys from litellm.proxy.config_resolvers.settings_rules import ( ABSENT, DbRow, Section, SettingValue, coerce_bool, is_absent, is_resource_list, ) from litellm.proxy.config_resolvers.settings_rules import ( JsonValue as SettingsJsonValue, ) from litellm.proxy.config_resolvers.sso import SSO_FIELD_ENV_VARS from litellm.proxy.container_endpoints.endpoints import router as container_router from litellm.proxy.credential_endpoints.endpoints import router as credential_router from litellm.proxy.db.connection_budget import ( configure_pool_budget_from_settings, rust_connections, with_configured_connection_budget, ) from litellm.proxy.db.create_views import SupportsRawQueries from litellm.proxy.db.db_transaction_queue.pod_lock_manager import PodLockManager from litellm.proxy.db.db_transaction_queue.spend_log_cleanup import ( SPEND_LOG_RETENTION_JOB, SPEND_LOG_SIZE_CHECK_JOB, ) from litellm.proxy.db.exception_handler import ( PrismaDBExceptionHandler, call_with_db_reconnect_retry, ) from litellm.proxy.db.gateway_request_tracking import ( GatewayRequestAccumulator, GatewayRequestRedisBuffer, flush_gateway_requests, ) from litellm.proxy.db.master_key_migration import ( count_values_encrypted_with_or_none, migrate_if_requested, ) from litellm.proxy.db.proxy_worker_heartbeat import ( PROXY_WORKER_HEARTBEAT_INTERVAL_SECONDS, ProxyWorkerHeartbeat, ) from litellm.proxy.db.request_error_tracking import ( RequestErrorRedisBuffer, flush_request_errors, request_error_accumulator, ) from litellm.proxy.db.scheduled_job_runs import JobScheduler, register_poll from litellm.proxy.db.spend_counter_reseed import END_USER_COUNTER_PREFIX, SpendCounterReseed from litellm.proxy.discovery_endpoints import ( agent_skills_discovery_router, ui_discovery_endpoints_router, ) from litellm.proxy.fine_tuning_endpoints.endpoints import router as fine_tuning_router from litellm.proxy.fine_tuning_endpoints.endpoints import set_fine_tuning_config from litellm.proxy.google_endpoints.endpoints import router as google_router from litellm.proxy.guardrails.init_guardrails import ( init_guardrails_v2, initialize_guardrails, ) from litellm.proxy.health_check import ( deployment_ids, filter_deployments_to_model_groups, health_check_filter_kwargs_from_general_settings, parse_background_health_check_model_groups, perform_health_check, public_wildcard_children, split_by_model_ids, ) from litellm.proxy.health_endpoints._health_endpoints import router as health_router from litellm.proxy.health_endpoints.deployment_warning_endpoints import router as deployment_warning_router from litellm.proxy.hooks.model_max_budget_limiter import ( # noqa: F401, RUF100 # legacy module exports PROXY_VirtualKeyModelMaxBudgetLimiter, _PROXY_VirtualKeyModelMaxBudgetLimiter, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export ) from litellm.proxy.hooks.parallel_request_limiter_v3 import fail_closed_rate_limit_enforcement_enabled from litellm.proxy.hooks.prompt_injection_detection import ( # noqa: F401, RUF100 # legacy module exports OPTIONAL_PromptInjectionDetection, _OPTIONAL_PromptInjectionDetection, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export ) from litellm.proxy.hooks.proxy_track_cost_callback import ( # noqa: F401, RUF100 # legacy module exports ProxyDBLogger, _ProxyDBLogger, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export run_spend_event, ) from litellm.proxy.image_endpoints.endpoints import router as image_router from litellm.proxy.list_api.common import ( ManagementProblem, problem_response, request_validation_problem, ) from litellm.proxy.litellm_pre_call_utils import ( SCHEDULER_PRIORITY_HEADER, LiteLLMProxyRequestSetup, add_litellm_data_to_request, request_routing_metadata, request_served_model, sanitize_for_log, ) from litellm.proxy.logging_endpoints.callback_logs_endpoints import ( rust_control_plane_router, ) from litellm.proxy.management_endpoints.auto_router_endpoints import ( router as auto_router_management_router, ) from litellm.proxy.management_endpoints.budget_management_endpoints import ( router as budget_management_router, ) from litellm.proxy.management_endpoints.cache_settings_endpoints import ( router as cache_settings_router, ) from litellm.proxy.management_endpoints.callback_management_endpoints import ( router as callback_management_endpoints_router, ) from litellm.proxy.management_endpoints.common_utils import ( # noqa: F401, RUF100 # legacy module exports _user_has_admin_privileges, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _user_has_admin_view, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export admin_can_invite_user, user_api_key_has_admin_view, user_has_admin_privileges, ) from litellm.proxy.management_endpoints.coordination_redis_endpoints import ( get_persisted_coordination_redis_settings, ) from litellm.proxy.management_endpoints.coordination_redis_endpoints import ( router as coordination_redis_settings_router, ) from litellm.proxy.management_endpoints.cost_tracking_settings import ( router as cost_tracking_settings_router, ) from litellm.proxy.management_endpoints.customer_endpoints import ( router as customer_router, ) from litellm.proxy.management_endpoints.daily_activity_routes import router as daily_activity_router from litellm.proxy.management_endpoints.fallback_management_endpoints import ( apply_content_policy_fallbacks, ) from litellm.proxy.management_endpoints.fallback_management_endpoints import ( router as fallback_management_router, ) from litellm.proxy.management_endpoints.gateway_request_endpoints import ( router as gateway_request_router, ) from litellm.proxy.management_endpoints.internal_user_endpoints import ( router as internal_user_router, ) from litellm.proxy.management_endpoints.internal_user_endpoints import ( user_update, ) from litellm.proxy.management_endpoints.key_management_endpoints import ( delete_verification_tokens, duration_in_seconds, generate_key_helper_fn, parse_key_alias_pattern, ) from litellm.proxy.management_endpoints.key_management_endpoints import ( router as key_management_router, ) from litellm.proxy.management_endpoints.management_v1 import ( router as management_v1_router, ) from litellm.proxy.management_endpoints.management_v1.common import MANAGEMENT_V1_PREFIX from litellm.proxy.management_endpoints.model_access_group_management_endpoints import ( router as model_access_group_management_router, ) from litellm.proxy.management_endpoints.model_management_endpoints import ( # noqa: F401, RUF100 # legacy module exports _add_model_to_db, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _add_team_model_to_db, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _deduplicate_litellm_router_models, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export add_model_to_db, add_team_model_to_db, deduplicate_litellm_router_models, live_model_ids_snapshot, ) from litellm.proxy.management_endpoints.model_management_endpoints import ( router as model_management_router, ) from litellm.proxy.management_endpoints.organization_endpoints import ( router as organization_router, ) from litellm.proxy.management_endpoints.password_endpoints import ( router as password_management_router, ) from litellm.proxy.management_endpoints.prompt_caching_requests import ( router as prompt_caching_requests_router, ) from litellm.proxy.management_endpoints.request_error_endpoints import ( router as request_error_router, ) from litellm.proxy.management_endpoints.router_settings_endpoints import ( router as router_settings_router, ) from litellm.proxy.management_endpoints.scim.scim_errors import ScimError, scim_error_response from litellm.proxy.management_endpoints.session_endpoints import ( router as session_management_router, ) from litellm.proxy.management_endpoints.sso.saml_sso import SAML_AUTHN_REQUEST_TTL_SECONDS from litellm.proxy.management_endpoints.subscription_accounts import ( router as subscription_accounts_router, ) from litellm.proxy.management_endpoints.tag_management_endpoints import ( router as tag_management_router, ) from litellm.proxy.management_endpoints.team_callback_endpoints import ( router as team_callback_router, ) from litellm.proxy.management_endpoints.team_endpoints import router as team_router from litellm.proxy.management_endpoints.team_endpoints import ( update_team, validate_membership, ) from litellm.proxy.management_endpoints.ui_sso import ( get_disabled_non_admin_personal_key_creation, ) from litellm.proxy.management_endpoints.ui_sso import router as ui_sso_router from litellm.proxy.management_endpoints.user_agent_analytics_endpoints import ( router as user_agent_analytics_router, ) from litellm.proxy.management_endpoints.workflow_management_endpoints import ( router as workflow_management_router, ) from litellm.proxy.management_helpers.audit_logs import ( create_audit_log_for_update, create_object_audit_log, ) from litellm.proxy.management_helpers.team_metadata_validation import ( TEAM_METADATA_SCHEMA_REGISTRY, TEAM_METADATA_VALIDATOR_REGISTRY, parse_team_metadata_schema, ) from litellm.proxy.mcp_server.byok_credential_cache import byok_credential_cache from litellm.proxy.mcp_server.catalog import public_catalog_operation from litellm.proxy.mcp_server.stdio_gate import MCP_STDIO_ENABLED_ENV_VAR, is_mcp_stdio_flag_key from litellm.proxy.memory.memory_endpoints import router as memory_router from litellm.proxy.middleware.billable_request_metrics_middleware import ( BillableRequestMetricsMiddleware, BillingRecorder, ) from litellm.proxy.middleware.budget_reservation_release_middleware import ( BudgetReservationReleaseMiddleware, ) from litellm.proxy.middleware.litellm_response_headers_middleware import LiteLLMResponseHeadersMiddleware from litellm.proxy.middleware.redis_request_batch_middleware import RedisRequestBatchMiddleware from litellm.proxy.plugin_routes import ( register_plugins_from_config, ) from litellm.proxy.plugin_routes import ( router as plugin_router, ) from litellm.proxy.realtime_endpoints.live_delegation import authorize_live_delegation, live_client_frame from litellm.proxy.realtime_endpoints.sideband import SESSION_NOT_ADMITTED_CODE from litellm.proxy.spend_tracking.spend_event_producer import ( CollectorSettings, SpendEventProducer, build_spend_event_producer, ) from litellm.proxy.telemetry.endpoints import router as telemetry_router from litellm.proxy.telemetry.middleware import TelemetryMiddleware from litellm.proxy.telemetry.runtime import TelemetryRuntime from litellm.proxy.telemetry.settings import describe_errors as describe_telemetry_errors from litellm.proxy.telemetry.settings import load_settings as load_telemetry_settings from litellm.redis.stores.auth_cache import AuthObject from litellm.redis.stores.cache_invalidation import CacheInvalidation, PythonCaches, connect_invalidation from litellm.redis.stores.connection import connect from litellm.redis.stores.spend_counters import ( SPEND_COUNTERS_TARGET, SpendCounterStore, is_unavailable, ) from litellm.router import router_arguments from litellm.router_config import build_router_config try: from litellm.proxy.enterprise_billing.billing_metrics import ( build_billing_metrics_recorder as _build_billing_metrics_recorder, ) from litellm.proxy.enterprise_billing.billing_metrics import ( shutdown_billing_metrics_recorder as _shutdown_billing_metrics_recorder, ) build_billing_metrics_recorder: Callable[..., BillingRecorder | None] | None = _build_billing_metrics_recorder shutdown_billing_metrics_recorder: Callable[[], None] | None = _shutdown_billing_metrics_recorder except ImportError: build_billing_metrics_recorder = None shutdown_billing_metrics_recorder = None from fastapi.exception_handlers import http_exception_handler from starlette.background import BackgroundTask from starlette.exceptions import HTTPException as StarletteHTTPException from starlette.websockets import WebSocketState from litellm.integrations.lens.config import is_lens_tracing_enabled from litellm.integrations.lens.proxy import tracing_endpoints from litellm.integrations.lens.proxy.tracing_runtime import manage_tracing from litellm.litellm_core_utils.on_end_byte_stream import OnEndAsyncByteStream from litellm.proxy.middleware.admission_control_middleware import ( AdmissionControlMiddleware, admission_control_state, get_admission_control_settings, ) from litellm.proxy.middleware.gzip_middleware import GZipBufferedResponseMiddleware from litellm.proxy.middleware.in_flight_requests_middleware import ( InFlightRequestsMiddleware, ) from litellm.proxy.middleware.per_request_root_path_middleware import ( PerRequestRootPathMiddleware, get_server_root_paths, ) from litellm.proxy.middleware.prometheus_auth_middleware import PrometheusAuthMiddleware from litellm.proxy.middleware.request_body_decompression_middleware import RequestBodyDecompressionMiddleware from litellm.proxy.middleware.request_size_limit_middleware import ( RequestSizeLimitMiddleware, ) from litellm.proxy.middleware.security_headers_middleware import ( SecurityHeadersMiddleware, ) from litellm.proxy.moyai_endpoints import router as moyai_router from litellm.proxy.ocr_endpoints.endpoints import router as ocr_router from litellm.proxy.openai_files_endpoints.files_endpoints import ( router as openai_files_router, ) from litellm.proxy.openai_files_endpoints.files_endpoints import ( set_files_config, ) from litellm.proxy.pass_through_endpoints.openai_passthrough_endpoints import ( router as openai_passthrough_router, ) from litellm.proxy.pass_through_endpoints.pass_through_endpoints import ( initialize_pass_through_endpoints, ) from litellm.proxy.pass_through_endpoints.pass_through_endpoints import ( router as pass_through_router, ) from litellm.proxy.prometheus_cleanup import mark_dead_workers, mark_worker_exit from litellm.proxy.public_endpoints import router as public_endpoints_router from litellm.proxy.public_endpoints.public_v1 import router as public_v1_router from litellm.proxy.rag_endpoints.endpoints import router as rag_router from litellm.proxy.rerank_endpoints.endpoints import router as rerank_router from litellm.proxy.response_api_endpoints.endpoints import router as response_router from litellm.proxy.route_llm_request import per_request_router_settings, route_request from litellm.proxy.route_priority import hot_routes_first from litellm.proxy.search_endpoints.endpoints import router as search_router from litellm.proxy.shutdown.graceful_shutdown_manager import GracefulShutdownManager from litellm.proxy.shutdown.scheduled_jobs import ( AwaitableAsyncIOExecutor, pause_scheduled_jobs, stop_in_flight_scheduler_jobs, ) from litellm.proxy.spend_tracking import legacy_spend_counters from litellm.proxy.spend_tracking.background_interaction_settlement import ( install_background_interaction_settlement, ) from litellm.proxy.spend_tracking.budget_reservation import ( BudgetCounter, customer_window_counters, release_cancelled_budget_reservation, release_unbound_budget_reservation, ) from litellm.proxy.spend_tracking.rust_spend_counters import charge_spend_counters from litellm.proxy.spend_tracking.spend_capture_rate import ( run_scheduled_spend_capture_rate_check, ) from litellm.proxy.spend_tracking.spend_counter_batch import ( PendingSpendIncrement, active_spend_counter_batch, forget_spend_counter, post_call_counter_keys, read_batched_spend_counter, record_spend_counter_value, spend_counter_batch_scope, ) from litellm.proxy.spend_tracking.spend_logs_ledger_setting import capture_spend_logs_ledger_setting from litellm.proxy.spend_tracking.spend_management_endpoints import ( router as spend_management_router, ) from litellm.proxy.spend_tracking.spend_tracking_utils import get_logging_payload from litellm.proxy.types_utils.utils import get_instance_fn from litellm.proxy.ui_crud_endpoints.latest_release_endpoints import ( router as latest_release_endpoints_router, ) from litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints import ( router as ui_crud_endpoints_router, ) from litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints import ( sync_ui_settings_to_general_settings, ) from litellm.proxy.ui_crud_endpoints.reference_rates_endpoints import ( router as reference_rates_endpoints_router, ) from litellm.proxy.ui_crud_endpoints.user_banner_endpoints import ( router as user_banner_endpoints_router, ) from litellm.proxy.utils import ( # noqa: F401, RUF100 # legacy module exports PrismaClient, ProxyLogging, ProxyUpdateSpend, _cache_user_row, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _get_docs_url, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _get_openapi_url, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _get_projected_spend_over_limit, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _get_redoc_url, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _is_projected_spend_over_limit, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export _is_valid_team_configs, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export cache_user_row, evict_config_param, get_config_param, get_custom_url, get_docs_url, get_error_message_str, get_openapi_url, get_projected_spend_over_limit, get_redoc_url, get_server_root_path, handle_exception_on_proxy, hash_password, hash_token, invalidate_config_param, is_projected_spend_over_limit, # pyright: ignore[reportUnusedImport] # backwards-compatible package export is_valid_team_configs, litellm_config_cache, migrate_passwords_to_scrypt_async, model_dump_with_preserved_fields, prefetch_config_params, query_raw_rows, run_off_loop_if_fips, update_spend, ) from litellm.proxy.video_endpoints.endpoints import router as video_router from litellm.repositories.base_repository import SupportsModelDump from litellm.repositories.credentials_repository import CredentialsRepository from litellm.repositories.prisma_protocols import TableActions from litellm.responses.main import provider_stores_the_conversation from litellm.responses.streaming_iterator import drain_logging_tasks from litellm.router import ( AssistantsTypedDict, Deployment, LiteLLM_Params, ModelGroupInfo, ) from litellm.router_api import ChangeSet, RoutingContext, deployment_id, router_init_args from litellm.router_strategy.tag_based_routing import ( get_deployments_for_tag, get_tag_metadata_variable_name_from_kwargs, ) from litellm.scheduler import FlowItem, Scheduler from litellm.secret_managers.aws_secret_manager import load_aws_kms from litellm.secret_managers.google_kms import load_google_kms from litellm.secret_managers.main import ( get_secret, get_secret_bool, get_secret_str, normalize_nonempty_secret_str, secret_manager_would_be_consulted, str_to_bool, ) from litellm.types.integrations.slack_alerting import AlertType, SlackAlertingArgs from litellm.types.litellm_params import STREAM_USAGE_INJECTED_FIELD from litellm.types.llms.anthropic import ( AnthropicMessagesRequest, AnthropicResponse, AnthropicResponseContentBlockText, AnthropicResponseUsageBlock, ) from litellm.types.llms.openai import ( AllMessageValues, ChatCompletionSystemMessage, ChatCompletionToolParam, GenericEvent, HttpxBinaryResponseContent, ResponseCompletedEvent, ResponseCreatedEvent, ResponseFailedEvent, ResponseIncompleteEvent, ResponseInProgressEvent, ResponsesAPIResponse, ) from litellm.types.proxy.auth.customer_hierarchy import CustomerHierarchyIds, CustomerScopeAuth from litellm.types.proxy.carried_budget_state import EntitySpendCounterKeys from litellm.types.proxy.control_plane_endpoints import WorkerRegistryEntry from litellm.types.proxy.management_endpoints.model_management_endpoints import ( ModelGroupInfoProxy, ) from litellm.types.proxy.management_endpoints.ui_sso import ( DefaultTeamSSOParams, LiteLLM_UpperboundKeyGenerateParams, ) from litellm.types.proxy.model_deprecation import ( DEFAULT_DEPRECATION_WARN_DAYS, ModelDeprecationResponse, ) from litellm.types.proxy.spend_capture_rate import SpendCaptureProvider, SpendCaptureRateCheckSettings from litellm.types.realtime import RealtimeQueryParams from litellm.types.router import ( ClassifierPlugin, DeploymentTypedDict, RouterGeneralSettings, RoutingGroup, RoutingPlugin, SearchToolTypedDict, holds_secret_pointer, updateDeployment, ) from litellm.types.router import ModelInfo as RouterModelInfo from litellm.types.scheduler import DefaultPriorities from litellm.types.secret_managers.main import ( KeyManagementSettings, KeyManagementSystem, ) from litellm.types.utils import ( CredentialItem, CustomHuggingfaceTokenizer, RawRequestTypedDict, SelectTokenizerResponse, StandardLoggingPayload, ) from litellm.utils import add_custom_logger_callback_to_specific_event try: from litellm._version import version except Exception: version = "0.0.0" litellm.suppress_debug_info = True import json from fastapi import ( Depends, FastAPI, File, Form, Header, HTTPException, Path, Query, Request, Response, UploadFile, WebSocket, WebSocketDisconnect, applications, status, ) from fastapi.encoders import jsonable_encoder from fastapi.exceptions import RequestValidationError from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.docs import get_swagger_ui_html from fastapi.openapi.utils import get_openapi from fastapi.responses import ( FileResponse, HTMLResponse, JSONResponse, ORJSONResponse, RedirectResponse, StreamingResponse, ) from fastapi.routing import APIRouter from fastapi.security import OAuth2PasswordBearer from fastapi.security.api_key import APIKeyHeader from fastapi.staticfiles import StaticFiles # import enterprise folder enterprise_router = APIRouter() try: # when using litellm cli from litellm.proxy import enterprise except Exception: # when using litellm docker image try: from litellm import enterprise except Exception: pass ################### # Import enterprise routes try: from litellm_enterprise.proxy.enterprise_routes import router as _enterprise_router from litellm_enterprise.proxy.proxy_server import EnterpriseProxyConfig enterprise_router = _enterprise_router enterprise_proxy_config: EnterpriseProxyConfig | None = EnterpriseProxyConfig() except ImportError: enterprise_proxy_config = None ################### server_root_path: Final = get_server_root_path() _license_check = LicenseCheck() premium_user: bool = _license_check.is_premium() premium_user_data: Optional["EnterpriseLicenseData"] = _license_check.airgapped_license_data def _set_premium_user(is_premium: bool) -> None: global premium_user, premium_user_data premium_user = is_premium # rebind-ok: config load can switch the license in use premium_user_data = _license_check.airgapped_license_data # rebind-ok: tracks the license in use def _use_config_license(config_license: str, env_license_is_premium: bool) -> bool: env_license: Final = _license_check.license_str env_license_data: Final = _license_check.airgapped_license_data _license_check.license_str = config_license if _license_check.is_premium(): return True if not env_license_is_premium: return False _license_check.license_str = env_license _license_check.airgapped_license_data = env_license_data return True global_max_parallel_request_retries_env: Final[str | None] = os.getenv("LITELLM_GLOBAL_MAX_PARALLEL_REQUEST_RETRIES") proxy_state: Final = ProxyState() SENSITIVE_DATA_MASKER: Final = SensitiveDataMasker() # Secret-bearing general_settings fields the segment masker does not match by # name: database_url and database_extra_connection_params embed DB credentials, # pass_through_endpoints carry upstream Authorization headers, and # alert_to_webhook_url is itself a webhook secret _EXTRA_SECRET_GENERAL_SETTINGS_FIELDS: Final = frozenset( { "database_url", "database_extra_connection_params", "pass_through_endpoints", "alert_to_webhook_url", } ) def _redact_worker_config_for_logging(worker_config: str | dict[str, JsonValue] | None) -> JsonValue: """Mask sensitive fields in the worker config before it enters a log record. `worker_config` reaches `proxy_startup_event` as either the JSON blob persisted by `save_worker_config` (a string) or the dict passed directly to `initialize`. Both shapes can carry `master_key`, `database_url`, provider API keys, etc.; passing the raw value to `verbose_proxy_logger` leaks them whenever the last-line-of-defense regex filter is bypassed (`LITELLM_DISABLE_REDACT_SECRETS=true`, an older log sink, a downstream handler that captures records pre-filter). Redact at the source. """ if worker_config is None: return None if isinstance(worker_config, dict): return _redact_secret_values_in_obj(worker_config) parsed: Final[JsonValue] = safe_json_loads(worker_config, default=None) if isinstance(parsed, dict): return safe_dumps(_redact_secret_values_in_obj(parsed)) return worker_config if global_max_parallel_request_retries_env is None: global_max_parallel_request_retries: int = 3 else: global_max_parallel_request_retries = int(global_max_parallel_request_retries_env) global_max_parallel_request_retry_timeout_env: Final[str | None] = os.getenv( "LITELLM_GLOBAL_MAX_PARALLEL_REQUEST_RETRY_TIMEOUT" ) if global_max_parallel_request_retry_timeout_env is None: global_max_parallel_request_retry_timeout: float = 60.0 else: global_max_parallel_request_retry_timeout = float(global_max_parallel_request_retry_timeout_env) ui_link: Final = f"{server_root_path}/ui" fallback_login_link: Final = f"{server_root_path}/fallback/login" model_hub_link: Final = f"{server_root_path}/ui/model_hub_table" ui_message = f"šŸ‘‰ [```LiteLLM Admin Panel on /ui```]({ui_link}). Create, Edit Keys with SSO. Having issues? Try [```Fallback Login```]({fallback_login_link})" ui_message += "\n\nšŸ’ø [```LiteLLM Model Cost Map```](https://models.litellm.ai/)." ui_message += f"\n\nšŸ”Ž [```LiteLLM Model Hub```]({model_hub_link}). See available models on the proxy. [**Docs**](https://docs.litellm.ai/docs/proxy/ai_hub)" custom_swagger_message: Final = ( "[**Customize Swagger Docs**](https://docs.litellm.ai/docs/enterprise#operations--branding)" ) ### CUSTOM BRANDING [ENTERPRISE FEATURE] ### title: Final = os.getenv("DOCS_TITLE", "LiteLLM API") if premium_user else "LiteLLM API" _title: Final = title _description: Final = ( os.getenv( "DOCS_DESCRIPTION", f"Enterprise Edition \n\nProxy Server to call 100+ LLMs in the OpenAI format. {custom_swagger_message}\n\n{ui_message}", ) if premium_user else f"Proxy Server to call 100+ LLMs in the OpenAI format. {custom_swagger_message}\n\n{ui_message}" ) def cleanup_router_config_variables(): global \ master_key, \ user_config_file_path, \ otel_logging, \ user_custom_auth, \ user_custom_auth_path, \ user_custom_key_generate, \ user_custom_key_update, \ user_custom_key_policy, \ user_custom_sso, \ user_custom_ui_sso_sign_in_handler, \ use_background_health_checks, \ use_shared_health_check, \ health_check_interval, \ health_check_concurrency, \ prisma_client, \ heuristic_v1_tuning_baselines # Set all variables to None master_key = None user_config_file_path = None otel_logging = None user_custom_auth = None user_custom_auth_path = None user_custom_key_generate = None user_custom_key_update = None user_custom_key_policy = None TEAM_METADATA_VALIDATOR_REGISTRY.set(None) TEAM_METADATA_SCHEMA_REGISTRY.set(()) user_custom_sso = None user_custom_ui_sso_sign_in_handler = None use_background_health_checks = None use_shared_health_check = None health_check_interval = None health_check_concurrency = None prisma_client = None heuristic_v1_tuning_baselines = None async def flush_spend_counters_on_shutdown() -> None: if prisma_client is None: return try: await proxy_logging_obj.db_spend_update_writer.db_update_spend_transaction_handler( prisma_client=prisma_client, n_retry_times=3, proxy_logging_obj=proxy_logging_obj ) except Exception as e: # noqa: BLE001 # shutdown must continue even if the commit fails verbose_proxy_logger.exception("Error flushing spend counters on shutdown: %s", e) async def _flush_spend_logs_queue_on_shutdown() -> None: if prisma_client is None: return try: from litellm.proxy.utils import drain_spend_logs_queue await drain_spend_logs_queue( prisma_client=prisma_client, db_writer_client=db_writer_client, proxy_logging_obj=proxy_logging_obj, ) except Exception as e: # noqa: BLE001 # shutdown must continue even if the drain fails verbose_proxy_logger.exception("Error flushing spend logs queue on shutdown: %s", e) async def _flush_spend_on_shutdown() -> None: from litellm.proxy.utils import update_daily_tag_spend await flush_spend_counters_on_shutdown() if prisma_client is not None: await update_daily_tag_spend(prisma_client=prisma_client, proxy_logging_obj=proxy_logging_obj) await _flush_spend_logs_queue_on_shutdown() async def flush_rust_callbacks_on_shutdown() -> None: from litellm.integrations.custom_logger_v2.rust import flush_rust_loggers from litellm.litellm_core_utils.litellm_logging import ( _in_memory_loggers, # pyright: ignore[reportPrivateUsage] # dynamic per-key loggers are held only here ) await flush_rust_loggers( (*litellm.logging_callback_manager.get_all_callbacks(), *litellm.input_callback, *_in_memory_loggers) ) async def _bounded_rust_flush(flush_rust: Callable[[], Awaitable[None]], timeout: float) -> None: try: await asyncio.wait_for(flush_rust(), timeout=timeout) except asyncio.TimeoutError: verbose_proxy_logger.warning( "Rust callback shutdown flush did not finish within %.3fs; their queued rows may be lost", timeout ) async def _flush_spend_around_logging_drain_on_shutdown( flush_spend: Callable[[], Awaitable[None]] = _flush_spend_on_shutdown, *, flush_rust: Callable[[], Awaitable[None]] = flush_rust_callbacks_on_shutdown, worker: LoggingWorker = GLOBAL_LOGGING_WORKER, clock: Callable[[], float] = time.monotonic, drain_tasks: Callable[[float], Awaitable[None]] = drain_logging_tasks, ) -> None: await flush_spend() remaining: Final = GracefulShutdownManager.remaining(clock=clock) callback_timeout: Final = max(0.0, remaining - GRACEFUL_SHUTDOWN_TEARDOWN_HEADROOM_SECONDS) verbose_proxy_logger.info( "Draining success callbacks before the final spend flush remaining_s=%.3f callback_timeout_s=%.3f", remaining, callback_timeout, ) async def drain_callbacks() -> None: await drain_tasks(callback_timeout) await worker.flush() try: await asyncio.wait_for(drain_callbacks(), timeout=callback_timeout) except asyncio.TimeoutError: verbose_proxy_logger.warning( "Success callback shutdown budget exhausted with %d unfinished callbacks; accounting may be incomplete", worker.pending_count, ) finally: await worker.stop(discard_pending=True, timeout=GracefulShutdownManager.remaining(2.0, clock=clock)) await drain_tasks(GracefulShutdownManager.remaining(5.0, clock=clock)) _ = await asyncio.gather( flush_spend(), _bounded_rust_flush(flush_rust, GracefulShutdownManager.remaining(clock=clock)) ) async def _drain_audit_tasks_on_shutdown() -> None: try: from litellm.proxy.management_helpers.audit_logs import drain_audit_tasks await drain_audit_tasks() except Exception as e: # noqa: BLE001 # shutdown must continue even if the drain fails verbose_proxy_logger.exception("Error draining audit tasks on shutdown: %s", e) async def _flush_request_accumulators(prisma: PrismaClient) -> None: await flush_gateway_requests(prisma, gateway_request_accumulator) await flush_request_errors(prisma, request_error_accumulator) async def proxy_shutdown_event(worker_heartbeat: ProxyWorkerHeartbeat | None = None) -> None: global prisma_client, master_key, user_custom_auth, user_custom_key_generate, user_custom_key_update verbose_proxy_logger.info("Shutting down LiteLLM Proxy Server") if worker_heartbeat is not None and prisma_client: await worker_heartbeat.deregister() if prisma_client: await asyncio.gather(_drain_audit_tasks_on_shutdown(), _flush_request_accumulators(prisma_client)) else: await _drain_audit_tasks_on_shutdown() await drain_post_call_redis_batches() if litellm.cache is not None: await litellm.cache.disconnect() await jwt_http_handler.close() if db_writer_client is not None: await db_writer_client.close() # final flush of billable-request counts: without it, up to one export # interval of enterprise billing data is dropped on every restart if shutdown_billing_metrics_recorder is not None: shutdown_billing_metrics_recorder() if "litellm.integrations.langfuse.langfuse_sdk" in sys.modules: try: from litellm.integrations.langfuse.langfuse_sdk import flush_langfuse_tracing flushed: Final = await asyncio.to_thread(flush_langfuse_tracing, LANGFUSE_SHUTDOWN_FLUSH_TIMEOUT_MILLIS) if flushed: verbose_proxy_logger.info("Langfuse export channels flushed") else: verbose_proxy_logger.warning( "Langfuse shutdown flush incomplete: a channel did not finish within %dms or a batch was rejected " "(see the export errors above); remaining spans are left to the background exporter", LANGFUSE_SHUTDOWN_FLUSH_TIMEOUT_MILLIS, ) except Exception as e: # noqa: BLE001 # shutdown must continue even if the flush fails verbose_proxy_logger.exception("Error flushing Langfuse export channels on shutdown: %s", e) ## RESET CUSTOM VARIABLES ## cleanup_router_config_variables() _AiohttpAddrInfo: TypeAlias = tuple[int | socket.AddressFamily, int | socket.SocketKind, int, str, tuple[object, ...]] @with_config(ConfigDict(extra="allow", strict=True)) class _LoginRequestBody(TypedDict, total=False): username: ReadOnly[object] password: ReadOnly[object] @with_config(ConfigDict(extra="allow", strict=True)) class _LoginExchangeRequestBody(TypedDict, total=False): code: ReadOnly[object] @with_config(ConfigDict(extra="allow", strict=True)) class _AnthropicBetaHeadersReloadConfig(TypedDict, total=False): interval_hours: ReadOnly[int | float | None] force_reload: ReadOnly[object] @with_config(ConfigDict(extra="allow", strict=True)) class _WebsearchInterceptionLitellmSettings(TypedDict, total=False): websearch_interception_params: ReadOnly[object] class _SemanticToolFilterLitellmSettings(TypedDict, total=False): mcp_semantic_tool_filter: ReadOnly["SemanticToolFilterConfig | None"] def _stored_semantic_filter_config( litellm_settings: _SemanticToolFilterLitellmSettings, ) -> "SemanticToolFilterConfig | None": return litellm_settings.get("mcp_semantic_tool_filter", None) _LOGIN_REQUEST_BODY: Final = TypeAdapter(_LoginRequestBody) _LOGIN_EXCHANGE_REQUEST_BODY: Final = TypeAdapter(_LoginExchangeRequestBody) _ANTHROPIC_BETA_HEADERS_RELOAD_CONFIG: Final = TypeAdapter(_AnthropicBetaHeadersReloadConfig) _LITELLM_PARAMS_COPY: Final = TypeAdapter(dict[str, object]) _WEBSEARCH_INTERCEPTION_LITELLM_SETTINGS: Final = TypeAdapter(_WebsearchInterceptionLitellmSettings) _UNTYPED_VALUE: Final = TypeAdapter(object) class _AiohttpConnectorKwargs(TypedDict, total=False): keepalive_timeout: float ttl_dns_cache: int enable_cleanup_closed: bool limit: int limit_per_host: int socket_factory: Callable[[_AiohttpAddrInfo], socket.socket] async def initialize_shared_aiohttp_session() -> "ClientSession | None": """Initialize shared aiohttp session for connection reuse with connection limits.""" try: from aiohttp import ClientSession, DummyCookieJar, TCPConnector from litellm.llms.custom_httpx.http_handler import ( build_aiohttp_keepalive_socket_factory, ) connector_kwargs: Final[_AiohttpConnectorKwargs] = { "keepalive_timeout": AIOHTTP_KEEPALIVE_TIMEOUT, "ttl_dns_cache": AIOHTTP_TTL_DNS_CACHE, } if AIOHTTP_NEEDS_CLEANUP_CLOSED: connector_kwargs["enable_cleanup_closed"] = True if AIOHTTP_CONNECTOR_LIMIT > 0: connector_kwargs["limit"] = AIOHTTP_CONNECTOR_LIMIT if AIOHTTP_CONNECTOR_LIMIT_PER_HOST > 0: connector_kwargs["limit_per_host"] = AIOHTTP_CONNECTOR_LIMIT_PER_HOST socket_factory: Final = build_aiohttp_keepalive_socket_factory() if socket_factory is not None: connector_kwargs["socket_factory"] = socket_factory connector: Final = TCPConnector(**connector_kwargs) session: Final = ClientSession( connector=connector, cookie_jar=DummyCookieJar(), trace_configs=[build_pool_wait_trace_config()], ) verbose_proxy_logger.info( "SESSION REUSE: Created shared aiohttp session for connection pooling (ID: %s, limit=%s, limit_per_host=%s)", id(session), AIOHTTP_CONNECTOR_LIMIT, AIOHTTP_CONNECTOR_LIMIT_PER_HOST, ) return session except Exception as e: verbose_proxy_logger.warning( "Failed to create shared aiohttp session: %s. Continuing without session reuse.", e ) return None _initialize_shared_aiohttp_session: Final = initialize_shared_aiohttp_session async def _connect_to_count_stored_values(resources: AsyncExitStack) -> SupportsRawQueries: client: Final = prisma_client or PrismaClient( database_url=str(get_secret("DATABASE_URL")), proxy_logging_obj=proxy_logging_obj ) if prisma_client is None: resources.push_async_callback(client.aclose) await client.connect() return client.writer_db # pyright: ignore[reportReturnType] # PrismaWrapper forwards query_raw via __getattr__ async def _own_database(resources: AsyncExitStack, client: PrismaClient | None) -> database_runtime.Database | None: if client is None: return None from litellm.proxy.db.rust_spend_log_writer import spend_log_writer_runs_in_rust connections: Final = rust_connections( os.environ, rust_spend_log_writer=spend_log_writer_runs_in_rust(general_settings_view()), ) database: Final = await database_runtime.Database.open( client, os.environ["DATABASE_URL"], os.environ.get("DATABASE_URL_READ_REPLICA") or None, writer_connections=connections.writer, reader_connections=connections.reader, ) resources.push_async_callback(database.aclose) return database async def _install_rust_spend_log_writer(resources: AsyncExitStack, database: database_runtime.Database | None) -> None: if database is None or database.native is None: return from litellm.proxy.db.rust_spend_log_writer import ( install_rust_spend_log_writer, uninstall_rust_spend_log_writer, ) if await install_rust_spend_log_writer(database.prisma, database.native, general_settings_view()) is not None: resources.push_async_callback(uninstall_rust_spend_log_writer, database.prisma) def _forget_prisma_client() -> None: global prisma_client # noqa: PLW0603 # routes still read this global until they move to DatabaseDep prisma_client = None # rebind-ok: a closed client must not survive into the next lifespan entry @asynccontextmanager async def proxy_startup_event(app: FastAPI) -> AsyncGenerator[ProxyLifespanState, None]: with GracefulShutdownManager.capture_sigterm(): async with AsyncExitStack() as resources, _proxy_lifespan(app, resources) as state: yield state def register_prompt_cache_alerts( settings: Mapping[str, object], prisma: PrismaClient | None, resources: AsyncExitStack, ) -> None: """Off unless `general_settings.prompt_cache_alerts` is true and a database and a shared salt exist. The logger reads the shared Redis at each callback and leaves the callback lists when the lifespan ends. """ if settings.get("prompt_cache_alerts") is not True or prisma is None: return from litellm.proxy.common_utils.encrypt_decrypt_utils import get_salt_key salt: Final = get_salt_key() if not salt: return from litellm.integrations.prompt_cache_alerts.detector import prompt_cache_alert_logger from litellm.repositories.alert_repository import AlertRepository logger: Final = prompt_cache_alert_logger(AlertRepository(prisma), lambda: redis_usage_cache, salt) litellm.logging_callback_manager.add_litellm_callback(logger) resources.callback(litellm.logging_callback_manager.remove_callback_from_all_lists, logger) @asynccontextmanager async def _proxy_lifespan(app: FastAPI, resources: AsyncExitStack) -> AsyncGenerator[ProxyLifespanState, None]: global \ prisma_client, \ master_key, \ use_background_health_checks, \ llm_router, \ llm_model_list, \ general_settings, \ proxy_budget_rescheduler_min_time, \ proxy_budget_rescheduler_max_time, \ litellm_proxy_admin_name, \ db_writer_client, \ store_model_in_db, \ premium_user, \ _license_check, \ proxy_batch_polling_interval, \ shared_aiohttp_session global ui_login_enabled # noqa: PLW0603 # read once at startup so auth does not look the secret up per request global database_url_configured # noqa: PLW0603 # read once at startup so auth does not look the secret up per request import json resources.callback(_forget_prisma_client) existing_database: Final = await _own_database(resources, prisma_client) init_verbose_loggers() running_loop: Final = asyncio.get_running_loop() if isinstance(running_loop, asyncio.BaseEventLoop) and running_loop.get_debug(): log_slow_gc_passes(threshold_seconds=running_loop.slow_callback_duration, log=verbose_proxy_logger.warning) prometheus_multiproc_dir: Final = os.environ.get("PROMETHEUS_MULTIPROC_DIR") if prometheus_multiproc_dir: mark_dead_workers(prometheus_multiproc_dir) ## RUN WORKER STARTUP HOOKS (e.g., gflags initialization) ## _startup_hooks_env: Final = os.environ.get("LITELLM_WORKER_STARTUP_HOOKS", "") if _startup_hooks_env: for _hook_spec in _startup_hooks_env.split(","): _hook_spec = _hook_spec.strip() if not _hook_spec: continue try: if ":" not in _hook_spec: raise ValueError( f"Invalid hook spec '{_hook_spec}': expected format is 'module.path:function_name'" ) _module_path, _func_name = _hook_spec.rsplit(":", 1) _module = importlib.import_module(_module_path) _hook_fn = _UNTYPED_VALUE.validate_python(getattr(_module, _func_name)) if inspect.iscoroutinefunction(_hook_fn): await _hook_fn() elif callable(_hook_fn): _hook_fn() else: raise TypeError(f"'{type(_hook_fn).__name__}' object is not callable") verbose_proxy_logger.info("Worker startup hook '%s' executed successfully", _hook_spec) except Exception as e: verbose_proxy_logger.error("Worker startup hook '%s' failed: %s", _hook_spec, e) raise ## CHECK PREMIUM USER verbose_proxy_logger.debug("litellm.proxy.proxy_server.py::startup() - CHECKING PREMIUM USER - %s", premium_user) if premium_user is False: premium_user = _license_check.is_premium() ## CHECK MASTER KEY IN ENVIRONMENT ## master_key = get_secret_str("LITELLM_MASTER_KEY") ### LOAD CONFIG ### worker_config: str | dict | None = get_secret("WORKER_CONFIG") env_config_yaml: Final[str | None] = get_secret_str("CONFIG_FILE_PATH") verbose_proxy_logger.debug("worker_config: %s", _redact_worker_config_for_logging(worker_config)) # check if it's a valid file path if env_config_yaml is not None: if os.path.isfile(env_config_yaml) and proxy_config.is_yaml(config_file_path=env_config_yaml): ( llm_router, llm_model_list, general_settings, ) = await proxy_config.load_config(router=llm_router, config_file_path=env_config_yaml) elif worker_config is not None: if ( ( isinstance(worker_config, str) and os.path.isfile(worker_config) and proxy_config.is_yaml(config_file_path=worker_config) ) or os.environ.get("LITELLM_CONFIG_BUCKET_NAME") is not None and isinstance(worker_config, str) ): ( llm_router, llm_model_list, general_settings, ) = await proxy_config.load_config(router=llm_router, config_file_path=worker_config) elif isinstance(worker_config, dict): await initialize_from_worker_config(worker_config) else: # if not, assume it's a json string decoded_worker_config: Final = as_str_mapping(_UNTYPED_VALUE.validate_python(json.loads(worker_config))) if decoded_worker_config is not None: await initialize_from_worker_config(decoded_worker_config) settings: Final = general_settings_view() enforce_fips_mode(settings) if configure_pool_budget_from_settings.get() and prisma_client is None: from litellm.proxy.db.rust_spend_log_writer import spend_log_writer_runs_in_rust resources.enter_context( with_configured_connection_budget( settings.get("database_connection_pool_limit"), rust_spend_log_writer=spend_log_writer_runs_in_rust(settings), ) ) resources.callback(capture_spend_logs_ledger_setting(settings)) async with AsyncExitStack() as boot_database: enforce_master_key_boot_verdict( await with_stored_secrets_counted( master_key_boot_verdict( master_key=master_key, environment_master_key=os.getenv(MASTER_KEY_ENV_VAR), general_settings=settings, config_file_path=user_config_file_path, override_env_is_on=get_secret_bool(WEAK_OR_UNSET_MASTER_KEY_OVERRIDE_ENV_VAR) is True, salt_key_is_set=os.getenv(SALT_KEY_ENV_VAR) is not None, database_is_configured=prisma_client is not None or get_secret("DATABASE_URL", None) is not None, ), count_values_encrypted_with=partial( count_values_encrypted_with_or_none, partial(_connect_to_count_stored_values, boot_database) ), ), announce=announce_on_stderr_at_exit, ) # check if DATABASE_URL in environment - load from there if prisma_client is None: _db_url: Final[str | None] = get_secret("DATABASE_URL", None) database_url_configured = isinstance(_db_url, str) and _db_url.strip() != "" # rebind-ok: set once at startup prisma_client = ( # rebind-ok: pre-existing rebinding on a rename-only line await ProxyStartupEvent.setup_prisma_client( database_url=_db_url if database_url_configured else None, proxy_logging_obj=proxy_logging_obj, user_api_key_cache=user_api_key_cache, ) ) database: Final = existing_database or await _own_database(resources, prisma_client) await _install_rust_spend_log_writer(resources, database) await migrate_if_requested( environ=os.environ, master_key=master_key, connected_database=lambda: None if prisma_client is None else prisma_client.writer_db, log=verbose_proxy_logger.warning, raise_unless_tolerated=PrismaDBExceptionHandler.handle_db_exception, ) if prisma_client is not None: startup_prisma: Final = prisma_client async def _run_pw_migration() -> None: try: result: Final = await migrate_passwords_to_scrypt_async(startup_prisma) verbose_proxy_logger.info("Password migration: %s", result) except Exception as e: verbose_proxy_logger.warning("Password migration skipped: %s", e) await resources.enter_async_context(database_runtime.background_task(_run_pw_migration())) async def _run_agent_grant_id_migration() -> None: from litellm.proxy.agent_endpoints.agent_registry import ( global_agent_registry, object_permission_table, ) for attempt in range(3): try: result = await global_agent_registry.migrate_legacy_grant_ids( table=object_permission_table(startup_prisma) ) if result.rewritten: verbose_proxy_logger.info( "Rewrote %s object_permission rows from legacy config agent ids", result.rewritten ) if result.missed == 0: return verbose_proxy_logger.warning( "Legacy agent grant id migration attempt %s/3 left %s rows unmigrated", attempt + 1, result.missed, ) except Exception as e: # noqa: BLE001 # startup task must survive any DB error and retry verbose_proxy_logger.warning( "Legacy agent grant id migration attempt %s/3 failed: %s", attempt + 1, e ) if attempt < 2: await asyncio.sleep(5) await resources.enter_async_context(database_runtime.background_task(_run_agent_grant_id_migration())) await resources.enter_async_context( database_runtime.background_task( warn_about_legacy_empty_models( keys=VerificationTokenRepository(prisma_client), teams=TeamRepository(prisma_client), ) ) ) ## A coordination_redis block saved from the admin UI lives in the database, ## which is only reachable once the prisma client exists. Apply it here, before ## the coordination Redis is published to its consumers below. db_coordination_redis_cache: Final = await ProxyStartupEvent.init_coordination_redis_from_db( litellm_settings=proxy_config.get_config_state().get("litellm_settings") or {}, llm_router=llm_router, ) if db_coordination_redis_cache is not None: _set_redis_usage_cache(db_coordination_redis_cache) ## use_redis_transaction_buffer: fall back to a standalone Redis (REDIS_* env) ## when the proxy cache backend is not Redis ## transaction_buffer_redis_cache = redis_usage_cache if transaction_buffer_redis_cache is None: transaction_buffer_redis_cache = ProxyStartupEvent.get_transaction_buffer_redis_cache( # rebind-ok: pre-existing rebinding on a rename-only line general_settings=general_settings ) ProxyStartupEvent.initialize_startup_logging( llm_router=llm_router, proxy_logging_obj=proxy_logging_obj, redis_usage_cache=transaction_buffer_redis_cache, ) ## V2 OTEL: publish the chosen V2 logger's TracerProvider as the OTel global. ## This MUST run after callback initialization above: a preset (arize, langfuse, ## …) builds its logger there, folding the OTEL_* base exporter and its own ## exporter into one logger. The FastAPI instrumentation mounted at app-creation ## binds to the global provider, so reusing that one logger is what makes the ## server span and the gen-ai spans share one provider and land in the same ## trace, exporting to every configured backend. Running before callback init ## (when no logger exists yet) would build a second, generic logger whose ## provider became the global, orphaning the gen-ai spans onto a different ## backend than the server span. A generic logger is built only when none was ## configured. try: from litellm.integrations.otel.model.config import is_otel_v2_enabled if is_otel_v2_enabled(): from opentelemetry import trace as _otel_trace from litellm.integrations.otel.logger import ( OpenTelemetryV2, publish_global_otel_v2_provider, ) from litellm.litellm_core_utils.litellm_logging import _in_memory_loggers registered: Final = open_telemetry_logger if isinstance(open_telemetry_logger, OpenTelemetryV2) else None publish_global_otel_v2_provider( _in_memory_loggers, # any-ok: pre-existing untyped List[Any] global _otel_trace.set_tracer_provider, registered=registered, ) except Exception as e: verbose_proxy_logger.debug("Skipping OTel V2 provider setup: %s", e) ## Validate use_redis_transaction_buffer requires Redis cache ## ProxyStartupEvent.validate_redis_transaction_buffer_config( general_settings=general_settings, redis_usage_cache=transaction_buffer_redis_cache, ) ProxyStartupEvent.warn_if_mock_testing_params_enabled(general_settings=general_settings) ## SEMANTIC TOOL FILTER ## # Read litellm_settings from config for semantic filter initialization try: verbose_proxy_logger.debug("About to initialize semantic tool filter") _config: Final = proxy_config.get_config_state() _litellm_settings: Final = _config.get("litellm_settings", {}) verbose_proxy_logger.debug("litellm_settings keys = %s", list(_litellm_settings.keys())) await ProxyStartupEvent.initialize_semantic_tool_filter( llm_router=llm_router, litellm_settings=_litellm_settings, ) verbose_proxy_logger.debug("After semantic tool filter initialization") except Exception as e: verbose_proxy_logger.error("Semantic filter init failed: %s", e, exc_info=True) ui_login_enabled = get_secret_bool("EXPERIMENTAL_UI_LOGIN") is not False ## JWT AUTH ## ProxyStartupEvent.initialize_jwt_auth( general_settings=general_settings, prisma_client=prisma_client, user_api_key_cache=user_api_key_cache, ) ProxyStartupEvent.attach_router_to_prompt_injection_detectors(llm_router=llm_router) verbose_proxy_logger.debug("prisma_client: %s", prisma_client) if prisma_client is not None and litellm.max_budget > 0: ProxyStartupEvent.add_proxy_budget_to_db() asyncio.create_task( ProxyStartupEvent.warm_global_spend_cache( user_api_key_cache=user_api_key_cache, prisma_client=prisma_client, ) ) ProxyStartupEvent.warn_budget_without_db( max_budget=litellm.max_budget, prisma_client=prisma_client, ) ProxyStartupEvent.warn_fail_closed_rate_limits_without_redis( fail_closed_rate_limit_enforcement=fail_closed_rate_limit_enforcement_enabled(general_settings), redis_usage_cache=redis_usage_cache, ) register_prompt_cache_alerts(general_settings_view(), prisma_client, resources) ### START BATCH WRITING DB + CHECKING NEW MODELS### worker_heartbeat: Final = ( await ProxyStartupEvent.initialize_scheduled_background_jobs( general_settings=general_settings, prisma_client=prisma_client, proxy_budget_rescheduler_min_time=proxy_budget_rescheduler_min_time, proxy_budget_rescheduler_max_time=proxy_budget_rescheduler_max_time, proxy_batch_write_at=proxy_batch_write_at, proxy_logging_obj=proxy_logging_obj, ) if prisma_client is not None else None ) if prisma_client is not None: await ProxyStartupEvent.update_default_team_member_budget() ## SYNC UI SETTINGS ## await ProxyStartupEvent.sync_ui_settings_to_general_settings() await install_background_interaction_settlement( prisma_client, router=lambda: llm_router, spend_logs_kept=lambda: disable_spend_logs is False ) # Start background health checks AFTER models are loaded and index is built if use_background_health_checks: asyncio.create_task(_run_background_health_check()) # start the background health check coroutine. # Start adaptive-router queue flusher unconditionally — adaptive routers # may be added later via `/config/reload`, and the flusher is a no-op when # `llm_router.adaptive_routers` is empty. Per-router DB state is loaded # lazily by the flusher on first tick (see `_state_loaded` flag) so # hot-reloaded routers also get their persisted priors. if llm_router is not None and llm_router.registry().plugins("adaptive"): for _tagged_routers in llm_router.registry().plugins("adaptive").values(): for _tagged in _tagged_routers: await _tagged.strategy.load_state_from_db(prisma_client) _tagged.strategy.state_loaded = True asyncio.create_task(_adaptive_router_flusher_loop()) ## [Optional] Initialize dd tracer ProxyStartupEvent.init_dd_tracer() ## [Optional] Initialize Pyroscope continuous profiling (env: LITELLM_ENABLE_PYROSCOPE=true) ProxyStartupEvent.init_pyroscope() ## Initialize shared aiohttp session for connection reuse shared_aiohttp_session = ( # rebind-ok: pre-existing rebinding on a rename-only line await initialize_shared_aiohttp_session() ) model_info_refresh_disabled: Final = ( "disable_model_info_refresh" in general_settings and general_settings["disable_model_info_refresh"] is True ) model_info_scheduler: Final = ( None if model_info_refresh_disabled else scheduler if scheduler is not None else AsyncIOScheduler() ) if model_info_scheduler is not None: model_info_scheduler.add_job( ProxyStartupEvent.refresh_model_info, "interval", seconds=MODEL_INFO_REFRESH_SECONDS, id="refresh_model_info", next_run_time=datetime.now(timezone.utc), max_instances=1, replace_existing=True, ) if not model_info_scheduler.running: model_info_scheduler.start() tracing_settings: Final = cast( # cast-ok: Pydantic validates the legacy untyped settings value dict[str, object] | None, TypeAdapter(dict[str, object] | None).validate_python(general_settings.get("tracing")), ) tracing_enabled: Final = is_lens_tracing_enabled(tracing_settings) async with manage_tracing( enabled=tracing_enabled, settings=tracing_settings, ) as receiver: state: Final[ProxyLifespanState] = {"tracing_receiver": receiver, "database": database} from litellm.proxy.admin_mcp import admin_mcp_lifespan telemetry_settings: Final = load_telemetry_settings() if isinstance(telemetry_settings, ValidationError): verbose_proxy_logger.warning( "telemetry: invalid settings, leaving it off: %s", describe_telemetry_errors(telemetry_settings) ) telemetry_start: Final = ( None if isinstance(telemetry_settings, ValidationError) else asyncio.create_task( telemetry_runtime.start( litellm_version=version, settings=telemetry_settings, db=lambda: ( # pyright: ignore[reportArgumentType] # PrismaWrapper forwards raw queries via __getattr__ prisma_client.writer_db if prisma_client is not None else None ), register=litellm.logging_callback_manager.add_litellm_callback, ) ) ) if telemetry_start is not None: telemetry_start.add_done_callback(_log_telemetry_start_failure) startup_callbacks: Final = tuple(litellm.logging_callback_manager.get_all_callbacks()) try: async with AsyncExitStack() as admin_mcp_stack: try: await admin_mcp_stack.enter_async_context(admin_mcp_lifespan(app)) await setup_proxy_callbacks(app, startup_callbacks) gc.freeze() verbose_proxy_logger.warning("gc.freeze pinned %d startup objects", gc.get_freeze_count()) yield state finally: if model_info_scheduler is not None and model_info_scheduler.running: model_info_scheduler.remove_job("refresh_model_info") if model_info_scheduler is not scheduler: model_info_scheduler.shutdown(wait=False) # Shutdown event - stop starting scheduled jobs; the ones already running keep the drain window if scheduler is not None: pause_scheduled_jobs(scheduler) # Shutdown event - drain in-flight requests before tearing down dependencies # so SIGTERM (rolling update, scale-down, liveness kill) doesn't drop them. GracefulShutdownManager.start_shutdown() await GracefulShutdownManager.wait_for_drain() finally: # Shutdown event - close shared aiohttp session if shared_aiohttp_session is not None: try: await shared_aiohttp_session.close() verbose_proxy_logger.info("SESSION REUSE: Closed shared aiohttp session") except Exception as e: verbose_proxy_logger.error("Error closing shared aiohttp session: %s", e) await _drain_spend_event_producer_on_shutdown() if telemetry_start is not None: telemetry_start.cancel() await asyncio.gather(telemetry_start, return_exceptions=True) await telemetry_runtime.stop() # Shutdown event - finish or cancel in-flight scheduled jobs before the shutdown flushes and the DB disconnect if scheduler is not None and scheduler_executor is not None: try: await stop_in_flight_scheduler_jobs(scheduler, scheduler_executor) except Exception as e: verbose_proxy_logger.error("Error stopping in-flight scheduled jobs: %s", e) await _flush_spend_around_logging_drain_on_shutdown() await close_proxy_callbacks( (*startup_callbacks, *litellm.logging_callback_manager.get_all_callbacks()), timeout=GracefulShutdownManager.remaining(CALLBACK_CLOSE_TIMEOUT_SECONDS), ) await proxy_config.stop_config_sync_subscriber() proxy_config.stop_cache_invalidation_subscriber() await proxy_shutdown_event(worker_heartbeat=worker_heartbeat) if prometheus_multiproc_dir: mark_worker_exit(os.getpid()) def _generate_stable_operation_id(route: "APIRoute") -> str: operation_id = re.sub(r"\W", "_", f"{route.name}{route.path_format}") route_methods: Final = sorted(route.methods or []) if len(route_methods) == 1: operation_id = f"{operation_id}_{route_methods[0].lower()}" return operation_id _OPENAPI_HTTP_METHODS: Final = { "delete", "get", "head", "options", "patch", "post", "put", "trace", } # Credentials surfaced by `/get/config/callbacks` in the alerting block: the # full Slack incoming-webhook URL is itself a credential, and the SMTP # password is a service password. Masked on read so plaintext never reaches # the UI. Kept here at module scope to match the analogous descriptor # `is_secret` flags in litellm.proxy.config_resolvers and the # `_CACHE_SENSITIVE_FIELDS` constant in the cache endpoint file. _ALERTING_SENSITIVE_VARS: Final[set[str]] = {"ALERTING_WEBHOOK_URL", "SLACK_WEBHOOK_URL", "SMTP_PASSWORD"} def _strip_operation_id_method_suffix(operation_id: str) -> str: base, separator, suffix = operation_id.rpartition("_") if separator and suffix in _OPENAPI_HTTP_METHODS: return base return operation_id class _SupportsValues(Protocol): def values(self) -> Iterable[JsonValue]: ... def _openapi_path_items(openapi_schema: JsonObject) -> Iterable[JsonObject]: paths: Final = require_attributes(openapi_schema.get("paths", {}), _SupportsValues, "values") return (path_item for path_item in paths.values() if isinstance(path_item, dict)) def ensure_unique_openapi_operation_ids( openapi_schema: JsonObject, reserved_operation_ids: set[str] | None = None, ) -> JsonObject: operation_entries: Final = [] operation_id_counts: Final[dict[str, int]] = {} for path_item in _openapi_path_items(openapi_schema): for method, operation in path_item.items(): if method not in _OPENAPI_HTTP_METHODS or not isinstance(operation, dict): continue operation_id = operation.get("operationId") if not isinstance(operation_id, str): continue operation_entries.append((method, operation, operation_id)) operation_id_counts[operation_id] = operation_id_counts.get(operation_id, 0) + 1 used_operation_ids: Final = set(reserved_operation_ids or set()) seen_operation_ids: Final[set[str]] = set() for method, operation, operation_id in operation_entries: should_rewrite = ( operation_id_counts[operation_id] > 1 or operation_id in used_operation_ids or operation_id in seen_operation_ids ) if not should_rewrite: seen_operation_ids.add(operation_id) used_operation_ids.add(operation_id) continue base_operation_id = _strip_operation_id_method_suffix(operation_id) new_operation_id = f"{base_operation_id}_{method}" suffix = 2 while new_operation_id in used_operation_ids or new_operation_id in seen_operation_ids: new_operation_id = f"{base_operation_id}_{method}_{suffix}" suffix += 1 operation["operationId"] = new_operation_id seen_operation_ids.add(new_operation_id) used_operation_ids.add(new_operation_id) if reserved_operation_ids is not None: reserved_operation_ids.update(used_operation_ids) return openapi_schema app = FastAPI( docs_url=get_docs_url(), redoc_url=get_redoc_url(), openapi_url=get_openapi_url(), title=title, description=_description, version=version, root_path=server_root_path, lifespan=proxy_startup_event, generate_unique_id_function=_generate_stable_operation_id, strict_content_type=False, ) ## V2 OTEL: instrument the FastAPI app for server spans (gated by ## LITELLM_OTEL_V2). This MUST run at app-creation time — once the lifespan runs, ## the middleware stack is frozen and ``instrument_app`` raises "Cannot add ## middleware after an application has started". See ## ``litellm.integrations.otel.mount`` for the full rationale; the call is a safe ## no-op when the gate is off or the instrumentation package is unavailable. from litellm.integrations.otel.mount import ( instrument_fastapi_app, trace_response_expose_headers, trace_response_headers, ) instrument_fastapi_app(app) vertex_live_passthrough_vertex_base: Final = VertexBase() ### CUSTOM API DOCS [ENTERPRISE FEATURE] ### # Custom OpenAPI schema generator to include only selected routes from fastapi.routing import APIWebSocketRoute from starlette.routing import BaseRoute from litellm.proxy.common_utils.openapi_route import OpenAPISchema, serve_openapi_off_the_event_loop class _OpenAPIQueryParam(Protocol): @property def name(self) -> object: ... @property def required(self) -> object: ... def _query_param_name_and_required(param: object) -> tuple[object, object]: fields: Final = require_attributes(param, _OpenAPIQueryParam, "name", "required") return fields.name, fields.required def _inject_websocket_stubs_into_openapi_schema(openapi_schema: dict, websocket_routes: list) -> dict: """ Add a synthetic GET stub for each WebSocket route so it appears in Swagger UI. Merges into any existing path entry rather than replacing it — a WebSocket route that shares its path with an HTTP route must not erase the HTTP operation. If a "get" operation is already documented on the path, the WebSocket stub is skipped to preserve the real GET. """ for route in websocket_routes: base_path = route.path.split("{")[0].rstrip("?") parameters = [] try: if hasattr(route, "dependant") and route.dependant is not None: # Handle both FastAPI <0.120 and >=0.120 query_params = _UNTYPED_VALUE.validate_python(getattr(route.dependant, "query_params", [])) if query_params: for param in iter_items(query_params): name, required = _query_param_name_and_required(param) parameters.append( { "name": name, "in": "query", "required": required, "schema": {"type": "string"}, } ) except (AttributeError, TypeError): pass path_entry = openapi_schema["paths"].setdefault(base_path, {}) if "get" not in path_entry: path_entry["get"] = { "summary": f"WebSocket: {route.name or base_path}", "description": "WebSocket connection endpoint", "operationId": f"websocket_{route.name or base_path.replace('/', '_')}", "parameters": parameters, "responses": {"101": {"description": "WebSocket Protocol Switched"}}, "tags": ["WebSocket"], } return openapi_schema @dataclass(frozen=True, slots=True) class _OpenAPIInputs: routes: tuple[BaseRoute, ...] loaded_lazy_modules: frozenset[str] def _openapi_inputs() -> _OpenAPIInputs: from litellm.proxy._lazy_features import loaded_lazy_modules return _OpenAPIInputs(routes=tuple(app.routes), loaded_lazy_modules=loaded_lazy_modules(app)) def _build_openapi_schema(inputs: _OpenAPIInputs) -> OpenAPISchema: # Use compatibility wrapper for FastAPI 0.120+ schema generation from litellm.proxy.common_utils.openapi_schema_compat import ( get_openapi_schema_with_compat, ) openapi_schema = get_openapi_schema_with_compat( get_openapi_func=get_openapi, title=app.title, version=app.version, description=app.description, routes=list(inputs.routes), ) # Find all WebSocket routes websocket_routes: Final = [route for route in inputs.routes if isinstance(route, APIWebSocketRoute)] # Add a synthetic GET stub for each so they render in Swagger UI, # without clobbering existing HTTP operations on the same path. openapi_schema = _inject_websocket_stubs_into_openapi_schema(openapi_schema, websocket_routes) # Add LLM API request schema bodies for documentation from litellm.proxy.common_utils.custom_openapi_spec import CustomOpenAPISpec openapi_schema = CustomOpenAPISpec.add_llm_api_request_schema_body(openapi_schema) # Stub unloaded lazy features so they appear as Swagger sections. from litellm.proxy._lazy_features import inject_lazy_stubs openapi_schema = inject_lazy_stubs(openapi_schema, inputs.loaded_lazy_modules) openapi_schema = ensure_unique_openapi_operation_ids(openapi_schema) # Fix Swagger UI execute path error when server_root_path is set if server_root_path: openapi_schema["servers"] = [{"url": "/" + server_root_path.strip("/")}] return openapi_schema def get_openapi_schema(): if app.openapi_schema: return app.openapi_schema app.openapi_schema = _build_openapi_schema(_openapi_inputs()) return app.openapi_schema def _build_docs_filtered_openapi_schema(inputs: _OpenAPIInputs) -> OpenAPISchema: openapi_schema = _build_openapi_schema(inputs) all_paths: Final = openapi_schema.get("paths") openapi_schema["paths"] = ( {route: all_paths[route] for route in LiteLLMRoutes.openai_routes.value if route in all_paths} if isinstance(all_paths, dict) else {} ) # Add LLM API request schema bodies for documentation from litellm.proxy.common_utils.custom_openapi_spec import CustomOpenAPISpec openapi_schema = CustomOpenAPISpec.add_llm_api_request_schema_body(openapi_schema) # Stub unloaded lazy features so they appear as Swagger sections. from litellm.proxy._lazy_features import inject_lazy_stubs openapi_schema = inject_lazy_stubs(openapi_schema, inputs.loaded_lazy_modules) openapi_schema = ensure_unique_openapi_operation_ids(openapi_schema) # Fix Swagger UI execute path error when server_root_path is set if server_root_path: openapi_schema["servers"] = [{"url": "/" + server_root_path.strip("/")}] return openapi_schema def custom_openapi(): if app.openapi_schema: return app.openapi_schema app.openapi_schema = _build_docs_filtered_openapi_schema(_openapi_inputs()) return app.openapi_schema if os.getenv("DOCS_FILTERED", "False") == "True" and premium_user: app.openapi = custom_openapi serve_openapi_off_the_event_loop(app, _openapi_inputs, _build_docs_filtered_openapi_schema) else: # For regular users, use get_openapi_schema to include LLM API schemas app.openapi = get_openapi_schema serve_openapi_off_the_event_loop(app, _openapi_inputs, _build_openapi_schema) class UserAPIKeyCacheTTLEnum(enum.Enum): in_memory_cache_ttl = 60 # 1 min ttl ## configure via `general_settings::user_api_key_cache_ttl: ` @app.exception_handler(ProxyException) async def openai_exception_handler(request: Request, exc: ProxyException): # NOTE: DO NOT MODIFY THIS, its crucial to map to Openai exceptions log_model_access_denial(exc) headers: Final = exc.headers if uses_anthropic_error_envelope(request): return anthropic_error_json_response(exc, request) error_dict: Final = with_call_id( JSON_OBJECT.validate_python(exc.to_dict()), error_body_call_id(general_settings_view(), headers.get(LITELLM_CALL_ID_HEADER)), ) status_code: Final = int(exc.code) if exc.code else status.HTTP_500_INTERNAL_SERVER_ERROR _close_dangling_otel_server_span(request, status_code, exc=exc) otlp_response: Final = tracing_endpoints.otlp_error_response(request, status_code, headers) if otlp_response is not None: return otlp_response return JSONResponse( status_code=status_code, content={"error": error_dict}, headers=headers, ) @app.exception_handler(AwsErrorTypeProxyException) async def aws_error_type_exception_handler(request: Request, exc: AwsErrorTypeProxyException) -> Response: """The ProxyException response plus the ``x-amzn-errortype`` header and top-level ``message`` an AWS SDK reads.""" response: Final = await openai_exception_handler(request, exc.error) return JSONResponse( status_code=response.status_code, content={"message": exc.error.message, **JSON_OBJECT.validate_json(bytes(response.body))}, headers={**exc.error.headers, AWS_ERROR_TYPE_HEADER: exc.aws_error_type}, ) @app.exception_handler(StarletteHTTPException) async def otlp_http_exception_handler(connection: Request | WebSocket, exc: StarletteHTTPException) -> Response | None: if isinstance(connection, WebSocket): return await _websocket_http_exception_response(connection, exc) response: Final = tracing_endpoints.otlp_error_response(connection, exc.status_code, exc.headers) if response is not None: _close_dangling_otel_server_span(connection, exc.status_code, exc=exc) return response if uses_anthropic_error_envelope(connection): return anthropic_error_json_response( proxy_exception_from_http_exception(exc, dict(exc.headers or {})), connection ) return await http_exception_handler(connection, exc) async def _websocket_http_exception_response(websocket: WebSocket, exc: StarletteHTTPException) -> Response | None: state: Final = websocket.application_state match state: case WebSocketState.CONNECTING: return JSONResponse({"detail": exc.detail}, status_code=exc.status_code, headers=exc.headers) case WebSocketState.CONNECTED: await websocket.close(code=_websocket_close_code(exc.status_code)) return None case WebSocketState.RESPONSE | WebSocketState.DISCONNECTED: return None case _: assert_never(state) def _websocket_close_code(status_code: int) -> int: return status.WS_1011_INTERNAL_ERROR if status_code >= 500 else status.WS_1008_POLICY_VIOLATION def _close_dangling_otel_server_span(request: Request, status_code: int, exc: Exception | None = None) -> None: parent_otel_span: Final[_Span | None] = getattr(request.state, "parent_otel_span", None) if parent_otel_span is None: return if open_telemetry_logger is None: return # Under OTel V2 the FastAPI instrumentor owns the server span (parent_otel_span # is that same span) and ends it itself with the http.* attributes stamped on # completion. The instrumentor only records an error when the exception reaches # it uncaught, but these handlers swallow it into a JSONResponse, so it never # does; stamp the error.* attributes here (without ending or re-statusing the # span, which the instrumentor still owns) so pre-call failures carry the error # like v1 did. Otherwise close and annotate the dangling span ourselves. try: from litellm.integrations.otel.model.config import is_otel_v2_enabled v2_enabled = is_otel_v2_enabled() except Exception: v2_enabled = False try: from opentelemetry.trace import Status, StatusCode if v2_enabled: if status_code >= 400: open_telemetry_logger.record_error_attributes_on_span(parent_otel_span, exc, status_code) return open_telemetry_logger.set_response_status_code_attribute(parent_otel_span, status_code) parent_otel_span.set_status( Status( StatusCode.ERROR, open_telemetry_logger.record_error_attributes_on_span(parent_otel_span, exc, status_code), ) if status_code >= 400 else Status(StatusCode.OK) ) parent_otel_span.end() except Exception as e: verbose_proxy_logger.debug("Error closing dangling OTEL SERVER span: %s", str(e)) finally: if not v2_enabled: request.state.parent_otel_span = None @app.exception_handler(ManagementProblem) async def management_problem_exception_handler(request: Request, exc: ManagementProblem): _close_dangling_otel_server_span(request, exc.problem.status, exc=exc) return problem_response(exc.problem) @app.exception_handler(ScimError) async def scim_error_exception_handler(request: Request, exc: ScimError) -> JSONResponse: _close_dangling_otel_server_span(request, exc.status_code, exc=exc.traced_as or exc) return scim_error_response(exc) class _ConfigParamRow(Protocol): param_name: str param_value: Mapping[str, JsonValue] | None class _ConfigParamValueRow(Protocol): @property def param_value(self) -> object: ... class _ConfigOverridesRow(Protocol): config_value: Mapping[str, JsonValue] | None class _SSOConfigRow(Protocol): sso_settings: MutableMapping[str, object] class _InvitationLinkRow(Protocol): user_id: str expires_at: datetime is_accepted: bool accepted_at: datetime | None created_by: str class _UserTableRow(Protocol): user_id: str user_email: str | None user_role: str | None class _UserTeamsRow(Protocol): @property def teams(self) -> Sequence[str]: ... class _InvitationAcceptTx(Protocol): @property def litellm_invitationlink(self) -> TableActions[_InvitationLinkRow]: ... @property def litellm_usertable(self) -> TableActions[_UserTableRow]: ... class _InvitationAcceptTxManager(Protocol): async def __aenter__(self) -> _InvitationAcceptTx: ... async def __aexit__( self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, /, ) -> bool | None: ... _ProxyModelRow: TypeAlias = "prisma_models.LiteLLM_ProxyModelTable" def _config_param_table(client: PrismaClient | None, *, use_writer: bool = False) -> TableActions[_ConfigParamRow]: return cast( # cast-ok: this is prisma's LiteLLM_Config actions object, which parses its Json column to a mapping "TableActions[_ConfigParamRow]", ConfigRepository(client, use_writer=use_writer).table ) async def _query_raw_rows( prisma_client: PrismaClient, sql_query: str, *args: object ) -> Sequence[Mapping[str, object]] | None: return await query_raw_rows(prisma_client.db, sql_query, *args) class _KeysAndGetItem(Protocol): def keys(self) -> Iterable[object]: ... def __getitem__(self, key: object, /) -> object: ... class _SupportsItems(Protocol): def items(self) -> Iterable[tuple[object, object]]: ... def _dict_update_source(value: object) -> _KeysAndGetItem: if has_attributes(value, _KeysAndGetItem, "keys"): return value return dict_from_pairs(value) @app.exception_handler(RequestValidationError) async def otel_request_validation_exception_handler(request: Request, exc: RequestValidationError): public_errors: Final = public_validation_errors(exc.errors()) public_exc: Final = RequestValidationError(public_errors).with_traceback(exc.__traceback__) if request.url.path.startswith(MANAGEMENT_V1_PREFIX): problem: Final = request_validation_problem(public_errors) _close_dangling_otel_server_span(request, problem.status, exc=public_exc) return problem_response(problem) _close_dangling_otel_server_span(request, 422, exc=public_exc) otlp_response: Final = tracing_endpoints.otlp_error_response(request, 422) if otlp_response is not None: return otlp_response return JSONResponse(status_code=422, content={"detail": public_errors}) def _include_litellm_headers() -> bool: return litellm.include_litellm_headers is not False @app.exception_handler(Exception) async def otel_unhandled_exception_handler(request: Request, exc: Exception): if isinstance(exc, (ProxyException, HTTPException, RequestValidationError)): raise exc response: Final = await _unhandled_exception_response(request, exc) response.headers.update(trace_response_headers(getattr(request.state, "parent_otel_span", None))) litellm_call_id: Final = attribute_of(request.state, "litellm_call_id") if isinstance(litellm_call_id, str): response.headers.setdefault(LITELLM_CALL_ID_HEADER, litellm_call_id) if not _include_litellm_headers(): response.raw_headers[:] = [ (name, value) for name, value in response.raw_headers if not name.lower().startswith(b"x-litellm-") ] return response async def _unhandled_exception_response(request: Request, exc: Exception) -> Response: if PrismaDBExceptionHandler.is_database_service_unavailable_error_in_chain(exc): verbose_proxy_logger.warning("Database unavailable during request: %s", type(exc).__name__) return await openai_exception_handler( request=request, exc=PrismaDBExceptionHandler.service_unavailable_proxy_exception(exc) ) verbose_proxy_logger.exception("Unhandled exception in request: %s", type(exc).__name__) if should_report_bug(exc): verbose_proxy_logger.error( bug_report_notice( build_proxy_bug_report( exc, call_type=allowlisted(request.url.path, KNOWN_PROXY_ROUTES), ) ) ) _close_dangling_otel_server_span(request, 500, exc=exc) otlp_response: Final = tracing_endpoints.otlp_error_response(request, 500) if otlp_response is not None: return otlp_response if uses_anthropic_error_envelope(request): return anthropic_internal_server_error_response(request) return JSONResponse( status_code=500, content={ "error": { "message": "Internal server error", "type": "internal_server_error", } }, ) router: Final = APIRouter() def _get_cors_config( cors_origins_env: str | None = None, cors_credentials_env: str | None = None, ): """ Compute CORS allowed origins and credentials flag from environment variables. Extracted into a function so it can be unit-tested without reloading the module. Args: cors_origins_env: Value of LITELLM_CORS_ORIGINS (defaults to os.getenv). cors_credentials_env: Value of LITELLM_CORS_ALLOW_CREDENTIALS (defaults to os.getenv). Returns: Tuple[List[str], bool]: (origins, allow_credentials) """ _origins_raw: Final = cors_origins_env if cors_origins_env is not None else os.getenv("LITELLM_CORS_ORIGINS") if _origins_raw is None or _origins_raw.strip() == "": computed_origins = ["*"] else: computed_origins = [o.strip() for o in _origins_raw.split(",") if o.strip()] # Disable credentials by default when wildcard origins are used — combining # allow_origins=["*"] with allow_credentials=True causes Starlette to reflect # the incoming Origin header, allowing any site to make credentialed requests. # Set LITELLM_CORS_ALLOW_CREDENTIALS=true to explicitly restore the old behaviour # (e.g. for non-browser clients that relied on the Access-Control-Allow-Credentials # header being present regardless of origin). _credentials_raw: Final = ( cors_credentials_env if cors_credentials_env is not None else os.getenv("LITELLM_CORS_ALLOW_CREDENTIALS") ) if _credentials_raw is not None: computed_credentials = _credentials_raw.strip().lower() == "true" else: computed_credentials = "*" not in computed_origins return computed_origins, computed_credentials origins, allow_cors_credentials = _get_cors_config() # get current directory try: current_dir = os.path.dirname(os.path.abspath(__file__)) packaged_ui_path: Final = os.path.join(current_dir, "_experimental", "out") ui_path = packaged_ui_path litellm_asset_prefix: Final = "/litellm-asset-prefix" def _dir_has_content(path: str) -> bool: try: return os.path.isdir(path) and any(os.scandir(path)) except FileNotFoundError: return False def _validate_ui_directory(ui_path: str) -> bool: """ Verify UI directory has minimum required structure. Checks for: - Directory exists - Has index.html (main entry point) - Has _next directory (Next.js assets) Returns True if UI directory appears valid and servable. """ if not os.path.isdir(ui_path): return False # Must have main index.html if not os.path.exists(os.path.join(ui_path, "index.html")): return False # Must have _next directory with Next.js assets next_dir: Final = os.path.join(ui_path, "_next") if not os.path.isdir(next_dir): return False return True def _is_ui_pre_restructured(ui_dir: str) -> bool: """ Detect if UI directory is already pre-restructured and ready to serve. Returns True if: 1. Marker file .litellm_ui_ready exists (created by Dockerfile), OR 2. Restructuring pattern detected (subdirectories with index.html inside) This allows skipping copy/restructure operations on read-only filesystems. """ if not os.path.isdir(ui_dir): return False # Primary signal: marker file created by Dockerfile marker_file: Final = os.path.join(ui_dir, ".litellm_ui_ready") if os.path.exists(marker_file): verbose_proxy_logger.debug("Found UI ready marker: %s", marker_file) return True # Fallback signal: Detect restructuring pattern # After restructuring, routes exist as directories with index.html inside # (e.g., login/index.html instead of login.html) # Check for main index.html first (basic UI structure requirement) if not os.path.exists(os.path.join(ui_dir, "index.html")): return False # Look for ANY subdirectory with index.html (proves restructuring happened) # Ignore directories starting with _ (Next.js internals like _next) try: for entry in os.scandir(ui_dir): if entry.is_dir() and not entry.name.startswith("_"): index_path = os.path.join(entry.path, "index.html") if os.path.exists(index_path): # Found at least one restructured route - this proves the pattern verbose_proxy_logger.debug( "Detected restructured UI via pattern: found %s/index.html", entry.name ) return True except (PermissionError, OSError) as e: verbose_proxy_logger.debug("Could not scan %s for restructuring detection: %s", ui_dir, e) return False # No restructured routes found return False def _try_populate_ui_directory(source_path: str, target_path: str) -> tuple[bool, str]: """ Attempt to populate target UI directory from source. Returns: (success: bool, error_message: str) """ try: os.makedirs(target_path, exist_ok=True) if not _dir_has_content(target_path) and _dir_has_content(source_path): shutil.copytree( source_path, target_path, dirs_exist_ok=True, ) verbose_proxy_logger.info("Successfully populated UI at %s", target_path) return True, "" else: return False, "Source or target directory state invalid" except (PermissionError, OSError) as e: return False, str(e) # Use a writable runtime UI directory whenever possible. # This prevents mutating the packaged UI directory (e.g. site-packages or the repo checkout) # and ensures extensionless routes like /ui/login work via /index.html. is_non_root: Final = os.getenv("LITELLM_NON_ROOT", "").lower() == "true" # Determine runtime UI path # Priority: LITELLM_UI_PATH env var > default path based on is_non_root if is_non_root: default_runtime_ui_path = "/var/lib/litellm/ui" else: default_runtime_ui_path = packaged_ui_path runtime_ui_path: Final = os.getenv("LITELLM_UI_PATH", default_runtime_ui_path) # Validate packaged UI before proceeding if not _validate_ui_directory(packaged_ui_path): verbose_proxy_logger.error( "Packaged UI at %s is invalid or incomplete. UI may not function correctly.", packaged_ui_path ) # Decision tree for UI path selection: # 1. If runtime path == packaged path: use packaged UI directly # 2. If runtime UI exists and is pre-restructured: use it # 3. If runtime UI exists but not restructured: use it (will restructure later) # 4. If runtime UI missing: try to populate from packaged UI # 4a. If population succeeds: use runtime UI # 4b. If population fails: fall back to packaged UI should_use_runtime_path: Final = runtime_ui_path != packaged_ui_path if should_use_runtime_path: is_pre_restructured = _is_ui_pre_restructured(runtime_ui_path) has_content: Final = _dir_has_content(runtime_ui_path) # Case 2: Runtime UI exists and is ready if has_content and is_pre_restructured: verbose_proxy_logger.info("Using pre-restructured UI at %s", runtime_ui_path) ui_path = runtime_ui_path # Case 3: Runtime UI exists but needs restructuring elif has_content and not is_pre_restructured: verbose_proxy_logger.warning( "UI at %s has content but is not properly restructured. Will attempt to restructure in place.", runtime_ui_path, ) ui_path = runtime_ui_path # Case 4: Runtime UI missing - try to populate else: verbose_proxy_logger.info("UI not found at %s. Attempting to populate from packaged UI.", runtime_ui_path) success, error = _try_populate_ui_directory(packaged_ui_path, runtime_ui_path) if success: # Case 4a: Population succeeded ui_path = runtime_ui_path else: # Case 4b: Population failed - fall back to packaged UI verbose_proxy_logger.warning( "Failed to populate UI at %s: %s. Falling back to packaged UI at %s. For read-only deployments, pre-build UI in Dockerfile or set LITELLM_UI_PATH to a writable emptyDir volume.", runtime_ui_path, error, packaged_ui_path, ) ui_path = packaged_ui_path else: # Case 1: Using packaged UI directly (local development) verbose_proxy_logger.info("Using packaged UI directory: %s", packaged_ui_path) ui_path = packaged_ui_path # Validate final UI path if not _validate_ui_directory(ui_path): verbose_proxy_logger.error("Selected UI path %s is invalid or incomplete. UI may not work correctly.", ui_path) # Only modify files if a custom server root path is set AND filesystem is writable if server_root_path and server_root_path != "/": # Check if UI path is writable is_writable = os.access(ui_path, os.W_OK) if not is_writable: verbose_proxy_logger.warning( "Cannot apply server_root_path replacements to UI at %s: path is not writable. Ensure server_root_path is '/' or pre-process UI files in Dockerfile with custom server_root_path.", ui_path, ) else: # Iterate through files in the UI directory for root, dirs, files in os.walk(ui_path): for filename in files: file_path = os.path.join(root, filename) # Skip binary files and files that don't need path replacement if filename.endswith( ( ".png", ".jpg", ".jpeg", ".gif", ".ico", ".woff", ".woff2", ".ttf", ".eot", ) ): continue try: with open(file_path, "r", encoding="utf-8") as f: content = f.read() # Replace the asset prefix with the server root path modified_content = content.replace( f"{litellm_asset_prefix}", f"{server_root_path}", ) # Replace the /.well-known/litellm-ui-config with the server root path modified_content = modified_content.replace( "/litellm/.well-known/litellm-ui-config", f"{server_root_path}/.well-known/litellm-ui-config", ) with open(file_path, "w", encoding="utf-8") as f: f.write(modified_content) except (UnicodeDecodeError, PermissionError, OSError): # Skip binary files or files we can't write to continue # # Mount the _next directory at the root level app.mount( "/_next", StaticFiles(directory=os.path.join(ui_path, "_next")), name="next_static", ) app.mount( f"{litellm_asset_prefix}/_next", StaticFiles(directory=os.path.join(ui_path, "_next")), name="next_static", ) # print(f"mounted _next at {server_root_path}/ui/_next") app.mount("/ui", StaticFiles(directory=ui_path, html=True), name="ui") def _restructure_ui_html_files(ui_root: str) -> None: """Ensure each exported HTML route is available as /index.html.""" for current_root, _, files in os.walk(ui_root): rel_root = os.path.relpath(current_root, ui_root) first_segment = "" if rel_root == "." else rel_root.split(os.sep)[0] # Ignore Next.js asset directories if first_segment in {"_next", "litellm-asset-prefix"}: continue for filename in files: if not filename.endswith(".html") or filename == "index.html": continue file_path = os.path.join(current_root, filename) target_dir = os.path.splitext(file_path)[0] target_path = os.path.join(target_dir, "index.html") os.makedirs(target_dir, exist_ok=True) try: os.replace(file_path, target_path) except FileNotFoundError: # Another process may have already moved this file. continue # Handle HTML file restructuring # Only restructure if: # 1. UI is not already pre-restructured # 2. Filesystem is writable try: is_pre_restructured = _is_ui_pre_restructured(ui_path) is_writable = os.access(ui_path, os.W_OK) if is_pre_restructured: verbose_proxy_logger.info("Skipping UI restructuring: %s is already pre-restructured", ui_path) elif not is_writable: verbose_proxy_logger.warning( "Cannot restructure UI at %s: path is not writable. UI may not work correctly for extensionless routes. Pre-build and restructure UI in Dockerfile for read-only deployments.", ui_path, ) else: _restructure_ui_html_files(ui_path) verbose_proxy_logger.info("Restructured UI directory: %s", ui_path) except PermissionError as e: verbose_proxy_logger.exception("Permission error while restructuring UI directory %s: %s", ui_path, e) except Exception as e: verbose_proxy_logger.exception("Error while restructuring UI directory %s: %s", ui_path, e) except Exception: pass current_dir = os.path.dirname(os.path.abspath(__file__)) # ui_path = os.path.join(current_dir, "_experimental", "out") # # Mount this test directory instead # app.mount("/ui", StaticFiles(directory=ui_path, html=True), name="ui") def configured_max_request_size_mb() -> int | float | None: return MAX_REQUEST_SIZE_MB_SETTING.validate_python(general_settings.get("max_request_size_mb")) _ROUTE_MAX_REQUEST_SIZE_MB: Final[Mapping[tuple[str, str], float]] = MappingProxyType( { ("POST", "/claude-code/plugins/upload"): 11, ("POST", "/skill-hub/skills/upload"): 11, ("PUT", "/skill-hub/skills/{name}/upload"): 11, ("PUT", "/skill-hub/submissions/{submission_id}/upload"): 11, } ) app.add_middleware( RequestBodyDecompressionMiddleware, get_max_request_size_mb=lambda: configured_max_request_size_mb() if premium_user is True else None, route_max_request_size_mb=_ROUTE_MAX_REQUEST_SIZE_MB, ) app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=allow_cors_credentials, allow_methods=["*"], allow_headers=["*"], expose_headers=[*LITELLM_UI_ALLOW_HEADERS, *trace_response_expose_headers()], ) app.add_middleware(PrometheusAuthMiddleware) # Added before InFlightRequestsMiddleware so it nests *inside* it: Starlette # makes the last-added middleware outermost. The billable count is recorded # after the inner app returns, so if this sat outside the in-flight tracker a # request could be counted as drained while its record() had not yet run, and # proxy_shutdown_event could flush and stop the exporter underneath it. app.add_middleware( BillableRequestMetricsMiddleware, # Factory, not an instance: the recorder is resolved on the first request so # it sees premium_user and the billing env vars AFTER proxy_startup_event has # loaded the YAML config's environment_variables. Building it here at import # time would permanently capture recorder=None for YAML-configured # deployments. The lambda reads the module globals at call time. recorder_factory=lambda: ( build_billing_metrics_recorder( premium=premium_user, # Read from the license check, not the premium_user_data module # global: that global is bound once at import and goes stale when # the license arrives via the YAML config's environment_variables. license_data=_license_check.airgapped_license_data, litellm_version=version, ) if build_billing_metrics_recorder is not None else None ), # Unlike the billing recorder this is not license-gated: the admin UI must # report SGR on any deployment. Gated only on a database being configured, # since without one the fold would never be drained. Read at call time, so # it sees prisma_client as of the first request rather than import time. sink_factory=lambda: gateway_request_accumulator if prisma_client is not None else None, ) telemetry_runtime: Final = TelemetryRuntime() def _log_telemetry_start_failure(task: asyncio.Task[None]) -> None: error: Final = None if task.cancelled() else task.exception() if error is not None: verbose_proxy_logger.warning("telemetry: failed to start, leaving it off: %s", error) app.add_middleware( TelemetryMiddleware, sink_provider=lambda: telemetry_runtime.sink, spawn=telemetry_runtime.spawn, settle_timeout_s=lambda: telemetry_runtime.settings.settle_timeout_seconds, ) app.add_middleware(BudgetReservationReleaseMiddleware, release=release_unbound_budget_reservation) app.add_middleware(RedisRequestBatchMiddleware) app.add_middleware(InFlightRequestsMiddleware) app.add_middleware(SecurityHeadersMiddleware) app.add_middleware(GZipBufferedResponseMiddleware) def mount_swagger_ui(): swagger_directory: Final = os.path.join(current_dir, "swagger") swagger_path = "/" if server_root_path is None else server_root_path if not swagger_path.endswith("/"): swagger_path = swagger_path + "/" custom_root_path_swagger_path: Final = swagger_path + "swagger" app.mount("/swagger", StaticFiles(directory=swagger_directory), name="swagger") # On dropdown expand: one-time fetch to the prefix (triggers lazy load), # then spec re-download so real routes replace the stub. Raw JS (no #