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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: resolve Read-only file system error in non-root images (#19449)
Harshit Jain3c3148789ae4
diff --git a/litellm/litellm_core_utils/default_encoding.py b/litellm/litellm_core_utils/default_encoding.pyindex 41bfcbb63f4..1771efba410 100644--- a/litellm/litellm_core_utils/default_encoding.py+++ b/litellm/litellm_core_utils/default_encoding.py@@ -15,6 +15,13 @@ except (ImportError, AttributeError):__name__, "litellm_core_utils/tokenizers")+# Check if the directory is writable. If not, use /tmp as a fallback.+# This is especially important for non-root Docker environments where the package directory is read-only.+is_non_root = os.getenv("LITELLM_NON_ROOT", "").lower() == "true"+if not os.access(filename, os.W_OK) and is_non_root:+ filename = "/tmp/tiktoken_cache"+ os.makedirs(filename, exist_ok=True)+os.environ["TIKTOKEN_CACHE_DIR"] = os.getenv("CUSTOM_TIKTOKEN_CACHE_DIR", filename) # use local copy of tiktoken b/c of - https://github.com/BerriAI/litellm/issues/1071@@ -36,5 +43,5 @@ for attempt in range(_max_retries):# Last attempt, re-raise the exceptionraise# Exponential backoff with jitter to reduce collision probability- delay = _retry_delay * (2 ** attempt) + random.uniform(0, 0.1)+ delay = _retry_delay * (2**attempt) + random.uniform(0, 0.1)time.sleep(delay)diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex c3a4de314e5..8d306327dec 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -899,7 +899,7 @@ def get_openapi_schema():from litellm.proxy.common_utils.custom_openapi_spec import CustomOpenAPISpecopenapi_schema = CustomOpenAPISpec.add_llm_api_request_schema_body(openapi_schema)-+# Fix Swagger UI execute path error when server_root_path is setif server_root_path:openapi_schema["servers"] = [{"url": "/" + server_root_path.strip("/")}]@@ -925,7 +925,7 @@ def custom_openapi():from litellm.proxy.common_utils.custom_openapi_spec import CustomOpenAPISpecopenapi_schema = CustomOpenAPISpec.add_llm_api_request_schema_body(openapi_schema)-+# Fix Swagger UI execute path error when server_root_path is setif server_root_path:openapi_schema["servers"] = [{"url": "/" + server_root_path.strip("/")}]@@ -1116,8 +1116,13 @@ try:# In development, we restructure directly in _experimental/out.# In non-root Docker, we restructure in /var/lib/litellm/ui.try:- _restructure_ui_html_files(ui_path)- verbose_proxy_logger.info(f"Restructured UI directory: {ui_path}")+ if is_non_root and ui_path == "/var/lib/litellm/ui":+ verbose_proxy_logger.info(+ f"Skipping runtime UI restructuring for non-root Docker. UI at {ui_path} is pre-restructured."+ )+ else:+ _restructure_ui_html_files(ui_path)+ verbose_proxy_logger.info(f"Restructured UI directory: {ui_path}")except PermissionError as e:verbose_proxy_logger.exception(f"Permission error while restructuring UI directory {ui_path}: {e}"diff --git a/tests/test_default_encoding_non_root.py b/tests/test_default_encoding_non_root.pynew file mode 100644index 00000000000..1f22b7c69e0--- /dev/null+++ b/tests/test_default_encoding_non_root.py@@ -0,0 +1,49 @@+import os+from unittest.mock import patch+++def test_tiktoken_cache_fallback(monkeypatch):+ """+ Test that TIKTOKEN_CACHE_DIR falls back to /tmp/tiktoken_cache+ if the default directory is not writable and LITELLM_NON_ROOT is true.+ """+ # Simulate non-root environment+ monkeypatch.setenv("LITELLM_NON_ROOT", "true")+ monkeypatch.delenv("CUSTOM_TIKTOKEN_CACHE_DIR", raising=False)++ # Mock os.access to return False (not writable)+ # and mock os.makedirs to avoid actually creating /tmp/tiktoken_cache on local machine+ with patch("os.access", return_value=False), patch("os.makedirs"):+ # We need to reload or re-run the logic in default_encoding.py+ # But since it's already executed, we'll just test the logic directly+ # mirroring what we wrote in the file.++ filename = (+ "/usr/lib/python3.13/site-packages/litellm/litellm_core_utils/tokenizers"+ )+ is_non_root = os.getenv("LITELLM_NON_ROOT", "").lower() == "true"++ if not os.access(filename, os.W_OK) and is_non_root:+ filename = "/tmp/tiktoken_cache"+ # mock_makedirs(filename, exist_ok=True)++ assert filename == "/tmp/tiktoken_cache"+++def test_tiktoken_cache_no_fallback_if_writable(monkeypatch):+ """+ Test that TIKTOKEN_CACHE_DIR does NOT fall back if writable+ """+ monkeypatch.setenv("LITELLM_NON_ROOT", "true")++ filename = "/usr/lib/python3.13/site-packages/litellm/litellm_core_utils/tokenizers"++ with patch("os.access", return_value=True):+ is_non_root = os.getenv("LITELLM_NON_ROOT", "").lower() == "true"+ if not os.access(filename, os.W_OK) and is_non_root:+ filename = "/tmp/tiktoken_cache"++ assert (+ filename+ == "/usr/lib/python3.13/site-packages/litellm/litellm_core_utils/tokenizers"+ )diff --git a/tests/test_proxy_server_non_root.py b/tests/test_proxy_server_non_root.pynew file mode 100644index 00000000000..b632e922929--- /dev/null+++ b/tests/test_proxy_server_non_root.py@@ -0,0 +1,52 @@+from unittest.mock import patch+++def test_restructure_ui_html_files_skipped_in_non_root(monkeypatch):+ """+ Test that _restructure_ui_html_files is SKIPPED when:+ - LITELLM_NON_ROOT is "true"+ - ui_path is "/var/lib/litellm/ui"+ """+ # 1. Setup environment variables and variables+ monkeypatch.setenv("LITELLM_NON_ROOT", "true")++ # We need to simulate the execution of the module-level code or+ # just test the logic we added.++ is_non_root = True # Simulate the variable in proxy_server+ ui_path = "/var/lib/litellm/ui"++ # Mock the _restructure_ui_html_files function to check if it's called+ with patch(+ "litellm.proxy.proxy_server._restructure_ui_html_files"+ ) as mock_restructure:+ # Simulate the logic we added in proxy_server.py+ if is_non_root and ui_path == "/var/lib/litellm/ui":+ # Skipping...+ pass+ else:+ mock_restructure(ui_path)++ # Verify it was NOT called+ mock_restructure.assert_not_called()+++def test_restructure_ui_html_files_NOT_skipped_locally(monkeypatch):+ """+ Test that _restructure_ui_html_files is NOT skipped for local development+ """+ monkeypatch.delenv("LITELLM_NON_ROOT", raising=False)++ is_non_root = False+ ui_path = "/some/local/path"++ with patch(+ "litellm.proxy.proxy_server._restructure_ui_html_files"+ ) as mock_restructure:+ if is_non_root and ui_path == "/var/lib/litellm/ui":+ pass+ else:+ mock_restructure(ui_path)++ # Verify it WAS called+ mock_restructure.assert_called_once_with(ui_path)