add NO_OPENAPI env var to disable /openapi.json endpoint (#25547)

This commit is contained in:
Jonas Neubert 2026-04-13 20:29:59 -06:00 committed by Sameer Kankute
parent 17bfa420e4
commit e724e5e07d
No known key found for this signature in database
4 changed files with 38 additions and 0 deletions

View file

@ -914,6 +914,7 @@ router_settings:
| MODEL_COST_MAP_MAX_SHRINK_RATIO | Maximum allowed shrinkage ratio when validating a fetched model cost map against the local backup. Rejects the fetched map if it is smaller than this fraction of the backup. Default is 0.5
| MODEL_COST_MAP_MIN_MODEL_COUNT | Minimum number of models a fetched cost map must contain to be considered valid. Default is 50
| NO_DOCS | Flag to disable Swagger UI documentation
| NO_OPENAPI | Flag to disable the /openapi.json endpoint
| NO_REDOC | Flag to disable Redoc documentation
| NO_PROXY | List of addresses to bypass proxy
| NON_LLM_CONNECTION_TIMEOUT | Timeout in seconds for non-LLM service connections. Default is 15

View file

@ -493,6 +493,7 @@ from litellm.proxy.utils import (
ProxyUpdateSpend,
_cache_user_row,
_get_docs_url,
_get_openapi_url,
_get_projected_spend_over_limit,
_get_redoc_url,
_is_projected_spend_over_limit,
@ -1000,6 +1001,7 @@ async def proxy_startup_event(app: FastAPI): # noqa: PLR0915
app = FastAPI(
docs_url=_get_docs_url(),
redoc_url=_get_redoc_url(),
openapi_url=_get_openapi_url(),
title=_title,
description=_description,
version=version,

View file

@ -5321,6 +5321,19 @@ def get_error_message_str(e: Exception) -> str:
return error_message
def _get_openapi_url() -> Optional[str]:
"""
Get the OpenAPI schema URL from the environment variables.
- If NO_OPENAPI is True, return None.
- Otherwise, default to "/openapi.json".
"""
if str_to_bool(os.getenv("NO_OPENAPI")) is True:
return None
return "/openapi.json"
def _get_redoc_url() -> Optional[str]:
"""
Get the Redoc URL from the environment variables.

View file

@ -0,0 +1,22 @@
import pytest
from litellm.proxy.utils import _get_openapi_url
@pytest.mark.parametrize(
"env_vars, expected_url",
[
({}, "/openapi.json"), # default case
({"NO_OPENAPI": "True"}, None), # OpenAPI disabled
],
)
def test_get_openapi_url(monkeypatch, env_vars, expected_url):
# Clear relevant environment variables
monkeypatch.delenv("NO_OPENAPI", raising=False)
# Set test environment variables
for key, value in env_vars.items():
monkeypatch.setenv(key, value)
result = _get_openapi_url()
assert result == expected_url