mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-11-17 11:23:33 +01:00
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08a2016953 | ||
|
|
ebd3ab4abf | ||
|
|
9b0d8857cc | ||
|
|
cde6bd46a6 | ||
|
|
2adf8a1382 | ||
|
|
ccfb0a1bf8 | ||
|
|
3183e90eca | ||
|
|
8fd97561d6 | ||
|
|
ba0acae562 | ||
|
|
cd95887ec5 | ||
|
|
96686e5700 | ||
|
|
acff17b041 | ||
|
|
46b2ef0e1e | ||
|
|
496885e569 | ||
|
|
23a77c57fd | ||
|
|
40f5cc7f1f | ||
|
|
ef6490a521 | ||
|
|
a054ed08dc |
No files matched your search
Generated
+1
-1
@@ -279,7 +279,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
source = { editable = "../checkpoint" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
@@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
from collections import defaultdict
|
||||
from collections.abc import AsyncIterator, Iterator, Mapping, Sequence
|
||||
from contextlib import asynccontextmanager
|
||||
from contextlib import asynccontextmanager, nullcontext
|
||||
from typing import Any, cast
|
||||
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
@@ -425,7 +425,12 @@ class AsyncPostgresSaver(BasePostgresSaver):
|
||||
Will be applied regardless of whether the AsyncPostgresSaver instance was initialized with a pipeline.
|
||||
If pipeline mode is not supported, will fall back to using transaction context manager.
|
||||
"""
|
||||
async with self.lock, _ainternal.get_connection(self.conn) as conn:
|
||||
# A pool hands each call its own connection, so only a shared one needs
|
||||
# the lock.
|
||||
lock = (
|
||||
nullcontext() if isinstance(self.conn, AsyncConnectionPool) else self.lock
|
||||
)
|
||||
async with lock, _ainternal.get_connection(self.conn) as conn:
|
||||
if self.pipe:
|
||||
# a connection in pipeline mode can be used concurrently
|
||||
# in multiple threads/coroutines, but only one cursor can be
|
||||
@@ -594,7 +599,11 @@ class AsyncPostgresSaver(BasePostgresSaver):
|
||||
if value["parent_checkpoint_id"]
|
||||
else None
|
||||
),
|
||||
await asyncio.to_thread(self._load_writes, value["pending_writes"]),
|
||||
(
|
||||
await asyncio.to_thread(self._load_writes, value["pending_writes"])
|
||||
if value["pending_writes"]
|
||||
else []
|
||||
),
|
||||
)
|
||||
|
||||
def list(
|
||||
|
||||
@@ -413,7 +413,14 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con
|
||||
) -> None:
|
||||
# Keep `conn` for compatibility with subclasses overriding this private hook.
|
||||
# All database I/O goes through `_cursor()`, which owns connection acquisition.
|
||||
async with self._cursor(pipeline=True) as cur:
|
||||
# A lone read is one statement: outside a pipeline it costs one round
|
||||
# trip instead of two (its fetch, then the Sync on leaving the pipeline).
|
||||
lone_read = (
|
||||
not self.pipe
|
||||
and PutOp not in grouped_ops
|
||||
and sum(map(len, grouped_ops.values())) == 1
|
||||
)
|
||||
async with self._cursor(pipeline=not lone_read) as cur:
|
||||
if GetOp in grouped_ops:
|
||||
await self._batch_get_ops(
|
||||
cast(Sequence[tuple[int, GetOp]], grouped_ops[GetOp]),
|
||||
|
||||
@@ -977,14 +977,19 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]):
|
||||
):
|
||||
yield cur
|
||||
else:
|
||||
with conn.cursor(binary=True, row_factory=dict_row) as cur:
|
||||
# Lone reads land here, so take the lock like the pipeline branch:
|
||||
# another thread may be holding this connection in a pipeline.
|
||||
with self.lock, conn.cursor(binary=True, row_factory=dict_row) as cur:
|
||||
yield cur
|
||||
|
||||
def batch(self, ops: Iterable[Op]) -> list[Result]:
|
||||
grouped_ops, num_ops = _group_ops(ops)
|
||||
results: list[Result] = [None] * num_ops
|
||||
|
||||
with self._cursor(pipeline=True) as cur:
|
||||
# A lone read is one statement: outside a pipeline it costs one round
|
||||
# trip instead of two (its fetch, then the Sync on leaving the pipeline).
|
||||
lone_read = num_ops == 1 and PutOp not in grouped_ops and not self.pipe
|
||||
with self._cursor(pipeline=not lone_read) as cur:
|
||||
if GetOp in grouped_ops:
|
||||
self._batch_get_ops(
|
||||
cast(Sequence[tuple[int, GetOp]], grouped_ops[GetOp]), results, cur
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph-checkpoint-postgres"
|
||||
version = "3.2.0"
|
||||
version = "3.2.1"
|
||||
description = "Library with a Postgres implementation of LangGraph checkpoint saver."
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# type: ignore
|
||||
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
@@ -417,3 +418,33 @@ async def test_delta_channel_chain_reconstruction(saver_name: str) -> None:
|
||||
assert msgs[1].content == "reply-1"
|
||||
assert msgs[2].content == "there"
|
||||
assert msgs[3].content == "reply-3"
|
||||
|
||||
|
||||
async def test_pool_saver_reads_while_a_list_is_open(test_data) -> None:
|
||||
async with _pool_saver() as saver:
|
||||
config = await saver.aput(
|
||||
test_data["configs"][0],
|
||||
test_data["checkpoints"][0],
|
||||
test_data["metadata"][0],
|
||||
{},
|
||||
)
|
||||
listing = saver.alist(None)
|
||||
try:
|
||||
assert await anext(listing) is not None
|
||||
assert await asyncio.wait_for(saver.aget_tuple(config), timeout=5)
|
||||
finally:
|
||||
await listing.aclose()
|
||||
|
||||
|
||||
async def test_base_saver_serializes_concurrent_calls(test_data) -> None:
|
||||
async with _base_saver() as saver:
|
||||
config = await saver.aput(
|
||||
test_data["configs"][0],
|
||||
test_data["checkpoints"][0],
|
||||
test_data["metadata"][0],
|
||||
{},
|
||||
)
|
||||
await asyncio.gather(
|
||||
*(saver.aput_writes(config, [("x", i)], str(uuid4())) for i in range(20)),
|
||||
*(saver.aget_tuple(config) for _ in range(20)),
|
||||
)
|
||||
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
from typing import Any
|
||||
@@ -289,6 +290,30 @@ def test_basic_store_ops(store) -> None:
|
||||
assert deleted_item is None
|
||||
|
||||
|
||||
def test_lone_reads_and_puts_share_a_connection(store: PostgresStore) -> None:
|
||||
errors: list[BaseException] = []
|
||||
|
||||
def run(op: Any) -> None:
|
||||
try:
|
||||
for i in range(100):
|
||||
op(i)
|
||||
except BaseException as e:
|
||||
errors.append(e)
|
||||
|
||||
def read(i: int) -> None:
|
||||
store.get(("race",), f"k{i % 10}")
|
||||
|
||||
def write(i: int) -> None:
|
||||
store.put(("race",), f"k{i % 10}", {"i": i})
|
||||
|
||||
threads = [threading.Thread(target=run, args=(op,)) for op in [read, write] * 4]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
assert errors == []
|
||||
|
||||
|
||||
def test_list_namespaces(store) -> None:
|
||||
# Create test data with various namespaces
|
||||
test_namespaces = [
|
||||
|
||||
Generated
+2
-2
@@ -278,7 +278,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
source = { editable = "../checkpoint" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -353,7 +353,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint-postgres"
|
||||
version = "3.2.0"
|
||||
version = "3.2.1"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "langgraph-checkpoint" },
|
||||
|
||||
Generated
+1
-1
@@ -285,7 +285,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
source = { editable = "../checkpoint" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
@@ -302,14 +302,20 @@ def _dedupe_ops(values: list[Op]) -> tuple[list[int] | None, list[Op]]:
|
||||
dedupped: list[Op] = []
|
||||
listen: list[int] = []
|
||||
puts: dict[tuple[tuple[str, ...], str], int] = {}
|
||||
reads: dict[Op, int] = {}
|
||||
|
||||
for op in values:
|
||||
if isinstance(op, (GetOp, SearchOp, ListNamespacesOp)):
|
||||
try:
|
||||
listen.append(dedupped.index(op))
|
||||
except ValueError:
|
||||
listen.append(len(dedupped))
|
||||
ix = reads.setdefault(op, len(dedupped))
|
||||
except TypeError: # an op holding a dict or a list is unhashable
|
||||
try:
|
||||
ix = dedupped.index(op)
|
||||
except ValueError:
|
||||
ix = len(dedupped)
|
||||
if ix == len(dedupped):
|
||||
dedupped.append(op)
|
||||
listen.append(ix)
|
||||
elif isinstance(op, PutOp):
|
||||
putkey = (op.namespace, op.key)
|
||||
if putkey in puts:
|
||||
|
||||
@@ -248,13 +248,20 @@ class InMemoryStore(BaseStore):
|
||||
for key, filter_value in op.filter.items()
|
||||
)
|
||||
|
||||
# Without a filter or a query embedding, the caller keeps only
|
||||
# filtered[offset:offset + limit], so stop collecting there. Negative
|
||||
# bounds keep the full scan, since a slice counts them from the end.
|
||||
stop = (
|
||||
op.offset + op.limit
|
||||
if not op.filter
|
||||
and not (op.query and self.embeddings)
|
||||
and min(op.offset, op.limit) >= 0
|
||||
else None
|
||||
)
|
||||
n = len(namespace_prefix)
|
||||
filtered = []
|
||||
for namespace in self._data:
|
||||
if not (
|
||||
namespace[: len(namespace_prefix)] == namespace_prefix
|
||||
if len(namespace) >= len(namespace_prefix)
|
||||
else False
|
||||
):
|
||||
if namespace[:n] != namespace_prefix:
|
||||
continue
|
||||
|
||||
for key, item in self._data[namespace].items():
|
||||
@@ -263,6 +270,8 @@ class InMemoryStore(BaseStore):
|
||||
filtered.append((item, list(embeddings.values())))
|
||||
else:
|
||||
filtered.append((item, []))
|
||||
if len(filtered) == stop:
|
||||
return filtered
|
||||
return filtered
|
||||
|
||||
def _embed_search_queries(
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
description = "Library with base interfaces for LangGraph checkpoint savers."
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
|
||||
@@ -22,7 +22,7 @@ from langgraph.store.base import (
|
||||
get_text_at_path,
|
||||
validate_op_namespace,
|
||||
)
|
||||
from langgraph.store.base.batch import AsyncBatchedBaseStore
|
||||
from langgraph.store.base.batch import AsyncBatchedBaseStore, _dedupe_ops
|
||||
from langgraph.store.memory import InMemoryStore
|
||||
from tests.embed_test_utils import CharacterEmbeddings
|
||||
|
||||
@@ -75,6 +75,29 @@ async def test_async_batch_store_resilience() -> None:
|
||||
assert result2.value == doc2
|
||||
|
||||
|
||||
def test_dedupe_ops_maps_repeated_reads_and_puts_to_one_op() -> None:
|
||||
ops = [
|
||||
GetOp(("a",), "k"),
|
||||
PutOp(("a",), "k", {"v": 1}),
|
||||
GetOp(("a",), "k"),
|
||||
SearchOp(("a",), filter={"x": 1}),
|
||||
SearchOp(("a",), filter={"x": 1}),
|
||||
PutOp(("a",), "k", {"v": 2}),
|
||||
GetOp(("b",), "k"),
|
||||
ListNamespacesOp(match_conditions=(MatchCondition("prefix", ("a",)),)),
|
||||
ListNamespacesOp(match_conditions=(MatchCondition("prefix", ("a",)),)),
|
||||
]
|
||||
listen, dedupped = _dedupe_ops(ops)
|
||||
assert dedupped == [
|
||||
GetOp(("a",), "k"),
|
||||
PutOp(("a",), "k", {"v": 2}),
|
||||
SearchOp(("a",), filter={"x": 1}),
|
||||
GetOp(("b",), "k"),
|
||||
ListNamespacesOp(match_conditions=(MatchCondition("prefix", ("a",)),)),
|
||||
]
|
||||
assert listen == [0, 1, 0, 2, 2, 1, 3, 4, 4]
|
||||
|
||||
|
||||
def test_get_text_at_path() -> None:
|
||||
nested_data = {
|
||||
"name": "test",
|
||||
@@ -437,6 +460,43 @@ def test_list_namespaces_no_conditions() -> None:
|
||||
assert sorted(result) == sorted(expected)
|
||||
|
||||
|
||||
def test_search_pages_match_full_listing() -> None:
|
||||
store = InMemoryStore()
|
||||
items_per_namespace = 7
|
||||
for ns in [("a",), ("a", "b"), ("ab",), ("a", "c", "d")]:
|
||||
for i in range(items_per_namespace):
|
||||
store.put(ns, f"k{i}", {"i": i, "even": i % 2 == 0})
|
||||
|
||||
def keys(items: list) -> list[tuple[tuple[str, ...], str]]:
|
||||
return [(item.namespace, item.key) for item in items]
|
||||
|
||||
full = keys(store.search(("a",), limit=100))
|
||||
assert {ns for ns, _ in full} == {("a",), ("a", "b"), ("a", "c", "d")}
|
||||
assert len(full) == 3 * items_per_namespace
|
||||
paged = [
|
||||
key
|
||||
for offset in range(0, len(full), 4)
|
||||
for key in keys(store.search(("a",), limit=4, offset=offset))
|
||||
]
|
||||
assert paged == full
|
||||
even = keys(store.search(("a",), filter={"even": True}, limit=100))
|
||||
assert (
|
||||
keys(store.search(("a",), filter={"even": True}, limit=3, offset=2))
|
||||
== even[2:5]
|
||||
)
|
||||
|
||||
|
||||
def test_vector_search_scores_every_item(fake_embeddings: CharacterEmbeddings) -> None:
|
||||
store = InMemoryStore(
|
||||
index={"dims": fake_embeddings.dims, "embed": fake_embeddings}
|
||||
)
|
||||
for i in range(4):
|
||||
store.put(("test",), f"other{i}", {"text": f"zzz qqq {i}"})
|
||||
store.put(("test",), "match", {"text": "apple pie"})
|
||||
results = store.search(("test",), query="apple pie", limit=1)
|
||||
assert [r.key for r in results] == ["match"]
|
||||
|
||||
|
||||
def test_list_namespaces_empty_store() -> None:
|
||||
store = InMemoryStore()
|
||||
|
||||
|
||||
Generated
+1
-1
@@ -394,7 +394,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
+35
-3
@@ -95,11 +95,43 @@ as hidden compatibility aliases, with no removal scheduled. Older Python CLI
|
||||
versions and the JavaScript CLI may still require the legacy spellings; check
|
||||
command help for your installed version.
|
||||
|
||||
#### Where `deploy` sends requests
|
||||
|
||||
All `deploy` commands need only a LangSmith API key (`LANGSMITH_API_KEY`). The CLI
|
||||
asks the LangSmith API (`LANGSMITH_ENDPOINT`, or `https://api.smith.langchain.com`
|
||||
by default) which workspaces the key can use. If the workspace runs on a
|
||||
LangSmith data plane in your own cloud (BYOC on AWS, Azure, or GCP), the CLI
|
||||
talks to that data plane's host API and sets the workspace for you. The data
|
||||
plane URL must use https, since the API key is sent there. Status links still
|
||||
point at the LangSmith UI. If the key's workspaces sit on different data planes,
|
||||
set `LANGSMITH_TENANT_ID` to the workspace ID to reach its data plane; without
|
||||
it, the CLI starts on the LangSmith control plane, and the workspace ID you
|
||||
enter when asked picks the data plane. A `LANGSMITH_TENANT_ID` the key can't
|
||||
use is an error. If the workspace lookup fails, the CLI says so and uses the
|
||||
LangSmith control plane.
|
||||
|
||||
`--host-url` (or `LANGGRAPH_HOST_URL`) sends every request to the given host
|
||||
backend URL instead and skips this lookup.
|
||||
|
||||
#### What a data plane supports
|
||||
|
||||
On a BYOC data plane, the data plane decides which deployments `deploy` can make:
|
||||
|
||||
- Building with `deploy` (a local Docker build, or `--remote`) needs a data plane
|
||||
that supports CLI builds. Otherwise, push to a registry you manage with
|
||||
`--push-to <registry>/<repository>`, or deploy an existing image with
|
||||
`--image-uri`. Both work on every data plane.
|
||||
- `--agent-server-type` defaults to `serverless`, or to `dev` on a data plane
|
||||
without scale to zero. Asking for `serverless` or `dedicated` on a data plane
|
||||
that doesn't support it fails before anything is created.
|
||||
|
||||
### `langgraph deploy metrics`
|
||||
|
||||
Fetch Agent Server monitoring time series as JSON through the LangSmith control plane.
|
||||
Use `langgraph deploy list` to find the Agent Server ID. Authentication uses the same
|
||||
API key and workspace configuration as the other `deploy` commands.
|
||||
Fetch Agent Server monitoring time series as JSON. Like the other `deploy`
|
||||
commands, it finds the API key's workspace and sends the request to that
|
||||
workspace's control plane or data plane (see
|
||||
[Where `deploy` sends requests](#where-deploy-sends-requests)). Use
|
||||
`langgraph deploy list` to find the Agent Server ID.
|
||||
|
||||
```bash
|
||||
langgraph deploy metrics AGENT_SERVER_ID \
|
||||
|
||||
@@ -31,7 +31,12 @@ from langgraph_cli.host_backend import (
|
||||
ControlPlaneEndpoints,
|
||||
HostBackendClient,
|
||||
HostBackendError,
|
||||
InstanceFlags,
|
||||
LangSmithApiClient,
|
||||
SourceName,
|
||||
Workspace,
|
||||
WorkspaceRouter,
|
||||
langsmith_api_url,
|
||||
)
|
||||
from langgraph_cli.image_reference import DIGEST_SEPARATOR, ImageReference
|
||||
from langgraph_cli.progress import Progress
|
||||
@@ -108,6 +113,20 @@ _LISTENER_REQUIRED_MARKER = "listener_id' is required"
|
||||
_LISTENERS_SHOWN = 10
|
||||
_LISTENER_NOT_FOUND_STATUSES = frozenset({404, 422})
|
||||
_LISTENERS_DOCS_URL = "https://docs.langchain.com/langsmith/control-plane#listeners"
|
||||
_DEFAULT_AGENT_SERVER_TYPE = "serverless"
|
||||
_WIRE_DEPLOYMENT_TYPES = {"serverless": "dev_zero", "dedicated": "prod"}
|
||||
_SOURCE_REJECTED_PREFIX = "'source' must be"
|
||||
_RESOURCE_SPEC_REQUIRED_MARKER = "'source_config.resource_spec' is required"
|
||||
_HOST_URL_ON_A_DATA_PLANE = (
|
||||
"If --host-url or LANGGRAPH_HOST_URL points at a BYOC data plane, drop it so "
|
||||
"'langgraph deploy' finds the data plane from your API key."
|
||||
)
|
||||
_CLI_BUILDS_UNSUPPORTED = (
|
||||
"This LangSmith data plane does not build images for 'langgraph deploy'. "
|
||||
"Push the image to a registry you manage with --push-to "
|
||||
"<registry>/<repository>, or deploy an image that is already there with "
|
||||
"--image-uri."
|
||||
)
|
||||
_NO_LISTENERS = (
|
||||
"This workspace has no listeners, so --listener-id and --k8s-namespace "
|
||||
"do not apply."
|
||||
@@ -1512,11 +1531,10 @@ def _run_remote_build(
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class DeployContext:
|
||||
client: HostBackendClient
|
||||
endpoints: ControlPlaneEndpoints
|
||||
spec: BuildSpec
|
||||
verbose: bool
|
||||
selector: DeploymentSelector
|
||||
deployment_type: str
|
||||
agent_server_type: str | None
|
||||
secrets: list[dict[str, str]]
|
||||
tracked_packages: list[str] | None
|
||||
|
||||
@@ -1531,17 +1549,70 @@ class DeploymentSource(Protocol):
|
||||
def run(self, ctx: DeployContext) -> DeployOutcome: ...
|
||||
|
||||
|
||||
def _unsupported_agent_server_type(label: str) -> click.UsageError:
|
||||
return click.UsageError(
|
||||
f"This LangSmith data plane does not support {label} Agent Servers. "
|
||||
"Use --agent-server-type dev instead."
|
||||
)
|
||||
|
||||
|
||||
def _require_cli_builds(client: HostBackendClient) -> InstanceFlags | None:
|
||||
flags = client.instance_flags()
|
||||
if flags is not None and not flags.cli_builds:
|
||||
raise click.UsageError(_CLI_BUILDS_UNSUPPORTED)
|
||||
return flags
|
||||
|
||||
|
||||
def _data_plane_source_config(
|
||||
flags: InstanceFlags, agent_server_type: str | None
|
||||
) -> dict[str, object]:
|
||||
requested = agent_server_type or (
|
||||
_DEFAULT_AGENT_SERVER_TYPE if flags.scale_to_zero else "dev"
|
||||
)
|
||||
if requested == "dev":
|
||||
return {"resource_spec": _OPERATOR_DEFAULT_RESOURCE_SPEC}
|
||||
if requested == "serverless":
|
||||
if not flags.scale_to_zero:
|
||||
raise _unsupported_agent_server_type("serverless (scale to zero)")
|
||||
return {
|
||||
"resource_spec": _OPERATOR_DEFAULT_RESOURCE_SPEC,
|
||||
"deployment_type": _WIRE_DEPLOYMENT_TYPES["serverless"],
|
||||
}
|
||||
if not flags.dedicated:
|
||||
raise _unsupported_agent_server_type("dedicated")
|
||||
return {
|
||||
"resource_spec": None,
|
||||
"deployment_type": _WIRE_DEPLOYMENT_TYPES["dedicated"],
|
||||
}
|
||||
|
||||
|
||||
def _new_deployment_source_config(
|
||||
flags: InstanceFlags | None, agent_server_type: str | None
|
||||
) -> dict[str, object]:
|
||||
if flags is not None:
|
||||
return _data_plane_source_config(flags, agent_server_type)
|
||||
requested = agent_server_type or _DEFAULT_AGENT_SERVER_TYPE
|
||||
return {"deployment_type": _WIRE_DEPLOYMENT_TYPES.get(requested, requested)}
|
||||
|
||||
|
||||
def _with_hint(err: HostBackendError, hint: str) -> HostBackendError:
|
||||
return HostBackendError(
|
||||
f"{err.message}\n{hint}", status_code=err.status_code, detail=err.detail
|
||||
)
|
||||
|
||||
|
||||
def _resolve_or_create(
|
||||
ctx: DeployContext, *, source: SourceName, not_found_message: str
|
||||
) -> tuple[str, int]:
|
||||
if isinstance(ctx.selector, ById):
|
||||
existing, step = _fetch_deployment(ctx.client, 1, ctx.selector)
|
||||
else:
|
||||
existing, step = _find_deployment(
|
||||
ctx.client, 1, ctx.selector, not_found_message=not_found_message
|
||||
)
|
||||
flags = _require_cli_builds(ctx.client)
|
||||
if existing is not None:
|
||||
return existing.id, step
|
||||
found, step = _find_deployment(
|
||||
ctx.client, 1, ctx.selector, not_found_message=not_found_message
|
||||
)
|
||||
if found is not None:
|
||||
return found.id, step
|
||||
try:
|
||||
created, step = _create_deployment(
|
||||
ctx.client,
|
||||
@@ -1549,7 +1620,7 @@ def _resolve_or_create(
|
||||
name=ctx.selector.name if isinstance(ctx.selector, ByName) else None,
|
||||
agent=asdict(ctx.selector) if isinstance(ctx.selector, ByAgent) else None,
|
||||
source=source,
|
||||
source_config={"deployment_type": ctx.deployment_type},
|
||||
source_config=_new_deployment_source_config(flags, ctx.agent_server_type),
|
||||
source_revision_config={},
|
||||
secrets=ctx.secrets,
|
||||
)
|
||||
@@ -1557,11 +1628,15 @@ def _resolve_or_create(
|
||||
if err.status_code == 400 and err.detail == (
|
||||
"Scale to zero deployments are not supported in AWS environments"
|
||||
):
|
||||
raise HostBackendError(
|
||||
f"{err.message}\nUse --agent-server-type dev instead.",
|
||||
status_code=err.status_code,
|
||||
detail=err.detail,
|
||||
) from None
|
||||
raise _with_hint(err, "Use --agent-server-type dev instead.") from None
|
||||
if err.status_code == 400 and (err.detail or "").startswith(
|
||||
_SOURCE_REJECTED_PREFIX
|
||||
):
|
||||
raise _with_hint(err, _CLI_BUILDS_UNSUPPORTED) from None
|
||||
if err.status_code == 400 and _RESOURCE_SPEC_REQUIRED_MARKER in (
|
||||
err.detail or ""
|
||||
):
|
||||
raise _with_hint(err, _HOST_URL_ON_A_DATA_PLANE) from None
|
||||
if _needs_a_listener(err):
|
||||
raise ListenerRequiredError(
|
||||
"The image has to come from a registry you manage, so re-run with "
|
||||
@@ -1787,7 +1862,7 @@ class CustomerRegistrySource:
|
||||
requested = self.create_only.placement
|
||||
if requested.listener_id is not None:
|
||||
return requested.on(_requested_listener(ctx.client, requested.listener_id))
|
||||
if not (ctx.endpoints.is_cloud or requested.requested):
|
||||
if not (ctx.client.endpoints.is_cloud or requested.requested):
|
||||
return Unplaced()
|
||||
return requested.among(_available_listeners(ctx.client))
|
||||
|
||||
@@ -1995,16 +2070,60 @@ def _create_host_backend_client(
|
||||
fg="yellow",
|
||||
)
|
||||
resolved_api_key = click.prompt("Enter LangSmith API key", hide_input=True)
|
||||
tenant_id = env_vars.get("LANGSMITH_TENANT_ID") or os.environ.get(
|
||||
"LANGSMITH_TENANT_ID"
|
||||
tenant_id = (
|
||||
env_vars.get("LANGSMITH_TENANT_ID")
|
||||
or os.environ.get("LANGSMITH_TENANT_ID")
|
||||
or None
|
||||
)
|
||||
langsmith_endpoint = env_vars.get("LANGSMITH_ENDPOINT") or os.environ.get(
|
||||
"LANGSMITH_ENDPOINT"
|
||||
)
|
||||
endpoints = ControlPlaneEndpoints.resolve(host_url, langsmith_endpoint)
|
||||
return HostBackendClient(
|
||||
endpoints.control_plane_url, resolved_api_key, tenant_id=tenant_id
|
||||
if host_url:
|
||||
return HostBackendClient(host_url, resolved_api_key, tenant_id)
|
||||
workspaces = _list_workspaces(
|
||||
LangSmithApiClient(langsmith_api_url(langsmith_endpoint), resolved_api_key)
|
||||
)
|
||||
target = _discover_target(workspaces, tenant_id)
|
||||
return HostBackendClient(
|
||||
ControlPlaneEndpoints.for_workspace(target.data_plane_url, langsmith_endpoint),
|
||||
resolved_api_key,
|
||||
target.tenant_id,
|
||||
router=WorkspaceRouter(workspaces, langsmith_endpoint),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class HostTarget:
|
||||
tenant_id: str | None
|
||||
data_plane_url: str | None = None
|
||||
|
||||
|
||||
def _list_workspaces(langsmith: LangSmithApiClient) -> list[Workspace]:
|
||||
try:
|
||||
return langsmith.list_workspaces()
|
||||
except HostBackendError:
|
||||
click.secho(
|
||||
"Could not look up this API key's LangSmith workspaces; "
|
||||
"using the default LangSmith control plane.",
|
||||
fg="yellow",
|
||||
err=True,
|
||||
)
|
||||
return []
|
||||
|
||||
|
||||
def _discover_target(workspaces: list[Workspace], tenant_id: str | None) -> HostTarget:
|
||||
if tenant_id is not None and workspaces:
|
||||
workspaces = [w for w in workspaces if w.has_id(tenant_id)]
|
||||
if not workspaces:
|
||||
raise click.ClickException(
|
||||
f"This API key has no access to workspace {tenant_id}. "
|
||||
"Check LANGSMITH_TENANT_ID."
|
||||
)
|
||||
data_plane_urls = {w.data_plane_url for w in workspaces}
|
||||
if len(data_plane_urls) != 1 or None in data_plane_urls:
|
||||
return HostTarget(tenant_id)
|
||||
only_workspace_id = workspaces[0].id if len(workspaces) == 1 else None
|
||||
return HostTarget(only_workspace_id, data_plane_urls.pop())
|
||||
|
||||
|
||||
def _call_host_backend_with_optional_tenant(
|
||||
@@ -2227,12 +2346,13 @@ def _deploy_base_options(
|
||||
"--agent-server-type",
|
||||
"deployment_type",
|
||||
type=click.Choice(["serverless", "dedicated", "dev", "prod"]),
|
||||
default="serverless",
|
||||
show_default=True,
|
||||
default=None,
|
||||
show_default=_DEFAULT_AGENT_SERVER_TYPE,
|
||||
help=(
|
||||
"Agent Server type (used when creating a new Agent Server): "
|
||||
"serverless (scale to zero) or dedicated (prod); "
|
||||
"legacy dev and prod are also accepted. "
|
||||
"legacy dev and prod are also accepted. Defaults to dev on "
|
||||
"a LangSmith data plane without scale to zero. "
|
||||
"Ignored with --push-to."
|
||||
),
|
||||
),
|
||||
@@ -2417,7 +2537,7 @@ def _deploy_cmd(
|
||||
host_url: str | None,
|
||||
api_key: str | None,
|
||||
deployment_id: str | None,
|
||||
deployment_type: str,
|
||||
deployment_type: str | None,
|
||||
name: str | None,
|
||||
agent_id: str | None,
|
||||
environment: str | None,
|
||||
@@ -2517,7 +2637,6 @@ def _deploy_cmd(
|
||||
outcome = source.run(
|
||||
DeployContext(
|
||||
client=client,
|
||||
endpoints=client.endpoints,
|
||||
spec=BuildSpec(
|
||||
config=config,
|
||||
config_json=config_json,
|
||||
@@ -2530,9 +2649,7 @@ def _deploy_cmd(
|
||||
),
|
||||
verbose=verbose,
|
||||
selector=selector,
|
||||
deployment_type={"serverless": "dev_zero", "dedicated": "prod"}.get(
|
||||
deployment_type, deployment_type
|
||||
),
|
||||
agent_server_type=deployment_type,
|
||||
secrets=secrets,
|
||||
tracked_packages=tracked_packages,
|
||||
)
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Literal
|
||||
from urllib.parse import urlparse
|
||||
from uuid import UUID
|
||||
|
||||
import click
|
||||
import httpx
|
||||
@@ -16,10 +17,13 @@ CLOUD_DOMAIN = "langchain.com"
|
||||
CLOUD_API_HOST = "api.smith.langchain.com"
|
||||
CLOUD_CONTROL_PLANE_HOST = "api.host.langchain.com"
|
||||
CLOUD_DASHBOARD_HOST = "smith.langchain.com"
|
||||
CLOUD_LANGSMITH_API_URL = f"https://{CLOUD_API_HOST}"
|
||||
CONTROL_PLANE_PATH = "/api-host"
|
||||
INFO_PATH = "/api/v1/info"
|
||||
LANGSMITH_API_PATHS = ("/api/v1", "/api")
|
||||
LOCAL_HOSTNAMES = ("localhost", "127.0.0.1")
|
||||
MAX_PAGE_SIZE = 100
|
||||
WORKSPACE_LOOKUP_TIMEOUT_SECONDS = 5
|
||||
SourceName = Literal["internal_docker", "internal_source", "external_docker"]
|
||||
|
||||
|
||||
@@ -27,6 +31,7 @@ SourceName = Literal["internal_docker", "internal_source", "external_docker"]
|
||||
class ControlPlaneEndpoints:
|
||||
control_plane_url: str
|
||||
dashboard_url: str
|
||||
info_url: str | None = None
|
||||
|
||||
@classmethod
|
||||
def resolve(
|
||||
@@ -50,22 +55,66 @@ class ControlPlaneEndpoints:
|
||||
control_plane_url = url.rstrip("/")
|
||||
hostname = urlparse(control_plane_url).hostname or ""
|
||||
if control_plane_url.endswith(CONTROL_PLANE_PATH):
|
||||
return cls(control_plane_url, control_plane_url[: -len(CONTROL_PLANE_PATH)])
|
||||
return cls(
|
||||
control_plane_url, control_plane_url.removesuffix(CONTROL_PLANE_PATH)
|
||||
)
|
||||
if hostname in LOCAL_HOSTNAMES:
|
||||
return cls(control_plane_url, control_plane_url)
|
||||
return cls(control_plane_url, _cloud_dashboard_for(hostname))
|
||||
|
||||
@classmethod
|
||||
def from_langsmith_endpoint(cls, endpoint: str) -> ControlPlaneEndpoints:
|
||||
parsed = urlparse(endpoint.rstrip("/"))
|
||||
hostname = parsed.hostname or ""
|
||||
hostname = urlparse(endpoint).hostname or ""
|
||||
if _is_cloud_host(hostname):
|
||||
return cls.from_control_plane_url(
|
||||
f"https://{_cloud_control_plane_host_for(hostname)}"
|
||||
)
|
||||
root = f"{parsed.scheme}://{parsed.netloc}{_without_api_path(parsed.path)}"
|
||||
root = langsmith_api_url(endpoint)
|
||||
return cls(f"{root}{CONTROL_PLANE_PATH}", root)
|
||||
|
||||
@classmethod
|
||||
def on_data_plane(
|
||||
cls, data_plane_url: str, langsmith_endpoint: str | None
|
||||
) -> ControlPlaneEndpoints:
|
||||
_require_https(data_plane_url)
|
||||
endpoint_is_the_data_plane = (
|
||||
langsmith_endpoint is not None
|
||||
and urlparse(langsmith_endpoint).hostname
|
||||
== urlparse(data_plane_url).hostname
|
||||
)
|
||||
dashboard_url = cls.resolve(
|
||||
None, None if endpoint_is_the_data_plane else langsmith_endpoint
|
||||
).dashboard_url
|
||||
root = data_plane_url.rstrip("/")
|
||||
return cls(f"{root}{CONTROL_PLANE_PATH}", dashboard_url, f"{root}{INFO_PATH}")
|
||||
|
||||
@classmethod
|
||||
def for_workspace(
|
||||
cls, data_plane_url: str | None, langsmith_endpoint: str | None
|
||||
) -> ControlPlaneEndpoints:
|
||||
if data_plane_url:
|
||||
return cls.on_data_plane(data_plane_url, langsmith_endpoint)
|
||||
return cls.resolve(None, langsmith_endpoint)
|
||||
|
||||
|
||||
def _require_https(url: str) -> None:
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme == "https" or (
|
||||
parsed.scheme == "http" and parsed.hostname in LOCAL_HOSTNAMES
|
||||
):
|
||||
return
|
||||
raise click.ClickException(
|
||||
f"Refusing to send your API key to the data plane at {url}: "
|
||||
"data plane URLs must use https."
|
||||
)
|
||||
|
||||
|
||||
def langsmith_api_url(langsmith_endpoint: str | None) -> str:
|
||||
if not langsmith_endpoint:
|
||||
return CLOUD_LANGSMITH_API_URL
|
||||
parsed = urlparse(langsmith_endpoint.rstrip("/"))
|
||||
return f"{parsed.scheme}://{parsed.netloc}{_without_api_path(parsed.path)}"
|
||||
|
||||
|
||||
def _is_cloud_host(hostname: str) -> bool:
|
||||
return hostname == CLOUD_DOMAIN or hostname.endswith(f".{CLOUD_DOMAIN}")
|
||||
@@ -124,45 +173,80 @@ def _error_detail(response: httpx.Response) -> str | None:
|
||||
return detail if isinstance(detail, str) else None
|
||||
|
||||
|
||||
class HostBackendClient:
|
||||
"""Minimal JSON HTTP client for the host backend deployment service."""
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Workspace:
|
||||
id: str
|
||||
data_plane_url: str | None
|
||||
|
||||
@classmethod
|
||||
def from_api(cls, workspace: dict[str, Any]) -> Workspace:
|
||||
data_plane_url = (workspace.get("data_plane_url") or "").rstrip("/")
|
||||
return cls(workspace["id"], data_plane_url or None)
|
||||
|
||||
def has_id(self, workspace_id: str) -> bool:
|
||||
return _as_uuid(self.id) == _as_uuid(workspace_id)
|
||||
|
||||
|
||||
def _as_uuid(workspace_id: str) -> UUID | str:
|
||||
try:
|
||||
return UUID(workspace_id)
|
||||
except ValueError:
|
||||
return workspace_id
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class WorkspaceRouter:
|
||||
workspaces: Sequence[Workspace]
|
||||
langsmith_endpoint: str | None
|
||||
|
||||
def endpoints_for(self, workspace_id: str) -> ControlPlaneEndpoints | None:
|
||||
workspace = next((w for w in self.workspaces if w.has_id(workspace_id)), None)
|
||||
if workspace is None:
|
||||
return None
|
||||
return ControlPlaneEndpoints.for_workspace(
|
||||
workspace.data_plane_url, self.langsmith_endpoint
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class InstanceFlags:
|
||||
scale_to_zero: bool
|
||||
dedicated: bool
|
||||
cli_builds: bool
|
||||
|
||||
@classmethod
|
||||
def from_info(cls, info: object) -> InstanceFlags:
|
||||
flags = info.get("instance_flags") if isinstance(info, dict) else None
|
||||
if not isinstance(flags, dict):
|
||||
flags = {}
|
||||
return cls(
|
||||
scale_to_zero=flags.get("deployments_scale_to_zero_supported") is True,
|
||||
dedicated=flags.get("deployments_dedicated_supported") is True,
|
||||
cli_builds=flags.get("deployments_cli_builds_supported") is True,
|
||||
)
|
||||
|
||||
|
||||
def _api_key_headers(api_key: str) -> dict[str, str]:
|
||||
return {"X-Api-Key": api_key, "Accept": "application/json"}
|
||||
|
||||
|
||||
class _JsonClient:
|
||||
def __init__(
|
||||
self,
|
||||
base_url: str,
|
||||
api_key: str,
|
||||
tenant_id: str | None = None,
|
||||
headers: dict[str, str],
|
||||
transport: httpx.BaseTransport | None,
|
||||
*,
|
||||
transport: httpx.BaseTransport | None = None,
|
||||
timeout: float = 30,
|
||||
retries: int = 3,
|
||||
):
|
||||
if not base_url:
|
||||
raise click.UsageError("Host backend URL is required")
|
||||
headers: dict[str, str] = {
|
||||
"X-Api-Key": api_key,
|
||||
"Accept": "application/json",
|
||||
}
|
||||
if tenant_id:
|
||||
headers["X-Tenant-ID"] = tenant_id
|
||||
self._endpoints = ControlPlaneEndpoints.from_control_plane_url(base_url)
|
||||
self._base_url = self._endpoints.control_plane_url
|
||||
self._client = httpx.Client(
|
||||
base_url=self._base_url,
|
||||
base_url=base_url,
|
||||
headers=headers,
|
||||
transport=transport or httpx.HTTPTransport(retries=3),
|
||||
timeout=30,
|
||||
transport=transport or httpx.HTTPTransport(retries=retries),
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
@property
|
||||
def base_url(self) -> str:
|
||||
return self._base_url
|
||||
|
||||
@property
|
||||
def endpoints(self) -> ControlPlaneEndpoints:
|
||||
return self._endpoints
|
||||
|
||||
def set_tenant(self, tenant_id: str) -> None:
|
||||
self._client.headers["X-Tenant-ID"] = tenant_id
|
||||
|
||||
def _request(
|
||||
self,
|
||||
method: str,
|
||||
@@ -193,6 +277,82 @@ class HostBackendClient:
|
||||
f"Failed to decode response from {path}: {err}"
|
||||
) from None
|
||||
|
||||
|
||||
class LangSmithApiClient(_JsonClient):
|
||||
def __init__(
|
||||
self,
|
||||
api_url: str,
|
||||
api_key: str,
|
||||
*,
|
||||
transport: httpx.BaseTransport | None = None,
|
||||
):
|
||||
super().__init__(
|
||||
api_url.rstrip("/"),
|
||||
_api_key_headers(api_key),
|
||||
transport,
|
||||
timeout=WORKSPACE_LOOKUP_TIMEOUT_SECONDS,
|
||||
retries=0,
|
||||
)
|
||||
|
||||
def list_workspaces(self) -> list[Workspace]:
|
||||
workspaces = self._request("GET", "/api/v1/workspaces")
|
||||
if not isinstance(workspaces, list):
|
||||
return []
|
||||
return [
|
||||
Workspace.from_api(workspace)
|
||||
for workspace in workspaces
|
||||
if isinstance(workspace, dict)
|
||||
and isinstance(workspace.get("id"), str)
|
||||
and not workspace.get("is_deleted")
|
||||
]
|
||||
|
||||
|
||||
class HostBackendClient(_JsonClient):
|
||||
"""Minimal JSON HTTP client for the host backend deployment service."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
control_plane: str | ControlPlaneEndpoints,
|
||||
api_key: str,
|
||||
tenant_id: str | None = None,
|
||||
*,
|
||||
router: WorkspaceRouter | None = None,
|
||||
transport: httpx.BaseTransport | None = None,
|
||||
):
|
||||
if not control_plane:
|
||||
raise click.UsageError("Host backend URL is required")
|
||||
headers = _api_key_headers(api_key)
|
||||
if tenant_id:
|
||||
headers["X-Tenant-ID"] = tenant_id
|
||||
self._endpoints = (
|
||||
control_plane
|
||||
if isinstance(control_plane, ControlPlaneEndpoints)
|
||||
else ControlPlaneEndpoints.from_control_plane_url(control_plane)
|
||||
)
|
||||
self._router = router or WorkspaceRouter((), None)
|
||||
super().__init__(self._endpoints.control_plane_url, headers, transport)
|
||||
|
||||
@property
|
||||
def base_url(self) -> str:
|
||||
return self._endpoints.control_plane_url
|
||||
|
||||
@property
|
||||
def endpoints(self) -> ControlPlaneEndpoints:
|
||||
return self._endpoints
|
||||
|
||||
def set_tenant(self, tenant_id: str) -> None:
|
||||
endpoints = self._router.endpoints_for(tenant_id)
|
||||
if endpoints is not None:
|
||||
self._endpoints = endpoints
|
||||
self._client.base_url = endpoints.control_plane_url
|
||||
self._client.headers["X-Tenant-ID"] = tenant_id
|
||||
|
||||
def instance_flags(self) -> InstanceFlags | None:
|
||||
"""Data plane capabilities, or None when the host backend is not on one."""
|
||||
if self._endpoints.info_url is None:
|
||||
return None
|
||||
return InstanceFlags.from_info(self._request("GET", self._endpoints.info_url))
|
||||
|
||||
def create_deployment(
|
||||
self,
|
||||
*,
|
||||
|
||||
@@ -4,6 +4,7 @@ import uuid
|
||||
from collections.abc import Callable, Iterator
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass, field
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
|
||||
import click.exceptions
|
||||
@@ -14,7 +15,11 @@ from click.testing import CliRunner, Result
|
||||
import langgraph_cli.archive as archive_module
|
||||
import langgraph_cli.deploy as deploy_module
|
||||
from langgraph_cli.cli import cli
|
||||
from langgraph_cli.host_backend import HostBackendClient
|
||||
from langgraph_cli.host_backend import (
|
||||
CONTROL_PLANE_PATH,
|
||||
HostBackendClient,
|
||||
LangSmithApiClient,
|
||||
)
|
||||
from langgraph_cli.image_reference import ImageReference
|
||||
|
||||
CONTROL_PLANE_URL = "https://control-plane.example.com"
|
||||
@@ -61,6 +66,17 @@ LISTENER_REQUIRED = (
|
||||
"Source configuration error: 'source_config.listener_id' is required "
|
||||
f"for workspace with available listener IDs: ['{LISTENER_ID}']"
|
||||
)
|
||||
DATA_PLANE_URL = "https://langsmith.customer.example"
|
||||
WORKSPACE_ID = "workspace-1"
|
||||
BYOC_WORKSPACE = {"id": WORKSPACE_ID, "data_plane_url": DATA_PLANE_URL}
|
||||
SAAS_WORKSPACE = {"id": "saas-workspace", "data_plane_url": None}
|
||||
SCALE_TO_ZERO = "deployments_scale_to_zero_supported"
|
||||
DEDICATED = "deployments_dedicated_supported"
|
||||
CLI_BUILDS = "deployments_cli_builds_supported"
|
||||
BYOC_FLAGS = {CLI_BUILDS: True, SCALE_TO_ZERO: True, DEDICATED: True}
|
||||
REVISION_ID = "rev-1"
|
||||
DISCOVER_WORKSPACES = "GET /api/v1/workspaces"
|
||||
DATA_PLANE_INFO = "GET /api/v1/info"
|
||||
LIST_DEPLOYMENTS = "GET /v2/deployments"
|
||||
LIST_LISTENERS = "GET /v2/listeners"
|
||||
CREATE_DEPLOYMENT = "POST /v2/deployments"
|
||||
@@ -98,19 +114,45 @@ class ControlPlaneDouble:
|
||||
push_token_extra: dict = field(default_factory=dict)
|
||||
upload_url_extra: dict = field(default_factory=dict)
|
||||
create_error: str | None = None
|
||||
create_status: int = 400
|
||||
listeners: list[dict] = field(default_factory=list)
|
||||
listeners_by_id: dict[str, dict] = field(default_factory=dict)
|
||||
workspaces: list[dict] = field(default_factory=list)
|
||||
workspaces_status: int = 200
|
||||
requires_workspace: bool = False
|
||||
instance_flags: dict[str, bool] = field(default_factory=dict)
|
||||
bodies: dict[str, dict] = field(default_factory=dict)
|
||||
requests: list[httpx.Request] = field(default_factory=list)
|
||||
|
||||
def handle(self, request: httpx.Request) -> httpx.Response:
|
||||
route = f"{request.method} {request.url.path}"
|
||||
route = f"{request.method} {self._path(request)}"
|
||||
self.timeline.append(route)
|
||||
self.requests.append(request)
|
||||
if request.content:
|
||||
self.bodies[route] = json.loads(request.content)
|
||||
return self._respond(request)
|
||||
|
||||
def host_backend_requests(self) -> list[httpx.Request]:
|
||||
return [r for r in self.requests if not r.url.path.startswith("/api/v1/")]
|
||||
|
||||
@staticmethod
|
||||
def _path(request: httpx.Request) -> str:
|
||||
return request.url.path.removeprefix(CONTROL_PLANE_PATH)
|
||||
|
||||
def _respond(self, request: httpx.Request) -> httpx.Response:
|
||||
method, path = request.method, request.url.path
|
||||
method, path = request.method, self._path(request)
|
||||
if (method, path) == ("GET", "/api/v1/workspaces"):
|
||||
return httpx.Response(self.workspaces_status, json=self.workspaces)
|
||||
if (method, path) == ("GET", "/api/v1/info"):
|
||||
return httpx.Response(200, json={"instance_flags": self.instance_flags})
|
||||
if self.requires_workspace and "X-Tenant-ID" not in request.headers:
|
||||
return httpx.Response(
|
||||
403, json={"detail": "This API key requires workspace specification"}
|
||||
)
|
||||
if path.endswith("/revisions"):
|
||||
return httpx.Response(200, json={"resources": [{"id": REVISION_ID}]})
|
||||
if path.endswith("/build_logs"):
|
||||
return httpx.Response(200, json={"logs": []})
|
||||
if (method, path) == ("GET", "/v2/listeners"):
|
||||
offset = int(request.url.params.get("offset", 0))
|
||||
limit = int(request.url.params.get("limit", 20))
|
||||
@@ -149,7 +191,9 @@ class ControlPlaneDouble:
|
||||
)
|
||||
if (method, path) == ("POST", "/v2/deployments"):
|
||||
if self.create_error is not None:
|
||||
return httpx.Response(400, json={"detail": self.create_error})
|
||||
return httpx.Response(
|
||||
self.create_status, json={"detail": self.create_error}
|
||||
)
|
||||
return httpx.Response(201, json={"id": CREATED_ID, "tenant_id": "tenant-1"})
|
||||
if path.endswith("/push-token"):
|
||||
if self.push_token_status != 200:
|
||||
@@ -183,15 +227,14 @@ class ControlPlaneDouble:
|
||||
)
|
||||
raise AssertionError(f"unexpected control plane call: {method} {path}")
|
||||
|
||||
def client_factory(self) -> Callable[..., HostBackendClient]:
|
||||
def install(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
transport = httpx.MockTransport(self.handle)
|
||||
|
||||
def make(
|
||||
host_url: str, api_key: str, tenant_id: str | None = None
|
||||
) -> HostBackendClient:
|
||||
return HostBackendClient(host_url, api_key, tenant_id, transport=transport)
|
||||
|
||||
return make
|
||||
for client_class in (HostBackendClient, LangSmithApiClient):
|
||||
monkeypatch.setattr(
|
||||
deploy_module,
|
||||
client_class.__name__,
|
||||
partial(client_class, transport=transport),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -288,15 +331,15 @@ class DeployProject:
|
||||
timeline: list[str]
|
||||
uploads: list[tuple[str, str, int, dict[str, str] | None]]
|
||||
|
||||
def run(self, *args: str, host_url: str = CONTROL_PLANE_URL) -> Result:
|
||||
def run(self, *args: str, host_url: str | None = CONTROL_PLANE_URL) -> Result:
|
||||
host_args = ("--host-url", host_url) if host_url else ()
|
||||
return CliRunner().invoke(
|
||||
cli,
|
||||
[
|
||||
"deploy",
|
||||
"--api-key",
|
||||
"test-key",
|
||||
"--host-url",
|
||||
host_url,
|
||||
*host_args,
|
||||
"--name",
|
||||
"my-app",
|
||||
"--no-input",
|
||||
@@ -341,9 +384,7 @@ def deploy_project(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> DeployPro
|
||||
|
||||
monkeypatch.setattr(deploy_module, "_no_input", False)
|
||||
monkeypatch.setattr(deploy_module, "_emitter", None)
|
||||
monkeypatch.setattr(
|
||||
deploy_module, "HostBackendClient", control_plane.client_factory()
|
||||
)
|
||||
control_plane.install(monkeypatch)
|
||||
monkeypatch.setattr(deploy_module, "build_docker_image", docker.build_docker_image)
|
||||
monkeypatch.setattr(deploy_module, "subp_exec", docker.subp_exec)
|
||||
monkeypatch.setattr(deploy_module, "Runner", _fake_runner)
|
||||
@@ -1578,3 +1619,414 @@ def test_a_listener_id_that_is_not_an_identifier_still_names_the_real_ones(
|
||||
assert "was not found" in result.output
|
||||
assert LISTENER_ID in result.output
|
||||
assert "uuid_parsing" not in result.output
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def byoc_project(deploy_project: DeployProject) -> DeployProject:
|
||||
deploy_project.control_plane.workspaces = [BYOC_WORKSPACE]
|
||||
deploy_project.control_plane.instance_flags = dict(BYOC_FLAGS)
|
||||
return deploy_project
|
||||
|
||||
|
||||
@pytest.mark.parametrize("build_mode", ["--no-remote", "--remote"])
|
||||
def test_byoc_deploy_runs_on_the_discovered_data_plane_and_workspace(
|
||||
byoc_project: DeployProject, build_mode: str
|
||||
) -> None:
|
||||
result = byoc_project.run(build_mode, host_url=None)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert byoc_project.timeline[:4] == [
|
||||
DISCOVER_WORKSPACES,
|
||||
LIST_DEPLOYMENTS,
|
||||
DATA_PLANE_INFO,
|
||||
CREATE_DEPLOYMENT,
|
||||
]
|
||||
host_backend_requests = byoc_project.control_plane.host_backend_requests()
|
||||
assert host_backend_requests
|
||||
assert all(
|
||||
str(r.url).startswith(f"{DATA_PLANE_URL}/api-host/v2/")
|
||||
and r.headers["X-Tenant-ID"] == WORKSPACE_ID
|
||||
for r in host_backend_requests
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"langsmith_endpoint",
|
||||
[
|
||||
pytest.param(None, id="default_endpoint"),
|
||||
pytest.param(DATA_PLANE_URL, id="endpoint_is_the_data_plane"),
|
||||
],
|
||||
)
|
||||
def test_byoc_status_link_points_at_langsmith_not_the_data_plane(
|
||||
byoc_project: DeployProject,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
langsmith_endpoint: str | None,
|
||||
) -> None:
|
||||
if langsmith_endpoint:
|
||||
monkeypatch.setenv("LANGSMITH_ENDPOINT", langsmith_endpoint)
|
||||
|
||||
result = byoc_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert (
|
||||
"View status: https://smith.langchain.com/o/tenant-1/host/deployments/dep-created"
|
||||
in result.output
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("instance_flags", "type_args", "source_config"),
|
||||
[
|
||||
pytest.param(
|
||||
BYOC_FLAGS,
|
||||
[],
|
||||
{"resource_spec": {}, "deployment_type": "dev_zero"},
|
||||
id="default_is_serverless_with_scale_to_zero",
|
||||
),
|
||||
pytest.param(
|
||||
{CLI_BUILDS: True},
|
||||
[],
|
||||
{"resource_spec": {}},
|
||||
id="default_is_dev_without_scale_to_zero",
|
||||
),
|
||||
pytest.param(
|
||||
BYOC_FLAGS,
|
||||
["--agent-server-type", "dev"],
|
||||
{"resource_spec": {}},
|
||||
id="dev",
|
||||
),
|
||||
pytest.param(
|
||||
BYOC_FLAGS,
|
||||
["--agent-server-type", "dedicated"],
|
||||
{"resource_spec": None, "deployment_type": "prod"},
|
||||
id="dedicated",
|
||||
),
|
||||
pytest.param(
|
||||
BYOC_FLAGS,
|
||||
["--agent-server-type", "prod"],
|
||||
{"resource_spec": None, "deployment_type": "prod"},
|
||||
id="legacy_prod_is_dedicated",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("build_mode", ["--no-remote", "--remote"])
|
||||
def test_byoc_create_sizes_the_agent_server_by_resource_spec(
|
||||
byoc_project: DeployProject,
|
||||
build_mode: str,
|
||||
instance_flags: dict[str, bool],
|
||||
type_args: list[str],
|
||||
source_config: dict,
|
||||
) -> None:
|
||||
byoc_project.control_plane.instance_flags = instance_flags
|
||||
|
||||
result = byoc_project.run(build_mode, *type_args, host_url=None)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert (
|
||||
byoc_project.control_plane.bodies[CREATE_DEPLOYMENT]["source_config"]
|
||||
== source_config
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("instance_flags", "type_args", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
{CLI_BUILDS: True},
|
||||
["--agent-server-type", "serverless"],
|
||||
"does not support serverless (scale to zero) Agent Servers",
|
||||
id="serverless_without_scale_to_zero",
|
||||
),
|
||||
pytest.param(
|
||||
{CLI_BUILDS: True},
|
||||
["--agent-server-type", "dedicated"],
|
||||
"does not support dedicated Agent Servers",
|
||||
id="dedicated_without_dedicated",
|
||||
),
|
||||
pytest.param(
|
||||
{CLI_BUILDS: True},
|
||||
["--agent-server-type", "prod"],
|
||||
"does not support dedicated Agent Servers",
|
||||
id="prod_without_dedicated",
|
||||
),
|
||||
pytest.param(
|
||||
{SCALE_TO_ZERO: True, DEDICATED: True},
|
||||
[],
|
||||
"--push-to <registry>/<repository>",
|
||||
id="no_cli_builds",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("build_mode", ["--no-remote", "--remote"])
|
||||
def test_byoc_refuses_unsupported_deploys_before_creating_anything(
|
||||
byoc_project: DeployProject,
|
||||
build_mode: str,
|
||||
instance_flags: dict[str, bool],
|
||||
type_args: list[str],
|
||||
expected: str,
|
||||
) -> None:
|
||||
byoc_project.control_plane.instance_flags = instance_flags
|
||||
|
||||
result = byoc_project.run(build_mode, *type_args, host_url=None)
|
||||
|
||||
assert result.exit_code == 2, result.output
|
||||
assert expected in result.output
|
||||
assert CREATE_DEPLOYMENT not in byoc_project.timeline
|
||||
|
||||
|
||||
@pytest.mark.parametrize("build_mode", ["--no-remote", "--remote"])
|
||||
def test_byoc_redeploys_an_existing_dedicated_agent_server_without_dedicated_support(
|
||||
byoc_project: DeployProject, build_mode: str
|
||||
) -> None:
|
||||
byoc_project.control_plane.instance_flags = {CLI_BUILDS: True}
|
||||
byoc_project.control_plane.existing_deployments = [
|
||||
{"id": "dep-existing", "name": "my-app"}
|
||||
]
|
||||
|
||||
result = byoc_project.run(
|
||||
build_mode, "--agent-server-type", "dedicated", host_url=None
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert CREATE_DEPLOYMENT not in byoc_project.timeline
|
||||
|
||||
|
||||
def test_byoc_without_cli_builds_names_both_external_image_flags(
|
||||
byoc_project: DeployProject,
|
||||
) -> None:
|
||||
byoc_project.control_plane.instance_flags = {}
|
||||
|
||||
result = byoc_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert "--push-to" in result.output
|
||||
assert "--image-uri" in result.output
|
||||
|
||||
|
||||
def test_byoc_without_cli_builds_still_deploys_an_external_image(
|
||||
byoc_project: DeployProject,
|
||||
) -> None:
|
||||
byoc_project.control_plane.instance_flags = {}
|
||||
|
||||
result = byoc_project.run("--image-uri", EXTERNAL_DIGEST, host_url=None)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert byoc_project.timeline == [
|
||||
DISCOVER_WORKSPACES,
|
||||
LIST_DEPLOYMENTS,
|
||||
CREATE_DEPLOYMENT,
|
||||
]
|
||||
assert byoc_project.control_plane.bodies[CREATE_DEPLOYMENT]["source"] == (
|
||||
"external_docker"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("workspaces", "workspaces_status"),
|
||||
[
|
||||
pytest.param([SAAS_WORKSPACE], 200, id="workspace_without_a_data_plane"),
|
||||
pytest.param([], 500, id="discovery_fails"),
|
||||
],
|
||||
)
|
||||
def test_saas_deploy_pays_one_discovery_call_and_is_otherwise_unchanged(
|
||||
deploy_project: DeployProject, workspaces: list[dict], workspaces_status: int
|
||||
) -> None:
|
||||
deploy_project.control_plane.workspaces = workspaces
|
||||
deploy_project.control_plane.workspaces_status = workspaces_status
|
||||
|
||||
result = deploy_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert deploy_project.timeline[:3] == [
|
||||
DISCOVER_WORKSPACES,
|
||||
LIST_DEPLOYMENTS,
|
||||
CREATE_DEPLOYMENT,
|
||||
]
|
||||
assert DATA_PLANE_INFO not in deploy_project.timeline
|
||||
assert all(
|
||||
str(r.url).startswith(f"{CLOUD_CONTROL_PLANE_URL}/v2/")
|
||||
and "X-Tenant-ID" not in r.headers
|
||||
for r in deploy_project.control_plane.host_backend_requests()
|
||||
)
|
||||
assert deploy_project.control_plane.bodies[CREATE_DEPLOYMENT]["source_config"] == {
|
||||
"deployment_type": "dev_zero"
|
||||
}
|
||||
|
||||
|
||||
def test_failed_discovery_says_it_falls_back_to_the_default_control_plane(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.workspaces_status = 500
|
||||
|
||||
result = deploy_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "using the default LangSmith control plane" in result.stderr
|
||||
|
||||
|
||||
def test_host_url_on_a_data_plane_gets_the_drop_host_url_hint(
|
||||
byoc_project: DeployProject,
|
||||
) -> None:
|
||||
detail = (
|
||||
"'source_config.resource_spec' is required for 'external_docker' source, "
|
||||
"and for every source on BYOC data planes, and must be 'null' for all "
|
||||
"other sources."
|
||||
)
|
||||
byoc_project.control_plane.create_error = detail
|
||||
|
||||
result = byoc_project.run(
|
||||
"--no-remote", host_url=f"{DATA_PLANE_URL}{CONTROL_PLANE_PATH}"
|
||||
)
|
||||
|
||||
assert result.exit_code == 1, result.output
|
||||
assert DATA_PLANE_INFO not in byoc_project.timeline
|
||||
assert detail in result.output
|
||||
assert "drop it so 'langgraph deploy' finds the data plane" in result.output
|
||||
|
||||
|
||||
def test_host_url_overrides_discovery(byoc_project: DeployProject) -> None:
|
||||
result = byoc_project.run("--no-remote")
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert DISCOVER_WORKSPACES not in byoc_project.timeline
|
||||
assert all(
|
||||
str(r.url).startswith(CONTROL_PLANE_URL)
|
||||
for r in byoc_project.control_plane.requests
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("build_mode", ["--no-remote", "--remote"])
|
||||
def test_an_older_data_plane_rejecting_the_source_gets_the_external_image_hint(
|
||||
deploy_project: DeployProject, build_mode: str
|
||||
) -> None:
|
||||
detail = "'source' must be one of ['github', 'external_docker']"
|
||||
deploy_project.control_plane.create_error = detail
|
||||
|
||||
result = deploy_project.run(build_mode)
|
||||
|
||||
assert result.exit_code == 1, result.output
|
||||
assert detail in result.output
|
||||
assert "--push-to <registry>/<repository>" in result.output
|
||||
assert "--image-uri" in result.output
|
||||
|
||||
|
||||
def test_byoc_build_logs_use_the_discovered_data_plane_and_workspace(
|
||||
byoc_project: DeployProject,
|
||||
) -> None:
|
||||
result = CliRunner().invoke(
|
||||
cli,
|
||||
[
|
||||
"deploy",
|
||||
"logs",
|
||||
"--api-key",
|
||||
"test-key",
|
||||
"--type",
|
||||
"build",
|
||||
"--agent-server-id",
|
||||
"dep-existing",
|
||||
],
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert [
|
||||
(str(r.url).split("?")[0], r.headers["X-Tenant-ID"])
|
||||
for r in byoc_project.control_plane.host_backend_requests()
|
||||
] == [
|
||||
(
|
||||
f"{DATA_PLANE_URL}/api-host/v2/deployments/dep-existing/revisions",
|
||||
WORKSPACE_ID,
|
||||
),
|
||||
(
|
||||
f"{DATA_PLANE_URL}/api-host/v1/projects/dep-existing/revisions/"
|
||||
f"{REVISION_ID}/build_logs",
|
||||
WORKSPACE_ID,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def test_mixed_data_planes_without_a_tenant_id_list_through_saas(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.workspaces = [BYOC_WORKSPACE, SAAS_WORKSPACE]
|
||||
|
||||
result = CliRunner().invoke(
|
||||
cli, ["deploy", "list", "--json", "--api-key", "test-key"]
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert deploy_project.timeline == [DISCOVER_WORKSPACES, LIST_DEPLOYMENTS]
|
||||
assert [
|
||||
str(r.url).split("?")[0]
|
||||
for r in deploy_project.control_plane.host_backend_requests()
|
||||
] == [f"{CLOUD_CONTROL_PLANE_URL}/v2/deployments"]
|
||||
|
||||
|
||||
def test_a_prompted_byoc_workspace_is_sent_to_its_data_plane(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.workspaces = [SAAS_WORKSPACE, BYOC_WORKSPACE]
|
||||
deploy_project.control_plane.requires_workspace = True
|
||||
|
||||
result = CliRunner().invoke(
|
||||
cli, ["deploy", "list", "--api-key", "test-key"], input=f"{WORKSPACE_ID}\n"
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert [
|
||||
(str(r.url).split("?")[0], r.headers.get("X-Tenant-ID"))
|
||||
for r in deploy_project.control_plane.host_backend_requests()
|
||||
] == [
|
||||
(f"{CLOUD_CONTROL_PLANE_URL}/v2/deployments", None),
|
||||
(f"{DATA_PLANE_URL}/api-host/v2/deployments", WORKSPACE_ID),
|
||||
]
|
||||
|
||||
|
||||
def test_an_empty_tenant_id_is_treated_as_unset(
|
||||
byoc_project: DeployProject, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("LANGSMITH_TENANT_ID", "")
|
||||
|
||||
result = byoc_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert all(
|
||||
r.headers["X-Tenant-ID"] == WORKSPACE_ID
|
||||
for r in byoc_project.control_plane.host_backend_requests()
|
||||
)
|
||||
|
||||
|
||||
def test_a_data_plane_without_https_never_gets_the_api_key(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.workspaces = [
|
||||
{"id": WORKSPACE_ID, "data_plane_url": "http://langsmith.customer.example"}
|
||||
]
|
||||
|
||||
result = deploy_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert result.exit_code == 1, result.output
|
||||
assert "must use https" in result.output
|
||||
assert deploy_project.timeline == [DISCOVER_WORKSPACES]
|
||||
|
||||
|
||||
def test_a_tenant_id_outside_the_keys_workspaces_fails_before_any_host_call(
|
||||
byoc_project: DeployProject, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("LANGSMITH_TENANT_ID", "unknown-ws")
|
||||
|
||||
result = byoc_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert result.exit_code == 1, result.output
|
||||
assert "unknown-ws" in result.output
|
||||
assert byoc_project.timeline == [DISCOVER_WORKSPACES]
|
||||
|
||||
|
||||
def test_a_data_plane_refusing_local_pushes_shows_its_hint(
|
||||
byoc_project: DeployProject,
|
||||
) -> None:
|
||||
hint = "Local image pushes are not supported on this data plane; use --remote."
|
||||
byoc_project.control_plane.create_status = 501
|
||||
byoc_project.control_plane.create_error = hint
|
||||
|
||||
result = byoc_project.run("--no-remote", host_url=None)
|
||||
|
||||
assert result.exit_code == 1, result.output
|
||||
assert hint in result.output
|
||||
@@ -1,10 +1,15 @@
|
||||
import json
|
||||
import os
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
import langgraph_cli.deploy as deploy_module
|
||||
from langgraph_cli.host_backend import LangSmithApiClient
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def deployment_metrics_response():
|
||||
@@ -21,3 +26,14 @@ def disable_analytics_env() -> None:
|
||||
|
||||
with patch.dict(os.environ, {"LANGGRAPH_CLI_NO_ANALYTICS": "1"}):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_discovered_workspaces(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Keep unit tests off the LangSmith API: discovery finds no workspaces."""
|
||||
transport = httpx.MockTransport(lambda request: httpx.Response(200, json=[]))
|
||||
monkeypatch.setattr(
|
||||
deploy_module,
|
||||
"LangSmithApiClient",
|
||||
partial(LangSmithApiClient, transport=transport),
|
||||
)
|
||||
@@ -20,6 +20,7 @@ from langgraph_cli.deploy import (
|
||||
CustomerRegistrySource,
|
||||
DockerBuildCommand,
|
||||
ExistingDeployment,
|
||||
HostTarget,
|
||||
Listener,
|
||||
ManagedRegistrySource,
|
||||
OnListener,
|
||||
@@ -29,9 +30,11 @@ from langgraph_cli.deploy import (
|
||||
Unplaced,
|
||||
_call_host_backend_with_optional_tenant,
|
||||
_create_host_backend_client,
|
||||
_discover_target,
|
||||
_docker_config_for_token,
|
||||
_Emitter,
|
||||
_env_without_deployment_name,
|
||||
_list_workspaces,
|
||||
_parse_env_from_config,
|
||||
_print_deployment_result,
|
||||
_resolve_env_path,
|
||||
@@ -43,7 +46,12 @@ from langgraph_cli.deploy import (
|
||||
normalize_image_tag,
|
||||
normalize_name,
|
||||
)
|
||||
from langgraph_cli.host_backend import HostBackendClient, HostBackendError
|
||||
from langgraph_cli.host_backend import (
|
||||
ControlPlaneEndpoints,
|
||||
HostBackendClient,
|
||||
HostBackendError,
|
||||
LangSmithApiClient,
|
||||
)
|
||||
from langgraph_cli.image_reference import ImageReference
|
||||
|
||||
|
||||
@@ -369,6 +377,24 @@ class TestCallHostBackendWithOptionalTenant:
|
||||
assert "smith.langchain.com" in exc_info.value.message
|
||||
assert seen_tenant_ids == [None, "workspace-123"]
|
||||
|
||||
def test_403_not_enabled_on_a_data_plane_links_langsmith(self):
|
||||
detail = (
|
||||
'{"detail":"LangSmith Deployment is not enabled for this organization"}'
|
||||
)
|
||||
client = HostBackendClient(
|
||||
ControlPlaneEndpoints.on_data_plane(
|
||||
"https://langsmith.customer.example", None
|
||||
),
|
||||
"test-key",
|
||||
transport=httpx.MockTransport(lambda req: httpx.Response(403, text=detail)),
|
||||
)
|
||||
with pytest.raises(HostBackendError, match="not enabled") as exc_info:
|
||||
_call_host_backend_with_optional_tenant(
|
||||
client, lambda c: c.list_deployments()
|
||||
)
|
||||
assert "https://smith.langchain.com/host/deployments" in exc_info.value.message
|
||||
assert "customer.example" not in exc_info.value.message
|
||||
|
||||
def test_other_403_re_raises_original(self):
|
||||
client = self._make_client(
|
||||
lambda req: httpx.Response(403, text='{"detail":"some other error"}')
|
||||
@@ -632,6 +658,120 @@ class TestCreateHostBackendClientEndpoint:
|
||||
assert client.base_url == "https://custom.host.com"
|
||||
|
||||
|
||||
DATA_PLANE = "https://langsmith.customer.example"
|
||||
OTHER_DATA_PLANE = "https://other.customer.example"
|
||||
WORKSPACE_UUID = "6f1c2a9e-0b7d-4c3e-9a51-2d8f4e6b7c10"
|
||||
|
||||
|
||||
def _workspaces(*workspaces: dict) -> LangSmithApiClient:
|
||||
return LangSmithApiClient(
|
||||
"https://api.smith.langchain.com",
|
||||
"key",
|
||||
transport=httpx.MockTransport(
|
||||
lambda req: httpx.Response(200, json=list(workspaces))
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class TestDiscoverTarget:
|
||||
@pytest.mark.parametrize(
|
||||
("workspaces", "tenant_id", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
[{"id": "ws-1", "data_plane_url": DATA_PLANE}],
|
||||
None,
|
||||
HostTarget("ws-1", DATA_PLANE),
|
||||
id="only_workspace",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{"id": "ws-1", "data_plane_url": DATA_PLANE},
|
||||
{"id": "ws-2", "data_plane_url": OTHER_DATA_PLANE},
|
||||
],
|
||||
"ws-2",
|
||||
HostTarget("ws-2", OTHER_DATA_PLANE),
|
||||
id="tenant_id_picks_its_workspace",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{"id": "ws-1", "data_plane_url": DATA_PLANE},
|
||||
{"id": WORKSPACE_UUID, "data_plane_url": OTHER_DATA_PLANE},
|
||||
],
|
||||
WORKSPACE_UUID.upper(),
|
||||
HostTarget(WORKSPACE_UUID, OTHER_DATA_PLANE),
|
||||
id="tenant_id_matches_the_workspace_uuid_in_any_case",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{"id": "ws-1", "data_plane_url": DATA_PLANE},
|
||||
{
|
||||
"id": "ws-2",
|
||||
"data_plane_url": OTHER_DATA_PLANE,
|
||||
"is_deleted": True,
|
||||
},
|
||||
],
|
||||
None,
|
||||
HostTarget("ws-1", DATA_PLANE),
|
||||
id="only_non_deleted_workspace",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{"id": "ws-1", "data_plane_url": DATA_PLANE},
|
||||
{"id": "ws-2", "data_plane_url": DATA_PLANE},
|
||||
],
|
||||
None,
|
||||
HostTarget(None, DATA_PLANE),
|
||||
id="shared_data_plane_leaves_the_workspace_to_the_backend",
|
||||
),
|
||||
pytest.param(
|
||||
[{"id": "ws-1", "data_plane_url": None}],
|
||||
None,
|
||||
HostTarget(None),
|
||||
id="saas_workspace_keeps_todays_headers",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{"id": "ws-1", "data_plane_url": DATA_PLANE},
|
||||
{"id": "ws-2", "data_plane_url": None},
|
||||
],
|
||||
None,
|
||||
HostTarget(None),
|
||||
id="mixed_data_planes_leave_the_workspace_to_the_backend",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{"id": "ws-1", "data_plane_url": DATA_PLANE},
|
||||
{"id": "ws-2", "data_plane_url": OTHER_DATA_PLANE},
|
||||
],
|
||||
None,
|
||||
HostTarget(None),
|
||||
id="different_data_planes_leave_the_workspace_to_the_backend",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_picks_the_workspace(self, workspaces, tenant_id, expected):
|
||||
target = _discover_target(_workspaces(*workspaces).list_workspaces(), tenant_id)
|
||||
|
||||
assert target == expected
|
||||
|
||||
def test_failed_discovery_keeps_todays_behavior(self):
|
||||
langsmith = LangSmithApiClient(
|
||||
"https://api.smith.langchain.com",
|
||||
"key",
|
||||
transport=httpx.MockTransport(lambda req: httpx.Response(401)),
|
||||
)
|
||||
|
||||
assert _discover_target(_list_workspaces(langsmith), "ws-1") == HostTarget(
|
||||
"ws-1"
|
||||
)
|
||||
|
||||
def test_a_tenant_id_outside_the_keys_workspaces_is_named(self):
|
||||
langsmith = _workspaces({"id": "ws-1", "data_plane_url": DATA_PLANE})
|
||||
|
||||
with pytest.raises(click.ClickException, match="workspace unknown-ws"):
|
||||
_discover_target(langsmith.list_workspaces(), "unknown-ws")
|
||||
|
||||
|
||||
class TestDockerBuildCommand:
|
||||
@pytest.mark.parametrize(
|
||||
("machine", "verbose", "expected"),
|
||||
@@ -1497,6 +1637,11 @@ DEFAULT_UPLOAD_HEADERS = {
|
||||
},
|
||||
id="returned_headers_keep_a_content_type",
|
||||
),
|
||||
pytest.param(
|
||||
{"Content-Type": "application/gzip"},
|
||||
{"content-type": "application/gzip", "content-length": "7"},
|
||||
id="s3_signs_only_the_content_type",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_source_upload_sends_the_headers_the_signed_url_requires(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
|
||||
import click
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
@@ -7,6 +8,11 @@ from langgraph_cli.host_backend import (
|
||||
ControlPlaneEndpoints,
|
||||
HostBackendClient,
|
||||
HostBackendError,
|
||||
InstanceFlags,
|
||||
LangSmithApiClient,
|
||||
Workspace,
|
||||
WorkspaceRouter,
|
||||
langsmith_api_url,
|
||||
)
|
||||
|
||||
|
||||
@@ -737,3 +743,251 @@ def test_error_detail_is_readable(body, expected):
|
||||
assert error.value.detail == expected
|
||||
if expected is not None:
|
||||
assert error.value.message.endswith(expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("langsmith_endpoint", "expected"),
|
||||
[
|
||||
pytest.param(None, "https://api.smith.langchain.com", id="cloud_default"),
|
||||
pytest.param(
|
||||
"https://eu.api.smith.langchain.com/",
|
||||
"https://eu.api.smith.langchain.com",
|
||||
id="regional_cloud",
|
||||
),
|
||||
pytest.param(
|
||||
"https://smith.example.com/api/v1",
|
||||
"https://smith.example.com",
|
||||
id="self_hosted_root",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_langsmith_api_url(langsmith_endpoint, expected):
|
||||
assert langsmith_api_url(langsmith_endpoint) == expected
|
||||
|
||||
|
||||
def test_list_workspaces_reads_the_api_keys_live_workspaces():
|
||||
seen: dict = {}
|
||||
|
||||
def handler(req: httpx.Request) -> httpx.Response:
|
||||
seen["url"] = str(req.url)
|
||||
seen["api_key"] = req.headers["x-api-key"]
|
||||
return httpx.Response(
|
||||
200,
|
||||
json=[
|
||||
{"id": "ws-1", "data_plane_url": "https://dp.example.com/"},
|
||||
{"id": "ws-2", "data_plane_url": None},
|
||||
{"id": "ws-3", "data_plane_url": "", "is_deleted": False},
|
||||
{
|
||||
"id": "ws-4",
|
||||
"data_plane_url": "https://old.example.com",
|
||||
"is_deleted": True,
|
||||
},
|
||||
{"data_plane_url": "https://no-id.example.com"},
|
||||
],
|
||||
)
|
||||
|
||||
client = LangSmithApiClient(
|
||||
"https://api.smith.langchain.com/",
|
||||
"key",
|
||||
transport=httpx.MockTransport(handler),
|
||||
)
|
||||
|
||||
assert client.list_workspaces() == [
|
||||
Workspace("ws-1", "https://dp.example.com"),
|
||||
Workspace("ws-2", None),
|
||||
Workspace("ws-3", None),
|
||||
]
|
||||
assert seen == {
|
||||
"url": "https://api.smith.langchain.com/api/v1/workspaces",
|
||||
"api_key": "key",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("langsmith_endpoint", "dashboard_url"),
|
||||
[
|
||||
pytest.param(None, "https://smith.langchain.com", id="cloud"),
|
||||
pytest.param(
|
||||
"https://eu.api.smith.langchain.com",
|
||||
"https://eu.smith.langchain.com",
|
||||
id="regional_cloud",
|
||||
),
|
||||
pytest.param(
|
||||
"https://smith.example.com/api/v1",
|
||||
"https://smith.example.com",
|
||||
id="self_hosted",
|
||||
),
|
||||
pytest.param(
|
||||
"https://langsmith.customer.example/api/v1",
|
||||
"https://smith.langchain.com",
|
||||
id="endpoint_is_the_data_plane",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_data_plane_endpoints_keep_the_data_plane_host_and_link_to_langsmith(
|
||||
langsmith_endpoint, dashboard_url
|
||||
):
|
||||
endpoints = ControlPlaneEndpoints.on_data_plane(
|
||||
"https://langsmith.customer.example/", langsmith_endpoint
|
||||
)
|
||||
|
||||
assert endpoints == ControlPlaneEndpoints(
|
||||
"https://langsmith.customer.example/api-host",
|
||||
dashboard_url,
|
||||
"https://langsmith.customer.example/api/v1/info",
|
||||
)
|
||||
assert not endpoints.is_cloud
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data_plane_url",
|
||||
[
|
||||
pytest.param("http://langsmith.customer.example", id="http"),
|
||||
pytest.param("ftp://localhost", id="other_scheme"),
|
||||
],
|
||||
)
|
||||
def test_data_plane_without_https_is_refused(data_plane_url):
|
||||
with pytest.raises(click.ClickException, match="must use https"):
|
||||
ControlPlaneEndpoints.on_data_plane(data_plane_url, None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data_plane_url", ["http://localhost:1980", "http://127.0.0.1:1980"]
|
||||
)
|
||||
def test_local_data_plane_may_use_http(data_plane_url):
|
||||
endpoints = ControlPlaneEndpoints.on_data_plane(data_plane_url, None)
|
||||
|
||||
assert endpoints.control_plane_url == f"{data_plane_url}/api-host"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("host_url", "langsmith_endpoint"),
|
||||
[
|
||||
pytest.param(
|
||||
"https://langsmith.customer.example/api-host", None, id="api_host_url"
|
||||
),
|
||||
pytest.param(None, "https://smith.example.com/api/v1", id="self_hosted"),
|
||||
pytest.param("https://api.host.langchain.com", None, id="cloud"),
|
||||
],
|
||||
)
|
||||
def test_only_a_discovered_data_plane_reads_instance_flags(
|
||||
host_url, langsmith_endpoint
|
||||
):
|
||||
assert ControlPlaneEndpoints.resolve(host_url, langsmith_endpoint).info_url is None
|
||||
assert (
|
||||
ControlPlaneEndpoints.on_data_plane(
|
||||
"https://langsmith.customer.example", None
|
||||
).info_url
|
||||
== "https://langsmith.customer.example/api/v1/info"
|
||||
)
|
||||
|
||||
|
||||
def test_workspace_ids_match_as_uuids():
|
||||
workspace = Workspace("6f1c2a9e-0b7d-4c3e-9a51-2d8f4e6b7c10", None)
|
||||
|
||||
assert workspace.has_id("6F1C2A9E-0B7D-4C3E-9A51-2D8F4E6B7C10")
|
||||
assert not workspace.has_id("ws-1")
|
||||
|
||||
|
||||
def test_setting_a_routed_workspace_moves_the_client_to_its_data_plane():
|
||||
seen: list[tuple[str, str | None]] = []
|
||||
|
||||
def handler(req: httpx.Request) -> httpx.Response:
|
||||
seen.append((str(req.url).split("?")[0], req.headers.get("X-Tenant-ID")))
|
||||
return httpx.Response(200, json={"resources": []})
|
||||
|
||||
router = WorkspaceRouter(
|
||||
[
|
||||
Workspace("saas-ws", None),
|
||||
Workspace("byoc-ws", "https://langsmith.customer.example"),
|
||||
],
|
||||
None,
|
||||
)
|
||||
client = HostBackendClient(
|
||||
ControlPlaneEndpoints.resolve(None, None),
|
||||
"key",
|
||||
router=router,
|
||||
transport=httpx.MockTransport(handler),
|
||||
)
|
||||
|
||||
client.set_tenant("byoc-ws")
|
||||
client.list_deployments()
|
||||
client.set_tenant("unknown-ws")
|
||||
client.list_deployments()
|
||||
|
||||
assert seen == [
|
||||
("https://langsmith.customer.example/api-host/v2/deployments", "byoc-ws"),
|
||||
("https://langsmith.customer.example/api-host/v2/deployments", "unknown-ws"),
|
||||
]
|
||||
assert client.endpoints.dashboard_url == "https://smith.langchain.com"
|
||||
|
||||
|
||||
def test_data_plane_client_targets_the_data_plane_host_backend():
|
||||
seen: list[str] = []
|
||||
|
||||
def handler(req: httpx.Request) -> httpx.Response:
|
||||
seen.append(str(req.url))
|
||||
return httpx.Response(200, json={"resources": []})
|
||||
|
||||
client = HostBackendClient(
|
||||
ControlPlaneEndpoints.on_data_plane(
|
||||
"https://byoc.langchain.com", "https://eu.api.smith.langchain.com"
|
||||
),
|
||||
"key",
|
||||
"ws-1",
|
||||
transport=httpx.MockTransport(handler),
|
||||
)
|
||||
client.list_deployments()
|
||||
|
||||
assert seen[0].startswith("https://byoc.langchain.com/api-host/v2/deployments?")
|
||||
assert client.endpoints.dashboard_url == "https://eu.smith.langchain.com"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("info", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
{
|
||||
"instance_flags": {
|
||||
"deployments_scale_to_zero_supported": True,
|
||||
"deployments_dedicated_supported": True,
|
||||
"deployments_cli_builds_supported": True,
|
||||
}
|
||||
},
|
||||
InstanceFlags(scale_to_zero=True, dedicated=True, cli_builds=True),
|
||||
id="all_supported",
|
||||
),
|
||||
pytest.param(
|
||||
{"instance_flags": {"deployments_dedicated_supported": True}},
|
||||
InstanceFlags(scale_to_zero=False, dedicated=True, cli_builds=False),
|
||||
id="missing_flags_are_false",
|
||||
),
|
||||
pytest.param(
|
||||
{},
|
||||
InstanceFlags(scale_to_zero=False, dedicated=False, cli_builds=False),
|
||||
id="older_data_plane_without_instance_flags",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_data_plane_client_reads_instance_flags_from_info(info, expected):
|
||||
seen: list[str] = []
|
||||
|
||||
def handler(req: httpx.Request) -> httpx.Response:
|
||||
seen.append(str(req.url))
|
||||
return httpx.Response(200, json=info)
|
||||
|
||||
client = HostBackendClient(
|
||||
ControlPlaneEndpoints.on_data_plane("https://dp.example.com/", None),
|
||||
"key",
|
||||
transport=httpx.MockTransport(handler),
|
||||
)
|
||||
|
||||
assert client.instance_flags() == expected
|
||||
assert seen == ["https://dp.example.com/api/v1/info"]
|
||||
|
||||
|
||||
def test_host_backend_off_a_data_plane_has_no_instance_flags():
|
||||
assert (
|
||||
HostBackendClient("https://api.host.langchain.com", "key").instance_flags()
|
||||
is None
|
||||
)
|
||||
@@ -194,14 +194,8 @@ def add_messages(
|
||||
if not isinstance(right, list):
|
||||
right = [right] # type: ignore[assignment]
|
||||
# coerce to message
|
||||
left = [
|
||||
message_chunk_to_message(cast(BaseMessageChunk, m))
|
||||
for m in convert_to_messages(left)
|
||||
]
|
||||
right = [
|
||||
message_chunk_to_message(cast(BaseMessageChunk, m))
|
||||
for m in convert_to_messages(right)
|
||||
]
|
||||
left = _coerce_to_messages(left)
|
||||
right = _coerce_to_messages(right)
|
||||
# assign missing ids
|
||||
for m in left:
|
||||
if m.id is None:
|
||||
@@ -234,7 +228,8 @@ def add_messages(
|
||||
|
||||
merged_by_id[m.id] = len(merged)
|
||||
merged.append(m)
|
||||
merged = [m for m in merged if m.id not in ids_to_remove]
|
||||
if ids_to_remove:
|
||||
merged = [m for m in merged if m.id not in ids_to_remove]
|
||||
|
||||
if format == "langchain-openai":
|
||||
merged = _format_messages(merged)
|
||||
@@ -247,6 +242,20 @@ def add_messages(
|
||||
return merged
|
||||
|
||||
|
||||
def _coerce_to_messages(messages: list) -> list[Any]:
|
||||
# Both conversions return a plain message unchanged, and a long history is
|
||||
# all plain messages, so check the few distinct types instead of each item.
|
||||
if all(
|
||||
issubclass(t, BaseMessage) and not issubclass(t, BaseMessageChunk)
|
||||
for t in set(map(type, messages))
|
||||
):
|
||||
return messages
|
||||
return [
|
||||
message_chunk_to_message(cast(BaseMessageChunk, m))
|
||||
for m in convert_to_messages(messages)
|
||||
]
|
||||
|
||||
|
||||
@deprecated(
|
||||
"MessageGraph is deprecated in langgraph 1.0.0, to be removed in 2.0.0. Please use StateGraph with a `messages` key instead.",
|
||||
category=None,
|
||||
|
||||
@@ -1452,6 +1452,8 @@ class CompiledStateGraph(
|
||||
output_keys = list(self.builder.channels) + [
|
||||
k for k, v in self.builder.managed.items()
|
||||
]
|
||||
# for membership checks, which run on every key of every node update
|
||||
output_key_set = frozenset(output_keys)
|
||||
|
||||
def _get_updates(
|
||||
input: None | dict | Any,
|
||||
@@ -1459,12 +1461,12 @@ class CompiledStateGraph(
|
||||
if input is None:
|
||||
return None
|
||||
elif isinstance(input, dict):
|
||||
return [(k, v) for k, v in input.items() if k in output_keys]
|
||||
return [(k, v) for k, v in input.items() if k in output_key_set]
|
||||
elif isinstance(input, Command):
|
||||
if input.graph == Command.PARENT:
|
||||
return None
|
||||
return [
|
||||
(k, v) for k, v in input._update_as_tuples() if k in output_keys
|
||||
(k, v) for k, v in input._update_as_tuples() if k in output_key_set
|
||||
]
|
||||
elif (
|
||||
isinstance(input, (list, tuple))
|
||||
@@ -1477,7 +1479,9 @@ class CompiledStateGraph(
|
||||
if i.graph == Command.PARENT:
|
||||
continue
|
||||
updates.extend(
|
||||
(k, v) for k, v in i._update_as_tuples() if k in output_keys
|
||||
(k, v)
|
||||
for k, v in i._update_as_tuples()
|
||||
if k in output_key_set
|
||||
)
|
||||
else:
|
||||
updates.extend(_get_updates(i) or ())
|
||||
|
||||
@@ -130,35 +130,35 @@ def map_output_updates(
|
||||
]
|
||||
if not output_tasks:
|
||||
return
|
||||
# a set: a wide state would otherwise scan every key for each write
|
||||
output_keys = (
|
||||
frozenset(output_channels) if not isinstance(output_channels, str) else None
|
||||
)
|
||||
updated: list[tuple[str, Any]] = []
|
||||
for task, writes in output_tasks:
|
||||
rtn = next((value for chan, value in writes if chan == RETURN), MISSING)
|
||||
if rtn is not MISSING:
|
||||
updated.append((task.name, rtn))
|
||||
elif isinstance(output_channels, str):
|
||||
elif output_keys is None:
|
||||
updated.extend(
|
||||
(task.name, value) for chan, value in writes if chan == output_channels
|
||||
)
|
||||
elif any(chan in output_channels for chan, _ in writes):
|
||||
elif any(chan in output_keys for chan, _ in writes):
|
||||
counts = Counter(chan for chan, _ in writes)
|
||||
if any(counts[chan] > 1 for chan in output_channels):
|
||||
if any(n > 1 for chan, n in counts.items() if chan in output_keys):
|
||||
updated.extend(
|
||||
(
|
||||
task.name,
|
||||
{chan: value},
|
||||
)
|
||||
for chan, value in writes
|
||||
if chan in output_channels
|
||||
if chan in output_keys
|
||||
)
|
||||
else:
|
||||
updated.append(
|
||||
(
|
||||
task.name,
|
||||
{
|
||||
chan: value
|
||||
for chan, value in writes
|
||||
if chan in output_channels
|
||||
},
|
||||
{chan: value for chan, value in writes if chan in output_keys},
|
||||
)
|
||||
)
|
||||
grouped: dict[str, Any] = {t.name: [] for t, _ in output_tasks}
|
||||
|
||||
@@ -358,6 +358,14 @@ class PregelLoop:
|
||||
self.retry_policy = retry_policy
|
||||
self.cache_policy = cache_policy
|
||||
self.durability = durability
|
||||
# Channel types are fixed for the run, so per-step code reads these
|
||||
# instead of scanning every channel.
|
||||
self._has_untracked_channels = any(
|
||||
isinstance(spec, UntrackedValue) for spec in specs.values()
|
||||
)
|
||||
self._delta_channel_names = [
|
||||
k for k, spec in specs.items() if isinstance(spec, DeltaChannel)
|
||||
]
|
||||
self._has_graph_lifecycle_callbacks = has_graph_lifecycle_callbacks
|
||||
self._graph_lifecycle_events = deque()
|
||||
if self.stream is not None and CONFIG_KEY_STREAM in config[CONF]:
|
||||
@@ -496,9 +504,7 @@ class PregelLoop:
|
||||
writes_to_save = writes
|
||||
|
||||
# check if any writes are to an UntrackedValue channel
|
||||
if any(
|
||||
isinstance(channel, UntrackedValue) for channel in self.channels.values()
|
||||
):
|
||||
if self._has_untracked_channels:
|
||||
# we do not persist untracked values in checkpoints
|
||||
writes_to_save = [
|
||||
# sanitize UntrackedValues that are nested within Send packets
|
||||
@@ -840,10 +846,12 @@ class PregelLoop:
|
||||
Unfinished (failed or interrupted) tasks keep empty writes, so the
|
||||
runner re-executes them or routes them to error handlers.
|
||||
"""
|
||||
for tid, status in read_task_statuses(self.checkpoint_pending_writes).items():
|
||||
if task := tasks.get(tid):
|
||||
task.writes.extend(status.output)
|
||||
self._reapplied_task_ids.add(tid)
|
||||
# Filter first: this runs once per task call, and reading the status of
|
||||
# every task that wrote would make runs with many task calls quadratic.
|
||||
writes = (w for w in self.checkpoint_pending_writes if w[0] in tasks)
|
||||
for tid, status in read_task_statuses(writes).items():
|
||||
tasks[tid].writes.extend(status.output)
|
||||
self._reapplied_task_ids.add(tid)
|
||||
|
||||
def _resume_error_handlers_if_applicable(self) -> None:
|
||||
"""On resume, schedule error handlers for tasks that failed in a prior run.
|
||||
@@ -1300,9 +1308,7 @@ class PregelLoop:
|
||||
)
|
||||
new_counters: dict[str, tuple[int, int]] = {}
|
||||
updated = self.updated_channels or set()
|
||||
for ch_name, ch in self.channels.items():
|
||||
if not isinstance(ch, DeltaChannel):
|
||||
continue
|
||||
for ch_name in self._delta_channel_names:
|
||||
u, s = prev_counters.get(ch_name, (0, 0))
|
||||
s += 1
|
||||
if ch_name in updated:
|
||||
@@ -1322,7 +1328,9 @@ class PregelLoop:
|
||||
# create new checkpoint
|
||||
channels_to_snapshot = (
|
||||
delta_channels_to_snapshot(
|
||||
self.channels, new_counters, self.checkpoint["channel_versions"]
|
||||
{k: self.channels[k] for k in self._delta_channel_names},
|
||||
new_counters,
|
||||
self.checkpoint["channel_versions"],
|
||||
)
|
||||
| self._delta_channels_forced_snapshot
|
||||
if do_checkpoint
|
||||
@@ -1351,9 +1359,7 @@ class PregelLoop:
|
||||
elif "counters_since_delta_snapshot" in self.checkpoint_metadata:
|
||||
del self.checkpoint_metadata["counters_since_delta_snapshot"]
|
||||
# sanitize TASK channel in the checkpoint before saving (durability=="exit")
|
||||
if TASKS in self.checkpoint["channel_values"] and any(
|
||||
isinstance(channel, UntrackedValue) for channel in self.channels.values()
|
||||
):
|
||||
if TASKS in self.checkpoint["channel_values"] and self._has_untracked_channels:
|
||||
sanitized_tasks = [
|
||||
sanitize_untracked_values_in_send(value, self.channels)
|
||||
if isinstance(value, Send)
|
||||
|
||||
@@ -78,6 +78,7 @@ class FuturesDict(Generic[F, E], dict[F, PregelExecutableTask | None]):
|
||||
should_stop: Callable[[set[F]], bool]
|
||||
counter: int
|
||||
done: set[F]
|
||||
failed: set[F]
|
||||
lock: threading.Lock
|
||||
|
||||
def __init__(
|
||||
@@ -97,6 +98,7 @@ class FuturesDict(Generic[F, E], dict[F, PregelExecutableTask | None]):
|
||||
self.should_stop = should_stop
|
||||
self.counter = 0
|
||||
self.done: set[F] = set()
|
||||
self.failed: set[F] = set()
|
||||
|
||||
def __setitem__(
|
||||
self,
|
||||
@@ -123,9 +125,13 @@ class FuturesDict(Generic[F, E], dict[F, PregelExecutableTask | None]):
|
||||
with self.lock:
|
||||
self.done.add(fut)
|
||||
self.counter -= 1
|
||||
# Only a future that raised can meet the stop condition, so check
|
||||
# those instead of every done future (quadratic in wide steps).
|
||||
if not fut.cancelled() and fut.exception() is not None:
|
||||
self.failed.add(fut)
|
||||
# Wake waiter when all tracked futures are done, or when runner-level
|
||||
# stop condition is met (for example, a non-handled fatal exception).
|
||||
if self.counter == 0 or self.should_stop(self.done):
|
||||
if self.counter == 0 or self.should_stop(self.failed):
|
||||
self.event.set()
|
||||
|
||||
|
||||
@@ -234,6 +240,7 @@ class PregelRunner:
|
||||
fut: concurrent.futures.Future = concurrent.futures.Future()
|
||||
fut.set_exception(exc)
|
||||
futures.done.add(fut)
|
||||
futures.failed.add(fut)
|
||||
elif reraise and id(exc) not in self._handled_exception_ids:
|
||||
# let the caller stream this task's result before it raises
|
||||
yield
|
||||
@@ -427,6 +434,7 @@ class PregelRunner:
|
||||
fut: asyncio.Future = loop.create_future()
|
||||
fut.set_exception(exc)
|
||||
futures.done.add(fut)
|
||||
futures.failed.add(fut)
|
||||
elif reraise and id(exc) not in self._handled_exception_ids:
|
||||
# let the caller stream this task's result before it raises
|
||||
yield
|
||||
|
||||
@@ -2902,16 +2902,17 @@ class Pregel(
|
||||
if CONFIG_KEY_TASK_ID not in config[CONF]:
|
||||
# Not run as a task (a child invoked with its own thread, say),
|
||||
# this graph's namespace starts at the root, so a caller's
|
||||
# handler would take this graph's events for its own graph's.
|
||||
# handler without `subgraphs` would take this graph's events
|
||||
# for its own graph's. One with `subgraphs` asked for them.
|
||||
callback_manager.handlers = [
|
||||
h
|
||||
for h in callback_manager.handlers
|
||||
if not _is_stream_mode_handler(h)
|
||||
if not _streams_only_its_graph(h)
|
||||
]
|
||||
callback_manager.inheritable_handlers = [
|
||||
h
|
||||
for h in callback_manager.inheritable_handlers
|
||||
if not _is_stream_mode_handler(h)
|
||||
if not _streams_only_its_graph(h)
|
||||
]
|
||||
if "ls_integration" not in callback_manager.metadata:
|
||||
callback_manager.add_metadata({"ls_integration": "langgraph"})
|
||||
@@ -3319,16 +3320,17 @@ class Pregel(
|
||||
if CONFIG_KEY_TASK_ID not in config[CONF]:
|
||||
# Not run as a task (a child invoked with its own thread, say),
|
||||
# this graph's namespace starts at the root, so a caller's
|
||||
# handler would take this graph's events for its own graph's.
|
||||
# handler without `subgraphs` would take this graph's events
|
||||
# for its own graph's. One with `subgraphs` asked for them.
|
||||
callback_manager.handlers = [
|
||||
h
|
||||
for h in callback_manager.handlers
|
||||
if not _is_stream_mode_handler(h)
|
||||
if not _streams_only_its_graph(h)
|
||||
]
|
||||
callback_manager.inheritable_handlers = [
|
||||
h
|
||||
for h in callback_manager.inheritable_handlers
|
||||
if not _is_stream_mode_handler(h)
|
||||
if not _streams_only_its_graph(h)
|
||||
]
|
||||
if "ls_integration" not in callback_manager.metadata:
|
||||
callback_manager.add_metadata({"ls_integration": "langgraph"})
|
||||
@@ -4480,8 +4482,11 @@ def _coerce_checkpoint_values(payload: Any, mapper: Callable[[Any], Any]) -> Non
|
||||
payload["values"] = mapper(payload["values"])
|
||||
|
||||
|
||||
def _is_stream_mode_handler(handler: object) -> bool:
|
||||
return isinstance(handler, (StreamMessagesHandler, StreamToolCallHandler))
|
||||
def _streams_only_its_graph(handler: object) -> bool:
|
||||
return (
|
||||
isinstance(handler, (StreamMessagesHandler, StreamToolCallHandler))
|
||||
and not handler.subgraphs
|
||||
)
|
||||
|
||||
|
||||
def _resolve_parent_ns(
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph"
|
||||
version = "1.2.20"
|
||||
version = "1.2.22"
|
||||
description = "Building stateful, multi-actor applications with LLMs"
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
|
||||
@@ -5,6 +5,7 @@ import langchain_core
|
||||
import pytest
|
||||
from langchain_core.messages import (
|
||||
AIMessage,
|
||||
AIMessageChunk,
|
||||
AnyMessage,
|
||||
HumanMessage,
|
||||
RemoveMessage,
|
||||
@@ -168,6 +169,41 @@ def test_non_list_inputs():
|
||||
assert result == expected_result
|
||||
|
||||
|
||||
def test_message_likes_and_chunks_are_converted_on_both_sides():
|
||||
left = [
|
||||
HumanMessage(content="Hello", id="1"),
|
||||
{"role": "assistant", "content": "Hi", "id": "2"},
|
||||
]
|
||||
right = [AIMessageChunk(content="chunk", id="3"), ("user", "Thanks")]
|
||||
result = add_messages(left, right)
|
||||
assert [type(m) for m in result] == [
|
||||
HumanMessage,
|
||||
AIMessage,
|
||||
AIMessage,
|
||||
HumanMessage,
|
||||
]
|
||||
assert result[:3] == [
|
||||
HumanMessage(content="Hello", id="1"),
|
||||
AIMessage(content="Hi", id="2"),
|
||||
AIMessage(content="chunk", id="3"),
|
||||
]
|
||||
assert result[3].content == "Thanks"
|
||||
|
||||
|
||||
def test_chunk_only_lists_are_converted():
|
||||
left = [AIMessageChunk(content="a", id="1")]
|
||||
right = [AIMessageChunk(content="b", id="2")]
|
||||
result = add_messages(left, right)
|
||||
assert [type(m) for m in result] == [AIMessage, AIMessage]
|
||||
|
||||
|
||||
def test_result_is_a_new_list():
|
||||
left = [HumanMessage(content="Hello", id="1")]
|
||||
result = add_messages(left, [])
|
||||
assert result == left
|
||||
assert result is not left
|
||||
|
||||
|
||||
def test_delete_all():
|
||||
left = [
|
||||
HumanMessage(content="Hello", id="1"),
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import concurrent.futures
|
||||
import threading
|
||||
import weakref
|
||||
from functools import partial
|
||||
|
||||
from langgraph.pregel._runner import FuturesDict, _should_stop_others
|
||||
|
||||
|
||||
def _on_done(task: object, exc: BaseException | None) -> None:
|
||||
pass
|
||||
|
||||
|
||||
def _futures(handled_exception_ids: set[int]) -> FuturesDict:
|
||||
return FuturesDict(
|
||||
event=threading.Event(),
|
||||
callback=weakref.ref(_on_done),
|
||||
should_stop=partial(
|
||||
_should_stop_others, handled_exception_ids=handled_exception_ids
|
||||
),
|
||||
future_type=concurrent.futures.Future,
|
||||
)
|
||||
|
||||
|
||||
def test_failed_task_wakes_waiter_before_others_finish() -> None:
|
||||
futures = _futures(set())
|
||||
ok, bad, pending = (concurrent.futures.Future() for _ in range(3))
|
||||
for fut in (ok, bad, pending):
|
||||
futures[fut] = object()
|
||||
|
||||
ok.set_result(None)
|
||||
assert not futures.event.is_set()
|
||||
|
||||
bad.set_exception(ValueError("boom"))
|
||||
assert futures.event.is_set()
|
||||
|
||||
|
||||
def test_handled_failure_waits_for_all_tasks() -> None:
|
||||
handled: set[int] = set()
|
||||
futures = _futures(handled)
|
||||
bad, pending = (concurrent.futures.Future() for _ in range(2))
|
||||
for fut in (bad, pending):
|
||||
futures[fut] = object()
|
||||
|
||||
exc = ValueError("handled by an error handler")
|
||||
handled.add(id(exc))
|
||||
bad.set_exception(exc)
|
||||
assert not futures.event.is_set()
|
||||
|
||||
pending.set_result(None)
|
||||
assert futures.event.is_set()
|
||||
@@ -73,6 +73,15 @@ _CHILD_CONFIGS = pytest.mark.parametrize(
|
||||
ids=["no_config", "own_thread", "caller_thread"],
|
||||
)
|
||||
|
||||
_OWN_THREAD_CONFIGS = pytest.mark.parametrize(
|
||||
"child_config",
|
||||
[
|
||||
{"configurable": {"thread_id": "child"}},
|
||||
{"configurable": {"thread_id": "parent"}},
|
||||
],
|
||||
ids=["own_thread", "caller_thread"],
|
||||
)
|
||||
|
||||
|
||||
_TASK_CALLERS = pytest.mark.parametrize(
|
||||
"nested", [False, True], ids=["from_entrypoint", "from_task_in_task"]
|
||||
@@ -202,15 +211,8 @@ async def test_child_graph_streams_only_to_the_node_streaming_it_async(
|
||||
assert len(received) == len(_CHAT_AND_ECHO_EVENTS)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"child_config",
|
||||
[
|
||||
{"configurable": {"thread_id": "child"}},
|
||||
{"configurable": {"thread_id": "parent"}},
|
||||
],
|
||||
ids=["own_thread", "caller_thread"],
|
||||
)
|
||||
def test_child_graph_on_its_own_thread_skips_a_caller_streaming_subgraphs(
|
||||
@_OWN_THREAD_CONFIGS
|
||||
def test_child_graph_on_its_own_thread_streams_to_a_caller_streaming_subgraphs(
|
||||
child_config: dict,
|
||||
) -> None:
|
||||
child = _one_node_graph(_chat_and_echo)
|
||||
@@ -224,9 +226,32 @@ def test_child_graph_on_its_own_thread_skips_a_caller_streaming_subgraphs(
|
||||
{"configurable": {"thread_id": "parent"}},
|
||||
stream_mode=["messages", "tools"],
|
||||
subgraphs=True,
|
||||
version="v2",
|
||||
)
|
||||
|
||||
assert list(parts) == []
|
||||
assert [_summary(part) for part in parts] == _CHAT_AND_ECHO_EVENTS
|
||||
|
||||
|
||||
@NEEDS_CONTEXTVARS
|
||||
@_OWN_THREAD_CONFIGS
|
||||
async def test_child_graph_on_its_own_thread_streams_to_a_caller_streaming_subgraphs_async(
|
||||
child_config: dict,
|
||||
) -> None:
|
||||
child = _one_node_graph(_achat_and_echo)
|
||||
|
||||
async def node(state: MessagesState) -> dict:
|
||||
await child.ainvoke({"messages": []}, child_config)
|
||||
return {}
|
||||
|
||||
parts = _one_node_graph(node).astream(
|
||||
{"messages": []},
|
||||
{"configurable": {"thread_id": "parent"}},
|
||||
stream_mode=["messages", "tools"],
|
||||
subgraphs=True,
|
||||
version="v2",
|
||||
)
|
||||
|
||||
assert [_summary(part) async for part in parts] == _CHAT_AND_ECHO_EVENTS
|
||||
|
||||
|
||||
def test_node_gets_the_events_of_every_subgraph_it_streams() -> None:
|
||||
|
||||
Generated
+4
-4
@@ -1440,7 +1440,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.20"
|
||||
version = "1.2.22"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -1629,7 +1629,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
source = { editable = "../checkpoint" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -1677,7 +1677,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint-postgres"
|
||||
version = "3.2.0"
|
||||
version = "3.2.1"
|
||||
source = { editable = "../checkpoint-postgres" }
|
||||
dependencies = [
|
||||
{ name = "langgraph-checkpoint" },
|
||||
@@ -1827,7 +1827,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.1"
|
||||
version = "1.1.2"
|
||||
source = { editable = "../prebuilt" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
@@ -638,15 +638,15 @@ def create_react_agent(
|
||||
messages = (
|
||||
_get_state_value(state, "llm_input_messages")
|
||||
) or _get_state_value(state, "messages")
|
||||
error_msg = f"Expected input to call_model to have 'llm_input_messages' or 'messages' key, but got {state}"
|
||||
expected_keys = "'llm_input_messages' or 'messages' key"
|
||||
else:
|
||||
messages = _get_state_value(state, "messages")
|
||||
error_msg = (
|
||||
f"Expected input to call_model to have 'messages' key, but got {state}"
|
||||
)
|
||||
expected_keys = "'messages' key"
|
||||
|
||||
if messages is None:
|
||||
raise ValueError(error_msg)
|
||||
raise ValueError(
|
||||
f"Expected input to call_model to have {expected_keys}, but got {state}"
|
||||
)
|
||||
|
||||
_validate_chat_history(messages)
|
||||
# we're passing messages under `messages` key, as this is expected by the prompt
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.1"
|
||||
version = "1.1.2"
|
||||
description = "Library with high-level APIs for creating and executing LangGraph agents and tools."
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
|
||||
Generated
+4
-4
@@ -381,7 +381,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.20"
|
||||
version = "1.2.22"
|
||||
source = { editable = "../langgraph" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -465,7 +465,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
source = { editable = "../checkpoint" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -513,7 +513,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint-postgres"
|
||||
version = "3.2.0"
|
||||
version = "3.2.1"
|
||||
source = { editable = "../checkpoint-postgres" }
|
||||
dependencies = [
|
||||
{ name = "langgraph-checkpoint" },
|
||||
@@ -607,7 +607,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.1"
|
||||
version = "1.1.2"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
Generated
+3
-3
@@ -394,7 +394,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.20"
|
||||
version = "1.2.22"
|
||||
source = { editable = "../langgraph" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -478,7 +478,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
source = { editable = "../checkpoint" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -526,7 +526,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.1"
|
||||
version = "1.1.2"
|
||||
source = { editable = "../prebuilt" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
Reference in new issue
Block a user