mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* fix(proxy): start the Prisma engine on Python 3.10 and pin prisma to the versions it supports The port-owning query engine waited with asyncio.timeout, which only exists on Python 3.11+, so a proxy with a database could not start on 3.10. It now waits with asyncio.wait_for The engine subclass imports prisma.builder, which prisma 0.13 removed, so extra_proxy now allows prisma>=0.11.0,<0.13. prisma 0.12.0 passes the engine tests on 3.10 A fake query engine executable covers the connect, silent, and early-exit paths, and a new prisma-engine-python-310 CI shard runs those tests on Python 3.10 whenever the engine code changes * fix(proxy): run the Prisma engine on prisma 0.13+ and fix Python 3.10 chat with a database Drop the prisma<0.13 cap and make the engine subclass work on prisma 0.11 through 0.15. OVERWRITE_DATASOURCES is built with json.dumps instead of the removed prisma.builder, and the client's HTTP options reach the engine as http_config when its constructor takes it Build CallContext without the subscripted alias, which raised TypeError on Python 3.10 for every chat call with a database Move the engine tests that start a real process to tests/integration/database, and have the Python 3.10 CI shard install prisma 0.15.0 and run the dispatcher tests too * fix(proxy): set the Prisma client engine through its setter so prisma 0.15 connects * test(proxy): run the query engine spawn path in-process on Python 3.10 * fix(proxy): keep TimeoutError as the cause of the query engine connect timeout The connect timeout EngineConnectionError chains the TimeoutError again, as on the merge base, so the startup traceback keeps its direct cause block --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
129 lines
4.2 KiB
Python
129 lines
4.2 KiB
Python
import asyncio
|
|
import sys
|
|
from dataclasses import dataclass
|
|
from datetime import timedelta
|
|
from pathlib import Path
|
|
from typing import Final, Literal
|
|
|
|
import pytest
|
|
from prisma import Prisma
|
|
from prisma.engine.errors import EngineConnectionError
|
|
|
|
from litellm.proxy.db.prisma_client import PrismaWrapper
|
|
from tests._process_helpers import process_is_gone
|
|
|
|
_FAKE_QUERY_ENGINE: Final = """
|
|
import http.server
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import signal
|
|
import sys
|
|
import time
|
|
|
|
signal.signal(signal.SIGINT, lambda *_: sys.exit(0))
|
|
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
|
|
if sys.argv[1:] == ["--version"]:
|
|
print("query-engine fake")
|
|
sys.exit(0)
|
|
pathlib.Path(os.environ["FAKE_ENGINE_PID_FILE"]).write_text(str(os.getpid()))
|
|
behavior = os.environ["FAKE_ENGINE_BEHAVIOR"]
|
|
if behavior == "exit":
|
|
sys.exit(1)
|
|
while behavior == "silent":
|
|
time.sleep(60)
|
|
requests = pathlib.Path(os.environ["FAKE_ENGINE_REQUESTS"])
|
|
|
|
|
|
class Status(http.server.BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
with requests.open("a") as log:
|
|
print(self.command, self.path, file=log)
|
|
body = b'{"status": "ok"}'
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Content-Length", str(len(body)))
|
|
self.end_headers()
|
|
self.wfile.write(body)
|
|
|
|
def log_message(self, *args):
|
|
pass
|
|
|
|
|
|
server = http.server.HTTPServer(("127.0.0.1", 0), Status)
|
|
port = server.server_address[1]
|
|
message = f"Started query engine http server on http://127.0.0.1:{port}"
|
|
print("query-engine banner that is not JSON", flush=True)
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"timestamp": "2026-01-01T00:00:00.000000Z",
|
|
"level": "INFO",
|
|
"fields": {"message": message, "ip": "127.0.0.1", "port": str(port)},
|
|
"target": "query_engine::server",
|
|
}
|
|
),
|
|
flush=True,
|
|
)
|
|
server.serve_forever()
|
|
"""
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class _FakeQueryEngine:
|
|
pid_file: Path
|
|
requests_file: Path
|
|
|
|
@property
|
|
def requests(self) -> tuple[str, ...]:
|
|
return tuple(self.requests_file.read_text().splitlines()) if self.requests_file.exists() else ()
|
|
|
|
def is_gone(self) -> bool:
|
|
return process_is_gone(int(self.pid_file.read_text()), within_seconds=5)
|
|
|
|
|
|
def _install_fake_query_engine(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, behavior: Literal["publish", "silent", "exit"]
|
|
) -> _FakeQueryEngine:
|
|
binary: Final = tmp_path / "query-engine"
|
|
binary.write_text(f"#!{sys.executable}\n{_FAKE_QUERY_ENGINE}")
|
|
binary.chmod(0o755)
|
|
engine: Final = _FakeQueryEngine(pid_file=tmp_path / "engine.pid", requests_file=tmp_path / "requests.log")
|
|
monkeypatch.setenv("PRISMA_QUERY_ENGINE_BINARY", str(binary))
|
|
monkeypatch.setenv("FAKE_ENGINE_BEHAVIOR", behavior)
|
|
monkeypatch.setenv("FAKE_ENGINE_PID_FILE", str(engine.pid_file))
|
|
monkeypatch.setenv("FAKE_ENGINE_REQUESTS", str(engine.requests_file))
|
|
return engine
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_the_client_connects_to_the_address_the_query_engine_publishes(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
engine: Final = _install_fake_query_engine(tmp_path, monkeypatch, "publish")
|
|
client: Final = PrismaWrapper(Prisma(datasource={"url": "postgresql://unused@localhost/unused"}))
|
|
try:
|
|
await client.connect(timedelta(seconds=30))
|
|
assert "GET /status" in engine.requests
|
|
finally:
|
|
await client.disconnect()
|
|
assert engine.is_gone()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("behavior", "engine_timeout"),
|
|
(("silent", timedelta(seconds=1)), ("exit", timedelta(minutes=10))),
|
|
ids=("never prints", "exits first"),
|
|
)
|
|
async def test_a_query_engine_that_publishes_no_address_fails_the_connect_and_is_stopped(
|
|
behavior: Literal["silent", "exit"], engine_timeout: timedelta, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
engine: Final = _install_fake_query_engine(tmp_path, monkeypatch, behavior)
|
|
client: Final = PrismaWrapper(Prisma(datasource={"url": "postgresql://unused@localhost/unused"}))
|
|
try:
|
|
with pytest.raises(EngineConnectionError):
|
|
await asyncio.wait_for(client.connect(engine_timeout), timeout=60)
|
|
finally:
|
|
await client.disconnect()
|
|
assert engine.is_gone()
|