Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion litellm/proxy/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,9 @@ def to_dict(self) -> dict:

class CommonProxyErrors(str, enum.Enum):
db_not_connected_error = (
"DB not connected. See https://docs.litellm.ai/docs/proxy/virtual_keys"
"DB not connected. This endpoint needs a database; set DATABASE_URL to a "
"PostgreSQL connection string (postgresql://...) to enable it. "
"See https://docs.litellm.ai/docs/proxy/virtual_keys"
)
no_llm_router = "No models configured on proxy"
not_allowed_access = "Admin-only endpoint. Not allowed to access this."
Expand Down
21 changes: 20 additions & 1 deletion litellm/proxy/db/db_url_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import os
import urllib.parse
from typing import Optional, cast
from typing import Final, Optional, cast

from pydantic import AliasChoices, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
Expand All @@ -44,6 +44,25 @@
_IAM_ENV_KEY = "IAM_TOKEN_DB_AUTH"
_DEFAULT_PG_PORT = "5432"

# schema.prisma pins `provider = "postgresql"`, so these are the only schemes
# Prisma can actually connect with.
SUPPORTED_DB_SCHEMES: Final[frozenset[str]] = frozenset({"postgresql", "postgres"})


def unsupported_db_scheme(database_url: str) -> Optional[str]:
"""Return the connection URL scheme when it is not PostgreSQL, else None.

A `sqlite://` / `mysql://` URL can never connect against the
postgresql-only datasource, but the resulting Prisma failure is opaque and
version-dependent (a confusing migration error, or a startup that never
binds). Callers use this to reject the URL up front with an actionable
error instead.
"""
scheme = urllib.parse.urlsplit(database_url).scheme.lower()
if scheme in SUPPORTED_DB_SCHEMES:
return None
return scheme or database_url
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved


class DatabaseURLSettings(BaseSettings):
"""Discrete ``DATABASE_*`` env vars, loaded once at process start.
Expand Down
20 changes: 20 additions & 0 deletions litellm/proxy/proxy_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,26 @@ def run_server( # noqa: PLR0915
os.getenv("DATABASE_URL", None) is not None
or os.getenv("DIRECT_URL", None) is not None
):
from litellm.proxy.db.db_url_settings import unsupported_db_scheme

for _db_env in ("DATABASE_URL", "DIRECT_URL"):
_db_url = os.getenv(_db_env)
if _db_url is None:
continue
_bad_scheme = unsupported_db_scheme(_db_url)
if _bad_scheme is not None:
print( # noqa
f"\033[1;31mLiteLLM Proxy: {_db_env} uses unsupported "
f"scheme '{_bad_scheme}'. LiteLLM's database features "
"(virtual keys, store_model_in_db, spend tracking) "
"require PostgreSQL; use a 'postgresql://' connection "
"string. SQLite and other engines are not supported. "
"See https://docs.litellm.ai/docs/proxy/virtual_keys"
"\033[0m",
file=sys.stderr,
flush=True,
)
sys.exit(1)
Comment thread
krishvsoni marked this conversation as resolved.
Resolved
try:
from litellm.secret_managers.main import get_secret

Expand Down
31 changes: 30 additions & 1 deletion tests/test_litellm/proxy/db/test_db_url_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import pytest

from litellm.proxy.db.db_url_settings import DatabaseURLSettings
from litellm.proxy.db.db_url_settings import (
DatabaseURLSettings,
unsupported_db_scheme,
)


def _apply() -> bool:
Expand Down Expand Up @@ -287,3 +290,29 @@ def test_password_reader_uses_own_credentials(monkeypatch):
os.environ["DATABASE_URL_READ_REPLICA"]
== "postgresql://litellm_ro:ro_pw@reader.example.com:5432/litellm_db"
)


@pytest.mark.parametrize(
"url",
[
"postgresql://u:p@host:5432/db",
"postgres://u:p@host:5432/db",
"POSTGRESQL://u:p@host:5432/db",
"postgresql://host/db?schema=public",
],
)
def test_unsupported_db_scheme_accepts_postgres(url):
assert unsupported_db_scheme(url) is None


@pytest.mark.parametrize(
"url,scheme",
[
("sqlite:///data/litellm.db", "sqlite"),
("sqlite:///./local.db", "sqlite"),
("mysql://u:p@host:3306/db", "mysql"),
("mssql://host/db", "mssql"),
],
)
def test_unsupported_db_scheme_rejects_non_postgres(url, scheme):
assert unsupported_db_scheme(url) == scheme
Loading
Loading