From e9d33c4d027eb4119d753bc2d1ceabda7c2713ee Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Mon, 2 Jun 2025 10:41:59 -0700 Subject: [PATCH] fix(ui/): working custom server root path for login --- litellm/proxy/proxy_server.py | 13 ++++++++----- litellm/proxy/utils.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index dec18388800..7b4fe0840ff 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -309,6 +309,7 @@ from litellm.proxy.utils import ( _get_redoc_url, _is_projected_spend_over_limit, _is_valid_team_configs, + get_custom_url, get_error_message_str, hash_token, update_spend, @@ -6586,8 +6587,10 @@ async def fallback_login(request: Request): PROXY_BASE_URL should be the your deployed proxy endpoint, e.g. PROXY_BASE_URL="https://litellm-production-7002.up.railway.app/" Example: """ + from litellm.proxy.proxy_server import ui_link + # get url from request - redirect_url = os.getenv("PROXY_BASE_URL", str(request.base_url)) + redirect_url = get_custom_url(str(request.base_url)) ui_username = os.getenv("UI_USERNAME") if redirect_url.endswith("/"): redirect_url += "sso/callback" @@ -6711,7 +6714,7 @@ async def login(request: Request): # noqa: PLR0915 code=status.HTTP_500_INTERNAL_SERVER_ERROR, ) key = response["token"] # type: ignore - litellm_dashboard_ui = os.getenv("PROXY_BASE_URL", "") + litellm_dashboard_ui = get_custom_url(str(request.base_url)) if litellm_dashboard_ui.endswith("/"): litellm_dashboard_ui += "ui/" else: @@ -6812,7 +6815,7 @@ async def login(request: Request): # noqa: PLR0915 code=status.HTTP_500_INTERNAL_SERVER_ERROR, ) key = response["token"] # type: ignore - litellm_dashboard_ui = os.getenv("PROXY_BASE_URL", "") + litellm_dashboard_ui = get_custom_url(str(request.base_url)) if litellm_dashboard_ui.endswith("/"): litellm_dashboard_ui += "ui/" else: @@ -6858,7 +6861,7 @@ async def login(request: Request): # noqa: PLR0915 @app.get("/onboarding/get_token", include_in_schema=False) -async def onboarding(invite_link: str): +async def onboarding(invite_link: str, request: Request): """ - Get the invite link - Validate it's still 'valid' @@ -6938,7 +6941,7 @@ async def onboarding(invite_link: str): ) key = response["token"] # type: ignore - litellm_dashboard_ui = os.getenv("PROXY_BASE_URL", "") + litellm_dashboard_ui = get_custom_url(str(request.base_url)) if litellm_dashboard_ui.endswith("/"): litellm_dashboard_ui += "ui/onboarding" else: diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 56f553852f8..e50df2743af 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -2845,3 +2845,21 @@ def is_known_model(model: Optional[str], llm_router: Optional[Router]) -> bool: is_in_list = True return is_in_list + + +def get_custom_url(request_base_url: str) -> str: + """ + Use proxy base url, if set. + + Else, use request base url. + """ + from httpx import URL + + proxy_base_url = os.getenv("PROXY_BASE_URL") + server_root_path = os.getenv("SERVER_ROOT_PATH") or "" + if proxy_base_url: + ui_link = str(URL(proxy_base_url).join(server_root_path)) + else: + ui_link = str(URL(request_base_url).join(server_root_path)) + + return ui_link