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(lint): generate the prisma client into the gate-owned venv

mateo-berrie8245107654a
diff --git a/.github/workflows/test-linting.yml b/.github/workflows/test-linting.yml
index 5125dd0a354..5e333f2a3ca 100644
--- a/.github/workflows/test-linting.yml
+++ b/.github/workflows/test-linting.yml
@@ -115,6 +115,7 @@ jobs:
- name: Check basedpyright budget (delta vs base)
env:
GH_TOKEN: ${{ github.token }}
+ PRISMA_BINARY_CACHE_DIR: ${{ runner.temp }}/prisma-cache
run: |
uv run --no-sync python scripts/type_check_gate.py --base "$GATE_BASE_SHA"
diff --git a/scripts/prisma_generate_if_needed.py b/scripts/prisma_generate_if_needed.py
index d2c40adf820..f2d2232b23f 100644
--- a/scripts/prisma_generate_if_needed.py
+++ b/scripts/prisma_generate_if_needed.py
@@ -9,13 +9,21 @@ client (a fresh or reinstalled prisma package) forces a regenerate even when
the stamp matches. The prisma package itself is never imported here: once
generated it re-exports the whole client on import, which costs more than the
generate this script exists to skip.
+
+prisma resolves its generator command (``prisma-client-py``) through a plain
+PATH lookup, never through the interpreter that invoked ``prisma generate``,
+so the generate runs with this interpreter's own bin directory pinned to the
+front of PATH; without that pin the client lands in whichever venv the caller
+happened to have on PATH (or the generate fails outright when none is).
"""
import hashlib
import importlib.metadata
import importlib.util
+import os
import subprocess
import sys
+from collections.abc import Callable, Mapping
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
@@ -46,6 +54,25 @@ def client_is_generated() -> bool:
)
+def env_with_own_bin_first(base_env: Mapping[str, str]) -> dict[str, str]:
+ bin_dir = str(Path(sys.executable).parent)
+ inherited = base_env.get("PATH")
+ path = os.pathsep.join((bin_dir, inherited)) if inherited else bin_dir
+ return {**base_env, "PATH": path}
+
+
+def _run_command(cmd: list[str], cwd: Path, env: dict[str, str]) -> int:
+ return subprocess.run(cmd, cwd=cwd, env=env).returncode
+
+
+def run_generate(run: Callable[[list[str], Path, dict[str, str]], int] = _run_command) -> int:
+ return run(
+ [sys.executable, "-m", "prisma", "generate", "--schema", str(SCHEMA)],
+ REPO_ROOT,
+ env_with_own_bin_first(os.environ),
+ )
+
+
def main() -> int:
version = importlib.metadata.version("prisma")
expected = stamp_value(SCHEMA.read_bytes(), version)
@@ -55,12 +82,9 @@ def main() -> int:
f"(prisma {version}); skipping prisma generate"
)
return 0
- result = subprocess.run(
- [sys.executable, "-m", "prisma", "generate", "--schema", str(SCHEMA)],
- cwd=REPO_ROOT,
- )
- if result.returncode != 0:
- return result.returncode
+ returncode = run_generate()
+ if returncode != 0:
+ return returncode
STAMP.write_text(expected)
return 0
diff --git a/scripts/type_check_gate.py b/scripts/type_check_gate.py
index f9d7f2912fd..2fab7f8363a 100644
--- a/scripts/type_check_gate.py
+++ b/scripts/type_check_gate.py
@@ -34,8 +34,8 @@ skipped outright. When it is needed, it is a second basedpyright pass over a
detached worktree at the merge-base, run under the same environment so import
resolution matches, and its per-rule counts are cached under the repo's git
common dir keyed by merge-base commit, ``pyrightconfig.json``, ``uv.lock``,
-and the dependency-group set, so re-runs against the same branch
-point pay for it once. A CI workflow publishes every staging commit's counts as
+the Prisma schema, and the dependency-group set, so re-runs against the same
+branch point pay for it once. A CI workflow publishes every staging commit's counts as
an artifact (``--emit-counts-dir`` is its entry point), and on a disk-cache miss
the gate first tries to download the merge-base's artifact through the ``gh``
CLI; any fetch failure falls back silently to the local base pass, so the gate
@@ -83,6 +83,7 @@ GH_TIMEOUT_SECONDS = 10
TYPECHECK_ENV_DIR = REPO_ROOT / ".venv-typecheck"
TYPECHECK_DEP_GROUPS = ("proxy-dev", "e2e-dev")
PRISMA_GENERATE_SCRIPT = REPO_ROOT / "scripts" / "prisma_generate_if_needed.py"
+PRISMA_SCHEMA = REPO_ROOT / "litellm" / "proxy" / "schema.prisma"
# basedpyright's node process needs more than the ~4 GB default heap on this
# repo; appended last so it wins node's last-flag-wins resolution over any
@@ -192,6 +193,11 @@ def ensure_typecheck_env(
measurement pass. Unconditional on purpose: an up-to-date env makes both
steps near-instant no-ops, and skipping them on a heuristic is how the
measured environment and the fingerprinted one drift apart."""
+ if not env_dir.exists():
+ sys.stderr.write(
+ f"provisioning {env_dir.name} (first run installs packages and "
+ "generates the Prisma client; re-runs are near-instant no-ops)\n"
+ )
env: Final = {**os.environ, "UV_PROJECT_ENVIRONMENT": str(env_dir)}
for cmd in typecheck_env_commands(env_dir):
if run(cmd, env) != 0:
@@ -298,7 +304,7 @@ def environment_fingerprints(
return (
*(
hashlib.sha256(path.read_bytes()).hexdigest()
- for path in (PYRIGHT_CONFIG, UV_LOCK)
+ for path in (PYRIGHT_CONFIG, UV_LOCK, PRISMA_SCHEMA)
if path.exists()
),
"groups:" + ",".join(dep_groups),
diff --git a/tests/test_litellm/test_prisma_generate_if_needed.py b/tests/test_litellm/test_prisma_generate_if_needed.py
index 39b9fcc4202..c36f580e24f 100644
--- a/tests/test_litellm/test_prisma_generate_if_needed.py
+++ b/tests/test_litellm/test_prisma_generate_if_needed.py
@@ -1,4 +1,6 @@
import importlib.util
+import os
+import sys
from pathlib import Path
_MODULE_PATH = (
@@ -33,3 +35,30 @@ def test_skip_requires_a_generated_client_even_with_a_matching_stamp(tmp_path):
expected = mod.stamp_value(b"schema", "0.11.0")
stamp.write_text(expected)
assert mod.should_skip(stamp, expected, client_generated=False) is False
+
+
+def test_env_puts_this_interpreters_bin_dir_first_on_path():
+ env = mod.env_with_own_bin_first({"PATH": "/usr/bin", "HOME": "/home"})
+ bin_dir = str(Path(sys.executable).parent)
+ assert env["PATH"].split(os.pathsep) == [bin_dir, "/usr/bin"]
+ assert env["HOME"] == "/home"
+
+
+def test_env_without_an_inherited_path_is_just_the_bin_dir():
+ env = mod.env_with_own_bin_first({})
+ assert env["PATH"] == str(Path(sys.executable).parent)
+
+
+def test_generate_runs_prisma_with_its_own_bin_dir_leading_the_childs_path():
+ seen = {}
+
+ def recorder(cmd, cwd, env):
+ seen["cmd"] = cmd
+ seen["cwd"] = cwd
+ seen["env"] = env
+ return 0
+
+ assert mod.run_generate(run=recorder) == 0
+ assert seen["cmd"][:4] == [sys.executable, "-m", "prisma", "generate"]
+ assert seen["cwd"] == mod.REPO_ROOT
+ assert seen["env"]["PATH"].split(os.pathsep)[0] == str(Path(sys.executable).parent)
diff --git a/tests/test_litellm/test_type_check_gate.py b/tests/test_litellm/test_type_check_gate.py
index 2d9731db53f..6cf21707629 100644
--- a/tests/test_litellm/test_type_check_gate.py
+++ b/tests/test_litellm/test_type_check_gate.py
@@ -1,3 +1,4 @@
+import hashlib
import importlib.util
import json
import subprocess
@@ -278,6 +279,11 @@ def test_fingerprints_carry_the_dependency_group_set():
)
+def test_fingerprints_cover_the_prisma_schema():
+ schema_hash = hashlib.sha256(gate.PRISMA_SCHEMA.read_bytes()).hexdigest()
+ assert schema_hash in gate.environment_fingerprints()
+
+
def test_env_commands_sync_the_canonical_groups_then_generate_prisma():
sync, generate = gate.typecheck_env_commands(Path("/envdir"))
assert sync[:3] == ("uv", "sync", "--frozen")
@@ -327,6 +333,22 @@ def test_ensure_env_fails_loudly_and_stops_at_the_first_failed_step(tmp_path):
assert len(calls) == 1
+def test_ensure_env_announces_a_cold_provision(tmp_path, capsys):
+ def runner(cmd, env):
+ return 0
+
+ gate.ensure_typecheck_env(env_dir=tmp_path / "fresh", run=runner)
+ assert "provisioning" in capsys.readouterr().err
+
+
+def test_ensure_env_is_silent_when_the_env_already_exists(tmp_path, capsys):
+ def runner(cmd, env):
+ return 0
+
+ gate.ensure_typecheck_env(env_dir=tmp_path, run=runner)
+ assert capsys.readouterr().err == ""
+
+
def test_cached_counts_round_trip(tmp_path):
path = gate.cache_path(tmp_path, "abc123", ("f1", "f2"))
gate.store_counts(tmp_path, path, "abc123", {"reportAny": 3, "reportCall": 1})