Compare commits

..
Author SHA1 Message Date
Josh RogersandRobin 77bb8b25ab feat(cli): resolve bare minor api_version to newest patch release
`api_version: "0.18"` produced a base image tag that is not published.
Bare minor (and major) pins on the default LangChain base images now
resolve to the newest final langgraph-api release in that line on PyPI.

Co-authored-by: Robin <open-swe@users.noreply.github.com>
2026-11-23 20:42:23 +00:00
6 changed files with 264 additions and 11 deletions

No files matched your search

+4 -1
View File
@@ -151,7 +151,10 @@ OPT_API_VERSION = click.option(
type=str,
help=(
"Agent Server version to use for the base image. If unspecified, the "
"latest stable version will be used. Compatible ranges like "
"latest stable version will be used. Exact versions like 0.18.3 are used "
"as-is. A bare minor like 0.18 (or major like 0) resolves to the newest "
"stable release in that line on PyPI, for example 0.18.4. 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 "
+71 -3
View File
@@ -54,6 +54,16 @@ _API_VERSION_PART_PATTERN = re.compile(
r"(?:\.(?P<patch>\d+))?"
r"(?:(?:\.|)(?P<pre>dev|rc)(?P<pre_n>\d+))?$"
)
_DEFAULT_API_BASE_IMAGES = frozenset(
{
"langchain/langgraph-api",
"langchain/langgraphjs-api",
"langchain/langgraph-executor",
}
)
_PIN_EXACT_API_VERSION_HINT = (
'Pin an exact api_version instead, e.g.:\n "api_version": "0.18.3"'
)
class _ParsedApiVersion(NamedTuple):
@@ -288,7 +298,7 @@ def _ensure_compatible_api_version_base_image(base_image: str) -> None:
)
def _get_pypi_versions(package_name: str) -> list[str]:
def _get_pypi_versions(package_name: str, *, exclude_yanked: bool = False) -> list[str]:
try:
response = httpx.get(
f"https://pypi.org/pypi/{package_name}/json",
@@ -305,7 +315,65 @@ def _get_pypi_versions(package_name: str) -> list[str]:
raise click.UsageError(
f"Failed to fetch PyPI versions for {package_name}: invalid response."
)
return [version for version in releases if isinstance(version, str)]
return [
version
for version, files in releases.items()
if isinstance(version, str)
and (
not exclude_yanked
or (
isinstance(files, list)
and any(
isinstance(file, dict) and not file.get("yanked") for file in files
)
)
)
]
def _resolve_api_version_line(
api_version: str,
base_image: str,
version_distro_tag: str,
) -> str:
"""Resolve a bare major or minor api_version to its newest final release."""
if base_image not in _DEFAULT_API_BASE_IMAGES:
return api_version
try:
line = _parse_api_version(api_version)
except ValueError:
return api_version
if line.prerelease is not None or len(line.release) > 2:
return api_version
try:
pypi_versions = _get_pypi_versions("langgraph-api", exclude_yanked=True)
except click.UsageError as exc:
raise click.UsageError(
f"Could not resolve api_version {api_version!r} to a release: "
f"{exc.message}\n\n{_PIN_EXACT_API_VERSION_HINT}"
) from None
candidates: list[tuple[_ParsedApiVersion, str]] = []
for candidate_str in pypi_versions:
try:
candidate = _parse_api_version(candidate_str)
except ValueError:
continue
if (
candidate.prerelease is None
and candidate.release[: len(line.release)] == line.release
):
candidates.append((candidate, candidate_str))
if not candidates:
raise click.UsageError(
f"No final PyPI releases match api_version {api_version!r} "
f"for {base_image} with {version_distro_tag!r}.\n\n"
f"{_PIN_EXACT_API_VERSION_HINT}"
)
return max(candidates, key=lambda item: _api_version_sort_key(item[0]))[1]
def _resolve_compatible_api_version(
@@ -315,7 +383,7 @@ def _resolve_compatible_api_version(
) -> str:
match = _API_VERSION_RANGE_PATTERN.fullmatch(api_version)
if not match:
return api_version
return _resolve_api_version_line(api_version, base_image, version_distro_tag)
floor_str = match.group("version").strip()
try:
+3 -1
View File
@@ -649,7 +649,9 @@ class Config(TypedDict, total=False):
api_version: str | None
"""Optional. Which semantic version of the Agent Server to use.
Defaults to latest. Check the
Defaults to latest. Exact versions like `"0.18.3"` are used as-is. A bare
minor like `"0.18"` (or major like `"0"`) resolves to the newest stable
release in that line on PyPI when using the default base images. Check the
[changelog](https://docs.langchain.com/langgraph-platform/langgraph-server-changelog)
for more information."""
+3 -3
View File
@@ -50,7 +50,7 @@
"type": "null"
}
],
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Check the\nfor more information.\n"
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Exact versions like `\"0.18.3\"` are used as-is. A bare\nminor like `\"0.18\"` (or major like `\"0\"`) resolves to the newest stable\nrelease in that line on PyPI when using the default base images. Check the\nfor more information.\n"
},
"auth": {
"anyOf": [
@@ -303,7 +303,7 @@
"type": "null"
}
],
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Check the\nfor more information.\n"
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Exact versions like `\"0.18.3\"` are used as-is. A bare\nminor like `\"0.18\"` (or major like `\"0\"`) resolves to the newest stable\nrelease in that line on PyPI when using the default base images. Check the\nfor more information.\n"
},
"auth": {
"anyOf": [
@@ -542,7 +542,7 @@
"type": "null"
}
],
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Check the\nfor more information.\n"
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Exact versions like `\"0.18.3\"` are used as-is. A bare\nminor like `\"0.18\"` (or major like `\"0\"`) resolves to the newest stable\nrelease in that line on PyPI when using the default base images. Check the\nfor more information.\n"
},
"auth": {
"anyOf": [
+3 -3
View File
@@ -50,7 +50,7 @@
"type": "null"
}
],
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Check the\nfor more information.\n"
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Exact versions like `\"0.18.3\"` are used as-is. A bare\nminor like `\"0.18\"` (or major like `\"0\"`) resolves to the newest stable\nrelease in that line on PyPI when using the default base images. Check the\nfor more information.\n"
},
"auth": {
"anyOf": [
@@ -303,7 +303,7 @@
"type": "null"
}
],
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Check the\nfor more information.\n"
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Exact versions like `\"0.18.3\"` are used as-is. A bare\nminor like `\"0.18\"` (or major like `\"0\"`) resolves to the newest stable\nrelease in that line on PyPI when using the default base images. Check the\nfor more information.\n"
},
"auth": {
"anyOf": [
@@ -542,7 +542,7 @@
"type": "null"
}
],
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Check the\nfor more information.\n"
"description": "Optional. Which semantic version of the Agent Server to use.\n\nDefaults to latest. Exact versions like `\"0.18.3\"` are used as-is. A bare\nminor like `\"0.18\"` (or major like `\"0\"`) resolves to the newest stable\nrelease in that line on PyPI when using the default base images. Check the\nfor more information.\n"
},
"auth": {
"anyOf": [
+180
View File
@@ -7,11 +7,13 @@ import textwrap
from unittest.mock import patch
import click
import httpx
import pytest
from langgraph_cli.config import (
_BUILD_TOOLS,
_get_pip_cleanup_lines,
_get_pypi_versions,
config_to_compose,
config_to_docker,
default_base_image,
@@ -3506,6 +3508,184 @@ def test_docker_tag_with_compatible_api_version_rejects_tagged_base_image():
docker_tag(config, base_image="langchain/langgraph-api:0.11.0")
_API_LINE_PYPI_VERSIONS = [
"0.17.9",
"0.18.0.dev1",
"0.18.0rc1",
"0.18.0",
"0.18.2",
"0.18.10",
"0.18.11rc1",
"0.19.0.dev1",
"0.19.0rc2",
"1.0.0rc1",
]
@pytest.mark.parametrize(
("api_version", "expected"),
[("0.18", "0.18.10"), ("0", "0.18.10"), ("0.17", "0.17.9")],
)
def test_docker_tag_resolves_api_version_line_to_latest_final_release(
api_version: str, expected: str
):
config = validate_config(
{
"python_version": "3.11",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": api_version,
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=_API_LINE_PYPI_VERSIONS,
) as get_versions:
tag = docker_tag(config)
get_versions.assert_called_once_with("langgraph-api", exclude_yanked=True)
assert tag == f"langchain/langgraph-api:{expected}-py3.11"
@pytest.mark.parametrize(
("config_extra", "base_image", "expected_prefix"),
[
(
{"node_version": "20", "graphs": {"agent": "./agent.js:graph"}},
None,
"langchain/langgraphjs-api:0.18.10-node20",
),
(
{"python_version": "3.12", "image_distro": "wolfi"},
"langchain/langgraph-executor",
"langchain/langgraph-executor:0.18.10-py3.12-wolfi",
),
],
)
def test_docker_tag_resolves_api_version_line_for_default_images(
config_extra: dict, base_image: str | None, expected_prefix: str
):
config = validate_config(
{
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": "0.18",
**config_extra,
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=_API_LINE_PYPI_VERSIONS,
):
tag = docker_tag(config, base_image=base_image)
assert tag == expected_prefix
@pytest.mark.parametrize("api_version", ["0.18.3", "0.19.0rc2", "0.19.0.dev1"])
def test_docker_tag_uses_exact_and_prerelease_api_version_verbatim(api_version: str):
config = validate_config(
{
"python_version": "3.11",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": api_version,
}
)
with patch("langgraph_cli.config._get_pypi_versions") as get_versions:
tag = docker_tag(config)
get_versions.assert_not_called()
assert tag == f"langchain/langgraph-api:{api_version}-py3.11"
@pytest.mark.parametrize(
"base_image", ["my-registry/langgraph-api", "langchain/langgraph-server"]
)
def test_docker_tag_keeps_api_version_line_verbatim_for_custom_base_image(
base_image: str,
):
config = validate_config(
{
"python_version": "3.11",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": "0.18",
}
)
with patch("langgraph_cli.config._get_pypi_versions") as get_versions:
tag = docker_tag(config, base_image=base_image)
get_versions.assert_not_called()
assert tag == f"{base_image}:0.18-py3.11"
def test_docker_tag_api_version_line_without_final_release_asks_for_exact_pin():
config = validate_config(
{
"python_version": "3.11",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": "0.19",
}
)
with patch(
"langgraph_cli.config._get_pypi_versions",
return_value=_API_LINE_PYPI_VERSIONS,
):
with pytest.raises(click.UsageError, match="No final PyPI releases") as exc:
docker_tag(config)
assert '"api_version": "0.18.3"' in exc.value.message
def test_docker_tag_api_version_line_when_pypi_unreachable_asks_for_exact_pin():
config = validate_config(
{
"python_version": "3.11",
"dependencies": ["."],
"graphs": {"agent": "./agent.py:graph"},
"api_version": "0.18",
}
)
with patch(
"langgraph_cli.config.httpx.get",
side_effect=httpx.ConnectError("network unreachable"),
):
with pytest.raises(click.UsageError, match="Could not resolve") as exc:
docker_tag(config)
assert "network unreachable" in exc.value.message
assert '"api_version": "0.18.3"' in exc.value.message
def test_get_pypi_versions_excludes_yanked_and_empty_releases():
releases = {
"0.18.0": [{"yanked": False}],
"0.18.1": [{"yanked": True}, {"yanked": True}],
"0.18.2": [{"yanked": True}, {"yanked": False}],
"0.18.3": [],
}
response = httpx.Response(
200,
json={"releases": releases},
request=httpx.Request("GET", "https://pypi.org/pypi/langgraph-api/json"),
)
with patch("langgraph_cli.config.httpx.get", return_value=response):
assert _get_pypi_versions("langgraph-api") == list(releases)
assert _get_pypi_versions("langgraph-api", exclude_yanked=True) == [
"0.18.0",
"0.18.2",
]
def test_config_to_docker_with_api_version():
"""Test config_to_docker function with api_version parameter."""