feat(cli): support compatible api version ranges (#8023)

Adds `api_version` ranges for LangGraph API base image tags.
`~=0.11.0.dev5` resolves to the newest matching compatible image while
freezing later dev prereleases until rc/final/patch tags exist;
`>~=0.11.0.dev5` uses the same floor but can also float to future stable
releases while ignoring future prereleases.

Verified with `make format`, `make lint`, and `make test` from
`libs/cli`.

---------

Signed-off-by: Connor Braa <cwlbraa@langchain.dev>
This commit is contained in:
Connor Braa
2026-06-15 20:24:31 -04:00
committed by GitHub
parent 97320843fe
commit 916025d2f6
3 changed files with 327 additions and 2 deletions
+8 -1
View File
@@ -159,7 +159,14 @@ OPT_POSTGRES_URI = click.option(
OPT_API_VERSION = click.option(
"--api-version",
type=str,
help="API server version to use for the base image. If unspecified, the latest version will be used.",
help=(
"API server version to use for the base image. If unspecified, the "
"latest stable version will be used. Compatible ranges like "
"~=0.11.0.dev5 stay on 0.11.0.dev5 while only newer dev builds exist, "
"then resolve to the newest matching rc or stable release, for example "
"0.11.0rc1 or 0.11.0. Stable-floating ranges like >~=0.11.0.dev5 "
"can also pick up future stable releases, for example 0.12.0."
),
)
OPT_ENGINE_RUNTIME_MODE = click.option(
+162 -1
View File
@@ -9,6 +9,7 @@ from collections import Counter
from typing import Literal, NamedTuple
import click
import httpx
from langgraph_cli.schemas import Config, Distros
from langgraph_cli.uv_lock import python_config_to_docker_uv_lock
@@ -41,6 +42,31 @@ _API_VERSION_PATTERN = re.compile(
r"(?:\.(?P<patch>\d+))?"
r"(?:(?:\.|)(?:[A-Za-z][0-9A-Za-z]*))?$"
)
_API_VERSION_RANGE_PATTERN = re.compile(r"^(?P<operator>~=|>~=)\s*(?P<version>.+)$")
_API_VERSION_PART_PATTERN = re.compile(
r"^(?P<major>\d+)"
r"(?:\.(?P<minor>\d+))?"
r"(?:\.(?P<patch>\d+))?"
r"(?:(?:\.|)(?P<pre>dev|rc)(?P<pre_n>\d+))?$"
)
class _ParsedApiVersion(NamedTuple):
release: tuple[int, ...]
prerelease: str | None
prerelease_number: int
class _ApiVersionRange(NamedTuple):
floor: _ParsedApiVersion
allow_future_stable: bool
_PRERELEASE_ORDER = {
"dev": 0,
"rc": 1,
None: 2,
}
def has_disallowed_build_command_content(command: str) -> bool:
@@ -141,6 +167,133 @@ def _parse_api_version_parts(version_str: str) -> tuple[int, ...]:
return tuple(int(part) for part in match.groups() if part is not None)
def _parse_api_version(version_str: str) -> _ParsedApiVersion:
match = _API_VERSION_PART_PATTERN.fullmatch(version_str)
if not match:
raise ValueError("Version must be major or major.minor or major.minor.patch.")
release = tuple(
int(part)
for part in (
match.group("major"),
match.group("minor"),
match.group("patch"),
)
if part is not None
)
prerelease = match.group("pre")
prerelease_number = int(match.group("pre_n") or 0)
return _ParsedApiVersion(release, prerelease, prerelease_number)
def _api_version_sort_key(
version: _ParsedApiVersion,
) -> tuple[tuple[int, ...], int, int]:
return (
version.release,
_PRERELEASE_ORDER[version.prerelease],
version.prerelease_number,
)
def _api_version_upper_bound(version: _ParsedApiVersion) -> tuple[int, ...]:
release = version.release
if len(release) <= 2:
return (release[0] + 1,)
return (release[0], release[1] + 1)
def _is_compatible_api_version_candidate(
candidate: _ParsedApiVersion,
version_range: _ApiVersionRange,
upper_bound: tuple[int, ...],
) -> bool:
floor = version_range.floor
if _api_version_sort_key(candidate) < _api_version_sort_key(floor):
return False
outside_compatible_range = candidate.release[: len(upper_bound)] >= upper_bound
if outside_compatible_range and not version_range.allow_future_stable:
return False
if outside_compatible_range and candidate.prerelease is not None:
return False
if floor.prerelease == "dev" and candidate.prerelease == "dev":
return candidate == floor
return True
def _ensure_compatible_api_version_base_image(base_image: str) -> None:
if ":" in base_image:
raise click.UsageError(
"Compatible api_version ranges cannot be used with a tagged base_image."
)
def _get_pypi_versions(package_name: str) -> list[str]:
try:
response = httpx.get(
f"https://pypi.org/pypi/{package_name}/json",
timeout=10,
)
response.raise_for_status()
except httpx.HTTPError as exc:
raise click.UsageError(
f"Failed to fetch PyPI versions for {package_name}: {exc}"
) from None
payload = response.json()
releases = payload.get("releases", {})
if not isinstance(releases, dict):
raise click.UsageError(
f"Failed to fetch PyPI versions for {package_name}: invalid response."
)
return [version for version in releases if isinstance(version, str)]
def _resolve_compatible_api_version(
api_version: str,
base_image: str,
version_distro_tag: str,
) -> str:
match = _API_VERSION_RANGE_PATTERN.fullmatch(api_version)
if not match:
return api_version
floor_str = match.group("version").strip()
try:
floor = _parse_api_version(floor_str)
except ValueError:
raise click.UsageError(
f"Invalid compatible api_version range: {api_version}.\n\n"
"Use a compatible version range, e.g.:\n"
' "api_version": "~=0.11.0.dev5"\n'
"or a stable-floating range, e.g.:\n"
' "api_version": ">~=0.11.0.dev5"'
) from None
version_range = _ApiVersionRange(
floor=floor,
allow_future_stable=match.group("operator") == ">~=",
)
_ensure_compatible_api_version_base_image(base_image)
pypi_versions = _get_pypi_versions("langgraph-api")
candidates: list[tuple[_ParsedApiVersion, str]] = []
upper_bound = _api_version_upper_bound(floor)
for candidate_str in pypi_versions:
try:
candidate = _parse_api_version(candidate_str)
except ValueError:
continue
if _is_compatible_api_version_candidate(candidate, version_range, upper_bound):
candidates.append((candidate, candidate_str))
if not candidates:
raise click.UsageError(
f"No PyPI releases match compatible api_version range {api_version!r} "
f"for {base_image} with {version_distro_tag!r}."
)
return max(candidates, key=lambda item: _api_version_sort_key(item[0]))[1]
def _is_node_graph(spec: str | dict) -> bool:
"""Check if a graph is a Node.js graph based on the file extension."""
if isinstance(spec, dict):
@@ -194,7 +347,12 @@ def validate_config(config: Config) -> Config:
)
if api_version:
try:
parts = _parse_api_version_parts(api_version)
compatible_match = _API_VERSION_RANGE_PATTERN.fullmatch(api_version)
parts = _parse_api_version_parts(
compatible_match.group("version").strip()
if compatible_match
else api_version
)
if len(parts) > 3:
raise ValueError(
"Version must be major or major.minor or major.minor.patch."
@@ -1430,6 +1588,9 @@ def docker_tag(
# Prepend API version if provided
if api_version:
api_version = _resolve_compatible_api_version(
api_version, base_image, f"{language}{version_distro_tag}"
)
full_tag = f"{api_version}-{language}{version_distro_tag}"
elif "/langgraph-server" in base_image and version_distro_tag not in base_image:
return f"{base_image}-{language}{version_distro_tag}"
+157
View File
@@ -2961,6 +2961,163 @@ def test_docker_tag_with_prerelease_api_version(version: str, in_config: bool):
assert tag == f"langchain/langgraph-api:{version}-py3.11"
def test_docker_tag_with_compatible_api_version_promotes_to_latest_patch():
config = validate_config(
{
"python_version": "3.12",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"image_distro": "wolfi",
"api_version": "~=0.11.0.dev5",
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=[
"0.11.0.dev5",
"0.11.0.dev6",
"0.11.0rc1",
"0.11.0",
"0.11.1rc1",
"0.11.1",
"0.12.0rc1",
],
) as get_versions:
tag = docker_tag(config)
get_versions.assert_called_once_with("langgraph-api")
assert tag == "langchain/langgraph-api:0.11.1-py3.12-wolfi"
def test_docker_tag_with_compatible_api_version_freezes_dev_until_rc():
config = validate_config(
{
"python_version": "3.12",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": "~=0.11.0.dev5",
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=[
"0.11.0.dev5",
"0.11.0.dev6",
"0.11.0.dev7",
],
):
tag = docker_tag(config)
assert tag == "langchain/langgraph-api:0.11.0.dev5-py3.12"
def test_docker_tag_with_stable_floating_api_version_promotes_to_future_stable():
config = validate_config(
{
"python_version": "3.12",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"image_distro": "wolfi",
"api_version": ">~=0.11.0.dev5",
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=[
"0.11.0.dev5",
"0.11.0.dev6",
"0.11.0rc1",
"0.11.0",
"0.11.1",
"0.12.0rc1",
"0.12.0",
"0.13.0.dev1",
"0.13.0",
],
) as get_versions:
tag = docker_tag(config)
get_versions.assert_called_once_with("langgraph-api")
assert tag == "langchain/langgraph-api:0.13.0-py3.12-wolfi"
def test_docker_tag_with_stable_floating_api_version_ignores_future_prereleases():
config = validate_config(
{
"python_version": "3.12",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": ">~=0.11.0.dev5",
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=[
"0.11.0.dev5",
"0.11.0",
"0.12.0rc1",
"0.12.0.dev1",
],
):
tag = docker_tag(config)
assert tag == "langchain/langgraph-api:0.11.0-py3.12"
def test_validate_config_rejects_unrecognized_api_version_range_operator():
with pytest.raises(click.UsageError, match="Invalid version format"):
validate_config(
{
"python_version": "3.12",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": "~>=0.11.0.dev5",
}
)
def test_docker_tag_with_compatible_api_version_supports_node_images():
config = validate_config(
{
"node_version": "20",
"graphs": {"agent": "./agent.js:graph"},
"image_distro": "wolfi",
"api_version": "~=1.2.4",
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=[
"1.2.4",
"1.2.5",
"1.3.0",
],
) as get_versions:
tag = docker_tag(config)
get_versions.assert_called_once_with("langgraph-api")
assert tag == "langchain/langgraphjs-api:1.2.5-node20-wolfi"
def test_docker_tag_with_compatible_api_version_rejects_tagged_base_image():
config = validate_config(
{
"python_version": "3.11",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": "~=0.11.0.dev5",
}
)
with pytest.raises(click.UsageError, match="tagged base_image"):
docker_tag(config, base_image="langchain/langgraph-api:0.11.0")
def test_config_to_docker_with_api_version():
"""Test config_to_docker function with api_version parameter."""