mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-07 18:27:52 +02:00
feat(cli): add distributed runtime support to langgraph cli (#7096)
This commit is contained in:
@@ -287,6 +287,13 @@ OPT_API_VERSION = click.option(
|
||||
help="API server version to use for the base image. If unspecified, the latest version will be used.",
|
||||
)
|
||||
|
||||
OPT_ENGINE_RUNTIME_MODE = click.option(
|
||||
"--engine-runtime-mode",
|
||||
type=click.Choice(["combined_queue_worker", "distributed"]),
|
||||
default="combined_queue_worker",
|
||||
help="Runtime mode. 'distributed' uses separate executor and orchestrator containers.",
|
||||
)
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.version_option(version=__version__, prog_name="LangGraph CLI")
|
||||
@@ -305,6 +312,7 @@ def cli():
|
||||
@OPT_WATCH
|
||||
@OPT_POSTGRES_URI
|
||||
@OPT_API_VERSION
|
||||
@OPT_ENGINE_RUNTIME_MODE
|
||||
@click.option(
|
||||
"--image",
|
||||
type=str,
|
||||
@@ -339,6 +347,7 @@ def up(
|
||||
debugger_base_url: str | None,
|
||||
postgres_uri: str | None,
|
||||
api_version: str | None,
|
||||
engine_runtime_mode: str,
|
||||
image: str | None,
|
||||
base_image: str | None,
|
||||
):
|
||||
@@ -362,6 +371,7 @@ For production use, requires a license key in env var LANGGRAPH_CLOUD_LICENSE_KE
|
||||
debugger_base_url=debugger_base_url,
|
||||
postgres_uri=postgres_uri,
|
||||
api_version=api_version,
|
||||
engine_runtime_mode=engine_runtime_mode,
|
||||
image=image,
|
||||
base_image=base_image,
|
||||
)
|
||||
@@ -518,6 +528,7 @@ def _build(
|
||||
"\n --base-image langchain/langgraph-server:0.2 # Pin to a minor version (Python)",
|
||||
)
|
||||
@OPT_API_VERSION
|
||||
@OPT_ENGINE_RUNTIME_MODE
|
||||
@click.option(
|
||||
"--install-command",
|
||||
help="Custom install command to run from the build context root. If not provided, auto-detects based on package manager files.",
|
||||
@@ -539,6 +550,7 @@ def build(
|
||||
docker_build_args: Sequence[str],
|
||||
base_image: str | None,
|
||||
api_version: str | None,
|
||||
engine_runtime_mode: str,
|
||||
pull: bool,
|
||||
tag: str,
|
||||
install_command: str | None,
|
||||
@@ -561,12 +573,17 @@ def build(
|
||||
raise click.UsageError("Docker not installed") from None
|
||||
config_json = langgraph_cli.config.validate_config_file(config)
|
||||
warn_non_wolfi_distro(config_json)
|
||||
effective_base_image = base_image
|
||||
if engine_runtime_mode == "distributed" and not base_image:
|
||||
effective_base_image = langgraph_cli.config.default_base_image(
|
||||
config_json, engine_runtime_mode=engine_runtime_mode
|
||||
)
|
||||
_build(
|
||||
runner,
|
||||
set,
|
||||
config,
|
||||
config_json,
|
||||
base_image,
|
||||
effective_base_image,
|
||||
api_version,
|
||||
pull,
|
||||
tag,
|
||||
@@ -1136,6 +1153,7 @@ tests
|
||||
"\n --base-image langchain/langgraph-server:0.2 # Pin to a minor version (Python)",
|
||||
)
|
||||
@OPT_API_VERSION
|
||||
@OPT_ENGINE_RUNTIME_MODE
|
||||
@log_command
|
||||
def dockerfile(
|
||||
save_path: str,
|
||||
@@ -1143,6 +1161,7 @@ def dockerfile(
|
||||
add_docker_compose: bool,
|
||||
base_image: str | None = None,
|
||||
api_version: str | None = None,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
) -> None:
|
||||
save_path = pathlib.Path(save_path).absolute()
|
||||
secho(f"🔍 Validating configuration at path: {config}", fg="yellow")
|
||||
@@ -1150,11 +1169,17 @@ def dockerfile(
|
||||
warn_non_wolfi_distro(config_json)
|
||||
secho("✅ Configuration validated!", fg="green")
|
||||
|
||||
effective_base_image = base_image
|
||||
if engine_runtime_mode == "distributed" and not base_image:
|
||||
effective_base_image = langgraph_cli.config.default_base_image(
|
||||
config_json, engine_runtime_mode=engine_runtime_mode
|
||||
)
|
||||
|
||||
secho(f"📝 Generating Dockerfile at {save_path}", fg="yellow")
|
||||
dockerfile, additional_contexts = langgraph_cli.config.config_to_docker(
|
||||
config_path=config,
|
||||
config=config_json,
|
||||
base_image=base_image,
|
||||
base_image=effective_base_image,
|
||||
api_version=api_version,
|
||||
)
|
||||
with open(str(save_path), "w", encoding="utf-8") as f:
|
||||
@@ -1425,6 +1450,7 @@ def prepare_args_and_stdin(
|
||||
debugger_base_url: str | None = None,
|
||||
postgres_uri: str | None = None,
|
||||
api_version: str | None = None,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
# Like "my-tag" (if you already built it locally)
|
||||
image: str | None = None,
|
||||
# Like "langchain/langgraphjs-api" or "langchain/langgraph-api
|
||||
@@ -1438,9 +1464,10 @@ def prepare_args_and_stdin(
|
||||
debugger_port=debugger_port,
|
||||
debugger_base_url=debugger_base_url,
|
||||
postgres_uri=postgres_uri,
|
||||
image=image, # Pass image to compose YAML generator
|
||||
image=image,
|
||||
base_image=base_image,
|
||||
api_version=api_version,
|
||||
engine_runtime_mode=engine_runtime_mode,
|
||||
)
|
||||
args = [
|
||||
"--project-directory",
|
||||
@@ -1458,6 +1485,7 @@ def prepare_args_and_stdin(
|
||||
base_image=langgraph_cli.config.default_base_image(config),
|
||||
api_version=api_version,
|
||||
image=image,
|
||||
engine_runtime_mode=engine_runtime_mode,
|
||||
)
|
||||
return args, stdin
|
||||
|
||||
@@ -1476,6 +1504,7 @@ def prepare(
|
||||
debugger_base_url: str | None = None,
|
||||
postgres_uri: str | None = None,
|
||||
api_version: str | None = None,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
image: str | None = None,
|
||||
base_image: str | None = None,
|
||||
) -> tuple[list[str], str]:
|
||||
@@ -1492,6 +1521,20 @@ def prepare(
|
||||
verbose=verbose,
|
||||
)
|
||||
)
|
||||
if engine_runtime_mode == "distributed":
|
||||
executor_base = langgraph_cli.config.default_base_image(
|
||||
config_json, engine_runtime_mode="distributed"
|
||||
)
|
||||
runner.run(
|
||||
subp_exec(
|
||||
"docker",
|
||||
"pull",
|
||||
langgraph_cli.config.docker_tag(
|
||||
config_json, executor_base, api_version
|
||||
),
|
||||
verbose=verbose,
|
||||
)
|
||||
)
|
||||
|
||||
args, stdin = prepare_args_and_stdin(
|
||||
capabilities=capabilities,
|
||||
@@ -1504,6 +1547,7 @@ def prepare(
|
||||
debugger_base_url=debugger_base_url or f"http://127.0.0.1:{port}",
|
||||
postgres_uri=postgres_uri,
|
||||
api_version=api_version,
|
||||
engine_runtime_mode=engine_runtime_mode,
|
||||
image=image,
|
||||
base_image=base_image,
|
||||
)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
@@ -1232,11 +1233,15 @@ def node_config_to_docker(
|
||||
return os.linesep.join(docker_file_contents), {}
|
||||
|
||||
|
||||
def default_base_image(config: Config) -> str:
|
||||
def default_base_image(
|
||||
config: Config, engine_runtime_mode: str = "combined_queue_worker"
|
||||
) -> str:
|
||||
if config.get("base_image"):
|
||||
return config["base_image"]
|
||||
if config.get("node_version") and not config.get("python_version"):
|
||||
return "langchain/langgraphjs-api"
|
||||
if engine_runtime_mode == "distributed":
|
||||
return "langchain/langgraph-executor"
|
||||
return "langchain/langgraph-api"
|
||||
|
||||
|
||||
@@ -1329,6 +1334,7 @@ def config_to_compose(
|
||||
api_version: str | None = None,
|
||||
image: str | None = None,
|
||||
watch: bool = False,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
) -> str:
|
||||
base_image = base_image or default_base_image(config)
|
||||
|
||||
@@ -1362,6 +1368,11 @@ def config_to_compose(
|
||||
"""
|
||||
|
||||
else:
|
||||
# Save a pristine copy before config_to_docker mutates graph paths
|
||||
config_snapshot = (
|
||||
copy.deepcopy(config) if engine_runtime_mode == "distributed" else None
|
||||
)
|
||||
|
||||
dockerfile, additional_contexts = config_to_docker(
|
||||
config_path=config_path,
|
||||
config=config,
|
||||
@@ -1379,7 +1390,7 @@ def config_to_compose(
|
||||
additional_contexts:
|
||||
{additional_contexts_str}"""
|
||||
|
||||
return f"""
|
||||
result = f"""
|
||||
{textwrap.indent(env_vars_str, " ")}
|
||||
{env_file_str}
|
||||
pull_policy: build
|
||||
@@ -1389,3 +1400,60 @@ def config_to_compose(
|
||||
{textwrap.indent(dockerfile, " ")}
|
||||
{watch_str}
|
||||
"""
|
||||
|
||||
if engine_runtime_mode == "distributed":
|
||||
executor_base_image = default_base_image(
|
||||
config_snapshot, engine_runtime_mode="distributed"
|
||||
)
|
||||
executor_dockerfile, executor_additional_contexts = config_to_docker(
|
||||
config_path=config_path,
|
||||
config=config_snapshot,
|
||||
base_image=executor_base_image,
|
||||
api_version=api_version,
|
||||
escape_variables=True,
|
||||
)
|
||||
|
||||
executor_additional_contexts_str = "\n".join(
|
||||
f" - {name}: {path}"
|
||||
for name, path in executor_additional_contexts.items()
|
||||
)
|
||||
if executor_additional_contexts_str:
|
||||
executor_additional_contexts_str = f"""
|
||||
additional_contexts:
|
||||
{executor_additional_contexts_str}"""
|
||||
|
||||
postgres_uri = "postgres://postgres:postgres@langgraph-postgres:5432/postgres?sslmode=disable"
|
||||
result += f""" langgraph-orchestrator:
|
||||
image: langchain/langgraph-orchestrator-licensed:latest
|
||||
depends_on:
|
||||
langgraph-api:
|
||||
condition: service_healthy
|
||||
langgraph-postgres:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
DATABASE_URI: {postgres_uri}
|
||||
EXECUTOR_TARGET: langgraph-executor:8188
|
||||
{env_file_str}
|
||||
langgraph-executor:
|
||||
depends_on:
|
||||
langgraph-postgres:
|
||||
condition: service_healthy
|
||||
langgraph-api:
|
||||
condition: service_healthy
|
||||
entrypoint: ["sh", "/storage/executor_entrypoint.sh"]
|
||||
environment:
|
||||
DATABASE_URI: {postgres_uri}
|
||||
REDIS_URI: redis://langgraph-redis:6379
|
||||
EXECUTOR_GRPC_PORT: "8188"
|
||||
ENGINE_GRPC_ADDRESS: "langgraph-orchestrator:50054"
|
||||
LSD_GRPC_SERVER_ADDRESS: "localhost:50050"
|
||||
LANGGRAPH_HTTP: ""
|
||||
{env_file_str}
|
||||
pull_policy: build
|
||||
build:
|
||||
context: .{executor_additional_contexts_str}
|
||||
dockerfile_inline: |
|
||||
{textwrap.indent(executor_dockerfile, " ")}
|
||||
"""
|
||||
|
||||
return result
|
||||
|
||||
@@ -149,6 +149,7 @@ def compose_as_dict(
|
||||
base_image: str | None = None,
|
||||
# API version of the base image
|
||||
api_version: str | None = None,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
) -> dict:
|
||||
"""Create a docker compose file as a dictionary in YML style."""
|
||||
if postgres_uri is None:
|
||||
@@ -207,15 +208,19 @@ def compose_as_dict(
|
||||
)["langgraph-debugger"]
|
||||
|
||||
# Add langgraph-api service
|
||||
api_environment = {
|
||||
"REDIS_URI": "redis://langgraph-redis:6379",
|
||||
"POSTGRES_URI": postgres_uri,
|
||||
}
|
||||
if engine_runtime_mode == "distributed":
|
||||
api_environment["N_JOBS_PER_WORKER"] = '"0"'
|
||||
|
||||
services["langgraph-api"] = {
|
||||
"ports": [f'"{port}:8000"'],
|
||||
"depends_on": {
|
||||
"langgraph-redis": {"condition": "service_healthy"},
|
||||
},
|
||||
"environment": {
|
||||
"REDIS_URI": "redis://langgraph-redis:6379",
|
||||
"POSTGRES_URI": postgres_uri,
|
||||
},
|
||||
"environment": api_environment,
|
||||
}
|
||||
if image:
|
||||
services["langgraph-api"]["image"] = image
|
||||
@@ -255,6 +260,7 @@ def compose(
|
||||
image: str | None = None,
|
||||
base_image: str | None = None,
|
||||
api_version: str | None = None,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
) -> str:
|
||||
"""Create a docker compose file as a string."""
|
||||
compose_content = compose_as_dict(
|
||||
@@ -266,6 +272,7 @@ def compose(
|
||||
image=image,
|
||||
base_image=base_image,
|
||||
api_version=api_version,
|
||||
engine_runtime_mode=engine_runtime_mode,
|
||||
)
|
||||
compose_str = dict_to_yaml(compose_content)
|
||||
return compose_str
|
||||
|
||||
Reference in New Issue
Block a user