Repository navigation
[cherry-pick][2.59.0][Serve] Import jinja2 lazily and declare it in the serve extra (#65542) - #65988
Merged
Merged
Conversation
As of 2.57.0, `controller.py` imports `haproxy.py` unconditionally, and `haproxy.py` imported jinja2 at module scope — but jinja2 was never declared in Ray's metadata (`ray-haproxy` is a binary distribution with `requires_dist: None`, so nothing pulls it in). On a slim base image: ```bash docker run --rm python:3.13-slim bash -c \ 'pip install -q "ray[default,serve]==2.57.0" && python -c "from ray import serve"' ``` This PR applies both fixes suggested in the issue: 1. **Lazy import**: `from jinja2 import Environment` moves from module scope into `_generate_config_file_internal()` (its single use site), so a plain `import ray.serve` no longer requires jinja2 — it's only needed when HAProxy mode actually renders its config. 2. **Declare the dependency**: `jinja2` is added to the `serve` extra in `setup.py` (mirrored in `python/requirements.txt`), so it's present when HAProxy mode *is* used. The compiled lockfiles already pin `jinja2==3.1.6`, so no lock regeneration is needed. Fixes #65507 - [x] I've signed off every commit (DCO). - [x] Formatting checked with the repo-pinned `black==22.10.0`. - [x] Testing strategy (run locally against master via Ray nightly wheel + `setup-dev.py` symlinks): - Added `test_import_serve_does_not_require_jinja2`: a subprocess blocks jinja2 via a meta-path finder, then imports `ray.serve` and `ray.serve._private.controller`. Fails on master (repro of the issue), passes with this change. - Verified the render path still works with jinja2 present: constructed `HAProxyApi` and called `_generate_config_file_internal()`; a full HAProxy config (~5 KB) renders through the now-lazy import. - This PR was prepared with AI assistance; I have reviewed every changed line and run the checks above locally. - Duplicate-work check: no open PR references #65507; the issue is unassigned with no claims. --------- Signed-off-by: lonexreb <[email protected]> Signed-off-by: elliot-barn <[email protected]> Co-authored-by: Elliot Barnwell <[email protected]> (cherry picked from commit e11fe03) Signed-off-by: elliot-barn <[email protected]>
elliot-barn
requested review from
a team,
edoakes and
richardliaw
as code owners
September 8, 2026 15:51
Contributor
There was a problem hiding this comment.
Code Review
This pull request makes the jinja2 import lazy in haproxy.py so that importing ray.serve does not require jinja2 unless HAProxy mode is actually used. It adds a unit test to verify this behavior, adds jinja2 to requirements.txt and setup.py, and updates the relevant dependency lock files. As there are no review comments, I have no feedback to provide.
This lock file was deleted on master before #65542 merged, so the cherry-pick could not bring the regenerated version; the release branch still compiles this depset and needs the via-ray entry now that jinja2 is declared in the serve extra. Signed-off-by: elliot-barn <[email protected]>
elliot-barn
added a commit
that referenced
this pull request
Sep 14, 2026
…the unrelated lock drift (#66119) (#66152) Cherry-pick of #66119 (master commit 3f785c0) onto releases/2.59.0. One conflict resolved: the original commit also touched `python/deplocks/ray-torch/ray-torch-cu128_base_py3.11.lock`, which does not exist on this branch (only the py3.14 ray-torch lock is present) — dropped that file from the pick, same resolution as #65988. All other 72 lock/requirements files applied cleanly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Jeffrey Wang <[email protected]> Signed-off-by: elliot-barn <[email protected]> Co-authored-by: Jeffrey Wang <[email protected]> Co-authored-by: Claude Opus 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-pick of #65542 (master commit e11fe03) onto releases/2.59.0.
One conflict resolved: the original commit also updated
python/deplocks/ray-torch/ray-torch-cu128_base_py3.11.lock, which does not exist on this branch (only the py3.14 ray-torch lock is present on releases/2.59.0) — dropped that file from the pick; all other 82 lock/requirements updates applied cleanly.🤖 Generated with Claude Code