Skip to content

[cherry-pick][2.59.0][Serve] Import jinja2 lazily and declare it in the serve extra (#65542) - #65988

Merged
elliot-barn merged 2 commits into
releases/2.59.0from
cherry-pick-65542-2.59.0
Sep 9, 2026
Merged

elliot-barn merged 2 commits into
releases/2.59.0from
cherry-pick-65542-2.59.0

Conversation

@elliot-barn

Copy link
Copy Markdown
Collaborator

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

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
elliot-barn requested review from a team, edoakes and richardliaw as code owners September 8, 2026 15:51

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@elliot-barn elliot-barn added the go add ONLY when ready to merge, run all tests label Sep 8, 2026
@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Sep 8, 2026
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
elliot-barn merged commit 9cfe980 into releases/2.59.0 Sep 9, 2026
5 of 6 checks passed
@elliot-barn
elliot-barn deleted the cherry-pick-65542-2.59.0 branch September 9, 2026 04:47
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants