Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Fix Swagger UI path with server_root_path in OpenAPI schema (#18947)

xiaofanf8836cb2a7af
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 2e99a2f6824..a0485da8495 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -884,6 +884,10 @@ def get_openapi_schema():
from litellm.proxy.common_utils.custom_openapi_spec import CustomOpenAPISpec
openapi_schema = CustomOpenAPISpec.add_llm_api_request_schema_body(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("/")}]
app.openapi_schema = openapi_schema
return app.openapi_schema
@@ -906,6 +910,10 @@ def custom_openapi():
from litellm.proxy.common_utils.custom_openapi_spec import CustomOpenAPISpec
openapi_schema = CustomOpenAPISpec.add_llm_api_request_schema_body(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("/")}]
app.openapi_schema = openapi_schema
return app.openapi_schema
diff --git a/tests/test_litellm/proxy/test_swagger_chat_completions.py b/tests/test_litellm/proxy/test_swagger_chat_completions.py
index b973eab6213..968443ef4d7 100644
--- a/tests/test_litellm/proxy/test_swagger_chat_completions.py
+++ b/tests/test_litellm/proxy/test_swagger_chat_completions.py
@@ -307,4 +307,43 @@ class TestSwaggerChatCompletions:
# Verify required fields are present in test request
required_fields = schema_def.get("required", [])
for required_field in required_fields:
- assert required_field in test_request, f"Required field '{required_field}' should be in test request"
\ No newline at end of file
+ assert required_field in test_request, f"Required field '{required_field}' should be in test request"
+
+ def test_openapi_schema_servers_url_with_root_path(self):
+ """
+ Test that OpenAPI schema includes correct servers URL when server_root_path is set.
+ This ensures Swagger UI works correctly with reverse proxies and subpath deployments.
+ """
+ from unittest.mock import patch
+ from litellm.proxy.proxy_server import get_openapi_schema, custom_openapi, app
+
+ # Test cases: (server_root_path, expected_servers_url)
+ # Note: empty string is falsy in Python, so servers won't be set
+ test_cases = [
+ ("/litellm", "/litellm"),
+ ("/litellm/", "/litellm"), # trailing slash should be removed
+ ("litellm", "/litellm"), # missing leading slash should be added
+ ("/api/v1", "/api/v1"),
+ ]
+
+ for root_path, expected_url in test_cases:
+ # Clear cached schema
+ app.openapi_schema = None
+
+ with patch("litellm.proxy.proxy_server.server_root_path", root_path):
+ # Test get_openapi_schema
+ schema = get_openapi_schema()
+
+ # Should have servers field with correct URL
+ assert "servers" in schema, f"servers field should exist when server_root_path={root_path}"
+ assert schema["servers"][0]["url"] == expected_url, \
+ f"Expected servers URL '{expected_url}', got '{schema['servers'][0]['url']}' for root_path '{root_path}'"
+
+ # Test custom_openapi as well
+ app.openapi_schema = None
+ with patch("litellm.proxy.proxy_server.server_root_path", root_path):
+ schema = custom_openapi()
+
+ assert "servers" in schema, f"servers field should exist in custom_openapi when server_root_path={root_path}"
+ assert schema["servers"][0]["url"] == expected_url, \
+ f"Expected servers URL '{expected_url}' in custom_openapi, got '{schema['servers'][0]['url']}'"
\ No newline at end of file