fix(cookbook): install llama-cpp-python[server] so llama.cpp serving works (#730) (#1338)

The llama.cpp serve auto-install built a bare `llama-cpp-python` in the Linux
source-build fallback and the Termux path, but the serve command runs
`python3 -m llama_cpp.server`, which needs the `[server]` extra. Because the
"already installed?" guard only checks `import llama_cpp` (a bare install
satisfies it), the missing extra was never added, so serving crashed with
`ModuleNotFoundError: No module named 'starlette_context'` (issue #730).

- Request the `[server]` extra in both the Termux direct install and the Linux
  Python-bindings fallback (the Windows path already used `[server]`).
- Shell-quote the package spec in `_pip_install_fallback_chain` via `shlex.quote`
  so the `[server]` brackets aren't treated as a bash glob; plain names unaffected.

Tests: tests/test_cookbook_helpers.py gains extras-quoting coverage and a
serve-runner regression guard.
This commit is contained in:
Shaw
2026-06-03 01:24:26 -04:00
committed by GitHub
parent 552bc15067
commit b10e6bc870
3 changed files with 41 additions and 4 deletions
+32
View File
@@ -159,6 +159,38 @@ def test_pip_install_fallback_chain_tries_user_outside_venv():
assert "user_attempt" in result.stdout, "Chain should try --user when not in venv and base fails"
def test_pip_install_fallback_chain_quotes_extras_spec():
"""An extras spec like ``llama-cpp-python[server]`` must be shell-quoted so
bash does not treat the brackets as a glob, and the ``[server]`` extra
(which pulls in starlette_context for ``python -m llama_cpp.server``) is
actually installed instead of a bare ``llama-cpp-python`` (issue #730)."""
chain = _pip_install_fallback_chain("llama-cpp-python[server]", python_cmd="pip")
# Quoted in both the plain and the --user attempt.
assert chain.count("'llama-cpp-python[server]'") == 2
# Never the unquoted form (bracket-glob risk).
assert "install -q llama-cpp-python[server]" not in chain
# A plain package name is still passed through unquoted (no regression).
plain = _pip_install_fallback_chain("hf_transfer", python_cmd="pip")
assert "install -q hf_transfer" in plain
def test_serve_runner_installs_llama_cpp_server_extra():
"""The llama.cpp serve auto-install must request the ``[server]`` extra in
every path (issue #730): a bare ``llama-cpp-python`` passes the
``import llama_cpp`` guard, so ``python -m llama_cpp.server`` then crashes
with ``ModuleNotFoundError: No module named 'starlette_context'`` and the
extra is never reinstalled."""
import pathlib
src = (pathlib.Path(__file__).resolve().parent.parent
/ "routes" / "cookbook_routes.py").read_text(encoding="utf-8")
# No serve path may install a bare (extra-less) llama-cpp-python.
assert "pip install llama-cpp-python " not in src
assert "_pip_install_fallback_chain('llama-cpp-python'" not in src
# The [server] extra is requested in the build/fallback paths.
assert "'llama-cpp-python[server]'" in src
assert "_pip_install_fallback_chain('llama-cpp-python[server]'" in src
def test_venv_safe_local_pip_install_strips_user_flags_only_for_local_venv():
cmd = 'python3 -m pip install -U --user --break-system-packages "vllm"'