Compare commits

...
Author SHA1 Message Date
Elior Nataf Lackritz 08a2016953 release(langgraph): 1.2.22 (#9702)
Bumps langgraph from 1.2.21 to 1.2.22.

Includes:

- Stream a child graph that runs on its own thread to a caller streaming
with `subgraphs=True` again, as before 1.2.19 (#9698)
- Skip message conversion in `add_messages` for plain messages (#9631)
- Filter pending writes to the given tasks before reading their statuses
(#9634)
- Match update keys against a set on wide states (#9632)
- Check only failed futures for the runner stop condition (#9630)
- Find untracked and delta channels once per run (#9629)
2026-11-13 20:25:56 -05:00
Elior Nataf Lackritz ebd3ab4abf release(checkpoint-postgres): 3.2.1 (#9700)
Bumps langgraph-checkpoint-postgres from 3.2.0 to 3.2.1.

Includes:

- Run a lone store read outside a pipeline (#9635)
- Skip the async saver's lock for connection pools (#9638)
- Load no pending writes without a thread hop (#9639)
2026-11-14 01:25:44 +00:00
Sreekara Yachamaneni 9b0d8857cc feat(cli): deploy to BYOC data planes with only an API key (#9692)
Linear: https://linear.app/langchain/issue/LSD-2184

`langgraph deploy` and its `list`, `revisions`, `logs`, and `delete`
subcommands only knew the SaaS control plane. On a BYOC data plane (AWS,
Azure, or GCP), users needed `--host-url`, got a raw 400 (`'source' must
be ...`) on CLI builds, defaulted to `serverless` even where scale to
zero isn't available, and got dashboard links pointing at the data
plane. This PR makes them work with only a LangSmith API key.

**Workspace discovery**
- Unless `--host-url` is given, the CLI calls `GET <LangSmith
API>/api/v1/workspaces` (root from `LANGSMITH_ENDPOINT`, default
`https://api.smith.langchain.com`).
- It picks the workspace from `LANGSMITH_TENANT_ID`, else the only
non-deleted workspace, else the `data_plane_url` all workspaces share.
When the key's workspaces sit on different planes and no tenant is set,
it keeps today's control-plane target, so the existing 403 workspace
prompt still applies. A `LANGSMITH_TENANT_ID` that none of the key's
workspaces match is an error that names it.
- With a `data_plane_url`, a `DataPlaneClient` sends host API calls to
`<dp>/api-host` with that workspace as `X-Tenant-ID`. Status links and
the "not enabled" message point at the LangSmith UI (regional
`smith.langchain.com`, or the self-hosted root), never the data plane.
- Without a `data_plane_url`, or when discovery fails, behavior is
unchanged: SaaS pays one extra call, and no tenant header is added.
- `--host-url` / `LANGGRAPH_HOST_URL` still overrides everything and
skips discovery.

**Data plane capabilities (`<dp>/api/v1/info` `instance_flags`; a
missing flag is false)**
- Local builds and `--remote` need `deployments_cli_builds_supported`.
Without it the CLI fails before any lookup or create, pointing at
`--push-to <registry>/<repository>` and `--image-uri`. External images
keep working and never read `/info`.
- `--agent-server-type` now defaults to `serverless` on SaaS and on data
planes with scale to zero, and to `dev` otherwise. An explicit
`serverless` or `dedicated`/`prod` the data plane doesn't support is a
usage error.
- Create bodies on a data plane are sized by `resource_spec`: `{}` for
serverless and dev, `null` for dedicated, and `deployment_type` is
`dev_zero`, omitted, or `prod`. SaaS keeps today's body.

**Older data planes**
- A 400 whose detail starts with `'source' must be` gets the same
`--push-to` / `--image-uri` hint, following the existing AWS
scale-to-zero hint.

**Unchanged and verified**
- Registry login still uses the returned `username` and `repository_url`
(falling back to `oauth2accesstoken`), which covers ECR, ACR, and
Artifact Registry `<repo>/<service>` responses.
- Upload headers from the backend are forwarded; S3 (`Content-Type`
only) and Azure (`x-ms-blob-type`) are covered by tests.
- `logs --type build` and revision polling call `list_revisions` /
`get_build_logs` on the same client, so they carry the discovered
workspace.

`_request` moved to a small shared base so the LangSmith API client
reuses the host backend's error handling. Unit tests now stub discovery
by default so none of them reach the network.

The JS CLI in langgraphjs (`libs/langgraph-cli`) has the same deploy
commands and needs a matching change.

Flag: none

## Test Plan
- [x] New unit tests for discovery selection, data-plane routing and
tenant headers, `/info` flag parsing, create bodies per type, early
refusals, the `'source' must be` hint, BYOC build logs, and BYOC
dashboard links on `*.langchain.com` data-plane hosts
- [ ] Against a real BYOC data plane on each of AWS, Azure, and GCP with
CLI builds on: `langgraph deploy` (local and `--remote`), `list`,
`revisions list`, `logs` (runtime and `--type build`), `delete`
- [ ] Against a BYOC data plane without CLI builds: local deploy fails
early with the hint; `--image-uri` deploy succeeds
2026-11-13 20:21:54 -05:00
Elior Nataf Lackritz cde6bd46a6 release(prebuilt): 1.1.2 (#9701)
Bumps langgraph-prebuilt from 1.1.1 to 1.1.2.

Includes:

- Only format the `call_model` error message when raising (#9628)
2026-11-13 20:21:50 -05:00
Elior Nataf Lackritz 2adf8a1382 release(checkpoint): 4.3.2 (#9699)
Bumps langgraph-checkpoint from 4.3.1 to 4.3.2.

Includes:

- Stop `InMemoryStore` search at the requested page (#9633)
- Dedupe batched store reads with a dict (#9636)
2026-11-13 20:21:07 -05:00
Elior Nataf Lackritz ccfb0a1bf8 fix(langgraph): stream a child graph on its own thread to a caller streaming subgraphs (#9698)
Since #9458, a graph that runs outside the caller's task (invoked inside
a node with its own `thread_id`, `checkpoint_ns` or `checkpoint_id`, or
through a runnable called with its own `configurable`) drops every
`messages` and `tools` handler it inherits. That keeps its events out of
a caller streaming without `subgraphs=True` (#5674), where they showed
up as the caller's own. It also dropped them for a caller that streams
with `subgraphs=True`, which asks for the events of every graph that
runs under it, so since 1.2.19 that caller gets none of the child's
tokens, tool events or node-output messages. The child now drops only
the handlers without `subgraphs`.

The child's events keep the namespace they had before 1.2.19, the
child's own, which is `()` for its top-level nodes. Moving them under
the calling node's namespace would change their shape a second time for
callers that read them today.


`test_child_graph_on_its_own_thread_streams_to_a_caller_streaming_subgraphs`
and its async twin fail on `main` (4 cases). JS needs no change: its
handlers filter these events only without `subgraphs` (`isOutsideStream`
in `libs/langgraph-core/src/pregel/stream.ts`).

## Release note

A graph invoked inside a node with its own `thread_id`, `checkpoint_ns`
or `checkpoint_id`, or through a runnable called with its own
`configurable`, streams its `messages` and `tools` events to a caller
that streams with `subgraphs=True` again, as it did before 1.2.19. They
still stay out of a stream without `subgraphs=True`.
2026-11-13 19:58:23 -05:00
Elior Nataf Lackritz 3183e90eca perf(checkpoint-postgres): run a lone store read outside a pipeline (#9635)
`PostgresStore` and `AsyncPostgresStore` always ran a batch inside a
pipeline. For a read that costs two round trips: fetching the result
forces one, and leaving the pipeline sends a Sync and waits for a
second. `store.get()`, `store.search()` and `store.list_namespaces()`
are batches of one read, so each paid two round trips, which is why a
`get` took longer than a `put`.

A batch of exactly one read now runs outside the pipeline, in one round
trip. Batches with several ops or any write still use the pipeline, and
so does a store opened with `pipeline=True`. In the sync store the
non-pipeline cursor now takes the store's lock, as every batch already
did through the pipeline branch, so a lone read never uses a shared
connection while another thread has it in a pipeline. That also covers
`sweep_ttl`, which went through that branch without the lock.

Median of four alternating rounds on main and on this branch, Python
3.14, Postgres 15 in local Docker:

| workload | main | this PR | speedup |
|---|---|---|---|
| round trips for 200 `get` calls, counted with a TCP proxy | 400 | 200
| 2x |
| 200 `PostgresStore.get` | 85.2 ms | 47.8 ms | 1.78x |
| 50 `PostgresStore.search` | 25.7 ms | 16.6 ms | 1.55x |
| 200 `AsyncPostgresStore.aget`, one after another | 103 ms | 59.5 ms |
1.73x |
| the same 200 `get` with 0.5 ms added per round trip | 433 ms | 228 ms
| 1.90x |
| graph node doing one `get` and one `put` per step, 50 steps | 46.8 ms
| 37.2 ms | 1.26x |
| `put`, and many concurrent `aget` batched together | | | about 1x |

## Test plan
- [x] The existing store suites pass for the sync and async stores in
their default, `pipeline=True` and pool setups, which cover the
lone-read path and the pipeline path.
2026-11-13 00:34:51 +00:00
Elior Nataf Lackritz 8fd97561d6 perf(checkpoint-postgres): skip the async saver's lock for connection pools (#9638)
Fixes #7259

`AsyncPostgresSaver` takes its instance lock around every database call.
The lock is needed when the saver shares one connection, which serves
one call at a time, but the saver also took it with an
`AsyncConnectionPool`, which gives each call its own connection. So
every saver call in the process waited for the one before it: concurrent
runs, and within one run the background checkpoint saves and each task's
`put_writes`.

With a pool, `_cursor` no longer takes the lock. A single connection and
a shared `pipe` still do.

Python 3.14 on an Apple M5 Pro, Postgres 16,
`AsyncConnectionPool(max_size=10)`, median of 5 rounds that alternate
main and this branch (3 rounds with 1 ms round trips):

| workload | main | this PR | speedup |
|---|---|---|---|
| 8 concurrent one-turn agents on one saver | 87.1 ms | 49.8 ms | 1.75x
|
| 500-way `Send` fan-out | 246 ms | 144 ms | 1.71x |
| 200 sequential nodes | 265 ms | 190 ms | 1.39x |
| one agent turn (7 supersteps) | 12.3 ms | 10.4 ms | 1.18x |
| 8 concurrent agents, 1 ms round trips | 234 ms | 60 ms | 3.86x |
| 500-way fan-out, 1 ms round trips | 773 ms | 167 ms | 4.63x |
| 200 sequential nodes, 1 ms round trips | 696 ms | 346 ms | 2.01x |

Process CPU stays about the same. A burst of calls larger than the pool
now waits in `psycopg_pool` (30 s timeout by default) instead of on the
lock. Calls that must be ordered already are: the loop saves a
checkpoint after the previous one and after its DeltaChannel writes.

The sync `PostgresSaver` takes the same lock with a `ConnectionPool`.
Removing it there let up to 19 executor threads contend for the GIL and
the pool: 1.3x to 1.5x faster, but 1.2x to 2.4x more CPU, so this PR
leaves it. `AsyncShallowPostgresSaver` is deprecated and also keeps its
lock.

@glaziermag reported this in #7259 with a load test, and @jitokim
proposed the same fix in #7269.

## Test plan
- [x] New `test_pool_saver_reads_while_a_list_is_open`: with a pool,
`aget_tuple` completes while an `alist` iteration is open. On main it
waits for the lock and times out after 5 s.
- [x] `checkpoint-postgres` suite (427 passed, 3 skipped), the
checkpoint conformance suite on a pool, and langgraph's Postgres tests
against this saver (2,832 passed).
2026-11-13 00:11:29 +00:00
Elior Nataf Lackritz ba0acae562 perf(langgraph): skip message conversion in add_messages for plain messages (#9631)
`add_messages` ran `convert_to_messages` and `message_chunk_to_message`
over every message already in the state on every update, plus a final
filter pass. Both conversions return a plain message unchanged, so on a
long history this is overhead: 0.18 ms per call at 1,000 messages. An
agent turn calls it once per update and once more for each conditional
edge that reads `messages`.

The list now passes through as is when every item is already a plain
message, checked on the few distinct types in the list rather than per
item, and the filter runs only when a message was removed. Anything else
(dicts, tuples, strings, chunks, or a mix) goes through the same
conversion as before.

Median of alternating rounds on main and on this branch, Python 3.14, an
agent turn being 3 tool calls on a thread with 1,000 messages:

| workload | main | this PR | speedup |
|---|---|---|---|
| `add_messages` on 1,000 messages plus 1 new one | 184 us | 69 us |
2.7x |
| agent turn, no checkpointer | 4.80 ms | 3.38 ms | 1.42x |
| agent turn, `InMemorySaver`, `durability="exit"` | 11.0 ms | 8.7 ms |
1.25x |
| agent turn, `InMemorySaver`, default durability | 22.4 ms | 21.1 ms |
1.06x |

With a checkpoint every step, saving the whole history at each step
dominates the turn, so the gain is small there.

@jkennedyvz trimmed the same per-message conversion loop in #6969.

## Test plan
- [x] Missing ids are still assigned and the result is still a new list.
Two new tests in `test_messages_state.py` cover message-likes and chunks
on both sides, and that the result is not the input list. They pass on
main too.
2026-11-13 00:10:04 +00:00
Elior Nataf Lackritz cd95887ec5 perf(checkpoint): stop InMemoryStore search at the requested page (#9633)
`InMemoryStore.search` built an `(item, vectors)` pair for every item in
every matching namespace, then kept `candidates[offset:offset + limit]`.
A `limit=10` search on a 10,000-item namespace took 1.9 ms, nearly all
of it building candidates the slice then dropped. Paging a namespace 100
items at a time, as deepagents' `StoreBackend` does for `ls`, `glob` and
`grep`, rebuilt the whole listing for every page.

Without a filter, and without a query the store can embed, the result is
exactly that slice, so `_filter_items` now stops once it has `offset +
limit` matches. The namespace prefix test also becomes one tuple
compare: a namespace shorter than the prefix slices to itself, so it can
never equal the prefix.

Median of four alternating rounds on main and on this branch, Python
3.14:

| workload | main | this PR | speedup |
|---|---|---|---|
| `search(ns, limit=10)` on a 1,000-item namespace | 70 µs | 4 µs | 17x
|
| same, 10,000 items | 1.92 ms | 4 µs | about 490x |
| graph node that searches 1,000 memories (`limit=5`) and adds one, 20
steps | 3.41 ms | 1.86 ms | 1.83x |
| same, 10,000 memories | 39.4 ms | 1.83 ms | 21.5x |
| paging 5,000 items 100 at a time | 47.1 ms | 30.1 ms | 1.57x |
| `search` over 10,000 one-item namespaces | 23.4 ms | 15.5 ms | 1.51x |
| `search` with a filter, `get`, `put` | | | about 1x |

A filter keeps the full scan, so a filter that raises on some item (a
range operator on a missing key) still raises the same way. Negative
`offset` or `limit` keep it too, since a slice counts those from the
end.

## Test plan
- [x] `test_search_pages_match_full_listing` pages plain and filtered
searches across nested and look-alike namespaces and compares each page
with the full listing. It passes on main too, since the results are
unchanged.
2026-11-13 00:08:22 +00:00
Elior Nataf Lackritz 96686e5700 perf(checkpoint-postgres): load no pending writes without a thread hop (#9639)
`AsyncPostgresSaver` deserializes a checkpoint's pending writes in
`asyncio.to_thread`. The hand-off costs about 50 us per checkpoint, and
it ran even when there was nothing to load: the usual case for the
latest checkpoint of a finished run, which `aget_state` and the start of
every run read.

It now returns an empty list without the hop when the row has no pending
writes.

Python 3.14 on an Apple M5 Pro, Postgres 16, median of 5 rounds that
alternate main and this branch:

| workload | main | this PR | speedup |
|---|---|---|---|
| `aget_tuple` of a checkpoint with no pending writes | 264 us | 214 us
| 1.24x |
| `aget_state`, 200-message thread | 2.46 ms | 2.13 ms | 1.16x |
| an agent turn on that thread | 23.8 ms | 23.7 ms | about 1x |

With 1 ms round trips the round trip dominates each call, so wall time
stays about 1x, while process CPU per `aget_tuple` is still 1.36x lower.

`_load_writes(None)` already returned an empty list, so the result is
the same.

## Test plan
- [x] `checkpoint-postgres` suite (426 passed, 3 skipped) and the
checkpoint conformance suite (`get_tuple`, `list`).
2026-11-12 18:55:18 -05:00
Elior Nataf Lackritz acff17b041 perf(checkpoint): dedupe batched store reads with a dict (#9636)
`AsyncBatchedBaseStore` collects the store calls made in the same event
loop tick into one batch and dedupes them first. `_dedupe_ops` found an
earlier equal read with `dedupped.index(op)`, a linear scan per read, so
a batch of n reads cost O(n²) comparisons: 1.1 ms for 500 concurrent
`aget` calls, 16 ms for 2,000. A fan-out where every task reads the
store fills exactly such a batch.

Reads are now looked up in a dict keyed by the op. An op that can't be
hashed (a `SearchOp` with a filter dict) falls back to the old scan, so
it dedupes exactly as before.

Median of four alternating rounds on main and on this branch, Python
3.14:

| workload | main | this PR | speedup |
|---|---|---|---|
| `_dedupe_ops` on 500 gets | 1.13 ms | 31 µs | 36x |
| 2,000 concurrent `aget`, `AsyncSqliteStore` | 17.6 ms | 8.0 ms | 2.2x
|
| 2,000 concurrent `aget`, `AsyncPostgresStore` | 20.2 ms | 11.3 ms |
1.78x |
| 500 concurrent `aget`, either store | | | 1.25x |
| 500-way `Send` fan-out where each task reads and writes the store | |
| about 1x |

## Test plan
- [x] `test_dedupe_ops_maps_repeated_reads_and_puts_to_one_op` covers
repeated gets, filtered searches, list ops and overwritten puts. It
passes on main too, since the result is unchanged.
2026-11-12 18:55:11 -05:00
Elior Nataf Lackritz 46b2ef0e1e perf(langgraph): filter pending writes to the given tasks before reading their statuses (#9634)
Each functional API task call (`accept_push`) restores any output the
pushed task saved in an earlier run. It did that by running
`read_task_statuses` over every pending write, which builds the status
of every task that has written, and then looked up the one task. On
resume every earlier task's writes are pending, so an entrypoint that
calls n tasks paid for n full status reads, O(n²): 196 µs per task call
at 1,000 pending writes. A sync entrypoint running tasks in parallel
hits it too, since writes land while tasks are still being pushed.

`_reapply_writes_to_succeeded_nodes` now hands `read_task_statuses` only
the writes of the tasks it was given. Statuses are per task, so those
tasks get exactly the statuses they got before.

Median of four alternating rounds on main and on this branch, Python
3.14, `InMemorySaver`:

| workload | main | this PR | speedup |
|---|---|---|---|
| matching saved writes for one task call, 1,000 pending writes | 196 µs
| 9.8 µs | 20x |
| resume an entrypoint that ran 500 tasks and then called `interrupt()`
| 153 ms | 35.4 ms | 4.3x |
| same, 100 tasks | 9.8 ms | 6.1 ms | 1.6x |
| entrypoint running 500 tasks in parallel, sync | 134 ms | 90.4 ms |
1.48x |
| 100 tasks in parallel or one after another, sync or async | | | about
1x |

Async entrypoints push all their tasks before any write lands, so they
barely move. In the 500-task parallel run, the biggest remaining cost in
the profile is the runner's stop check, which #9630 addresses.

## Test plan
- [x] Existing coverage: `test_imp_task` resumes an entrypoint and
checks its tasks don't run again, and the full suite passes.
2026-11-12 18:55:04 -05:00
Elior Nataf Lackritz 496885e569 perf(langgraph): match update keys against a set on wide states (#9632)
`_get_updates` (run on every node's return value) and
`map_output_updates` (run for every task in `stream_mode="updates"`)
checked each written key against a list of all state keys, and
`map_output_updates` also looped over every key to look for duplicate
writes. On a state with hundreds of keys that is a list scan for every
written key.

Both now check against a frozenset: `_get_updates` builds it once when
the node is attached, `map_output_updates` once per call. The updates
keep the order of the node's writes.

Median of four alternating rounds on main and on this branch, Python
3.14:

| workload | main | this PR | speedup |
|---|---|---|---|
| `map_output_updates`, 20 writes on a 300-key state | 23.3 us | 4.0 us
| 5.8x |
| 30 nodes writing 20 keys each on a 300-key state, sync | 10.7 ms | 9.6
ms | 1.12x |
| same, async | 12.9 ms | 11.7 ms | 1.10x |
| `bench` `wide_state_25x300` (17 keys), sync | 3.84 ms | 3.74 ms |
about 1x |

States with a handful of keys see no change. Compiling builds one extra
frozenset per node, about 0.5 us each (`pydantic_state_25x300` compiles
in 117 us instead of 114 us).

## Test plan
- [x] The same keys pass the check, so no new test. Existing tests cover
the update paths, including a node that writes the same key twice in
`updates` mode (`test_multiple_writes_same_channel_from_same_node`).
2026-11-12 18:54:57 -05:00
Elior Nataf Lackritz 23a77c57fd perf(langgraph): check only failed futures for the runner stop condition (#9630)
When a task in a superstep finished, `FuturesDict.on_done` ran the stop
check over every future that had finished so far. The check calls
`cancelled()` and `exception()` on each one, and both take the future's
lock for a `concurrent.futures.Future`. A step with n parallel tasks
paid about n²/2 of these calls, about 125,000 for a 500-task `Send`
fan-out.

Only a future that raised can meet the stop condition, and a finished
future never changes, so `FuturesDict` now keeps the failed futures in
their own set and checks only those. The single-task path, which adds a
failed future straight to `done`, adds it to the new set too, so the
check sees the same failures as before, including ones an error handler
takes over.

Median of four alternating rounds on main and on this branch, Python
3.14:

| workload | main | this PR | speedup |
|---|---|---|---|
| 500 futures finishing in one step, `FuturesDict` alone | 28.5 ms |
0.72 ms | 39x |
| 500-way `Send` fan-out, sync, `InMemorySaver` | 77.3 ms | 33.9 ms |
2.28x |
| 500-way `Send` fan-out, sync, no checkpointer | 65.2 ms | 28.1 ms |
2.32x |
| 500-way `Send` fan-out, async | 36.4 ms | 35.8 ms | about 1x |
| `bench` `fanout_to_subgraph_100x`, sync | 116 ms | 113 ms | about 1x |

Async barely moves because asyncio futures answer these calls without a
lock, and the 100-way benchmark spends its time inside the subgraphs.

@AaronG18 (#8860), @nankingjing (#8347) and @014-code (#8270) proposed
checking fewer futures here, and @HumphreySun98 reported the problem in
#8240.

## Test plan
- [x] `test_runner_futures.py` pins the stop decision: a failed task
wakes the waiter before the other tasks finish, and a failure an error
handler took over does not. Both tests pass on main too, since the
behavior is unchanged.
2026-11-12 18:54:50 -05:00
Elior Nataf Lackritz 40f5cc7f1f perf(langgraph): find untracked and delta channels once per run (#9629)
The Pregel loop looked up channel types in two hot places. `put_writes`
scanned every channel for an `UntrackedValue` on each task's writes, and
`_put_checkpoint` walked every channel twice per superstep to find the
`DeltaChannel`s. A graph has a channel per node plus one per state key,
and channels are ABCs, so each check goes through
`ABCMeta.__instancecheck__`: a 200-node graph paid about 630 of them per
step.

Channel types can't change during a run, so the loop now works out both
answers once from `specs` and reuses them.

Median of four alternating rounds on main and on this branch, Python
3.14:

| workload | main | this PR | speedup |
|---|---|---|---|
| channel-type checks per step, 211 channels | 38 us | under 0.1 us | |
| `bench` `sequential_1000`, sync | 219 ms | 93 ms | 2.36x |
| `bench` `sequential_1000`, async | 370 ms | 250 ms | 1.48x |
| 200 sequential nodes, `InMemorySaver` | 38.7 ms | 29.4 ms | 1.31x |
| 200 sequential nodes, no checkpointer | 19.4 ms | 12.9 ms | 1.50x |
| agent turn on a model node + `ToolNode` graph | 2.51 ms | 2.48 ms |
about 1x |

The gain grows with the number of nodes and state keys. Small graphs see
no change.

`self.channels` is built from the same `specs`, with the same keys in
the same order, so the same writes are dropped, the same `Send` packets
are sanitized, and the same delta counters and snapshots come out.

@AaronG18 proposed the `UntrackedValue` half of this in #8858, and
@jkennedyvz in #7121.

## Test plan
- [x] No behavior change, so no new test. `test_untracked_value`,
`test_send_with_untracked_value` and the `test_delta_channel_*` files
cover these paths.
2026-11-12 18:54:43 -05:00
Elior Nataf Lackritz ef6490a521 perf(prebuilt): only format the call_model error message when raising (#9628)
Fixes #9328

`_get_model_input_state` in `create_react_agent` built its
missing-messages error string before checking whether it needed it. The
f-string includes `{state}`, so every model call ran `repr()` over the
whole message history and threw the result away. The cost grows with the
history, so a long tool loop pays it on every step.

The message is now built only when the error is raised, with the same
text.

Median of four alternating rounds on main and on this branch, Python
3.14:

| workload | main | this PR | speedup |
|---|---|---|---|
| building the model input, 1,000 messages | 7.6 ms | 0.08 ms | 95x |
| one agent turn (3 tool calls) on a 1,000-message thread,
`InMemorySaver` | 60.7 ms | 29.6 ms | 2.05x |
| same turn on a 200-message thread | 14.1 ms | 8.1 ms | 1.75x |
| `bench` `react_agent_100x`, sync | 319 ms | 53 ms | 6.0x |
| `bench` `react_agent_100x`, async | 390 ms | 126 ms | 3.1x |

The saving is the cost of that `repr()`, so it grows with the size of
the history.

Thanks @deadstrobe5, who reported this in #9328 and proposed the same
change in #9329.

## Test plan
- [x] Behavior is unchanged: the same `ValueError` with the same text
when the input has no messages. The `create_react_agent` tests in
`libs/prebuilt` cover the call path.
2026-11-12 18:53:53 -05:00
Richard Scarrott a054ed08dc release(langgraph): 1.2.21 (#9683)
Bumps langgraph from 1.2.20 to 1.2.21.

Includes:

- Read a checkpoint namespace the graph doesn't declare, such as a
deepagents subagent's `tools:<task_id>`, with the parent graph instead
of raising `Subgraph tools not found`. The Python Agent Server no longer
returns 400 on `/history?checkpoint_ns=...` for these. Updates to such a
namespace still raise (#9614)
- Match whole namespace segments in `get_subgraphs` / `aget_subgraphs`.
With `namespace=` and `recurse=True` they now yield only the subgraph at
that namespace, and so does the Agent Server's `GET
/assistants/{id}/subgraphs/{namespace}?recurse=true` (#9615)
- Say each `interrupt()` call has its own id in the
`UnknownInterruptError` docstring (#9600)
2026-11-12 17:47:18 +00:00
36 changed files with 1648 additions and 177 deletions

No files matched your search

+1 -1
View File
@@ -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
+1 -1
View File
@@ -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 = [
+2 -2
View File
@@ -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" },
+1 -1
View File
@@ -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(
+1 -1
View File
@@ -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"
+61 -1
View File
@@ -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()
+1 -1
View File
@@ -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
View File
@@ -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 \
+144 -27
View File
@@ -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,
)
+194 -34
View File
@@ -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
+16
View File
@@ -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
)
+18 -9
View File
@@ -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,
+7 -3
View File
@@ -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 ())
+9 -9
View File
@@ -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}
+20 -14
View File
@@ -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)
+9 -1
View File
@@ -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
+13 -8
View File
@@ -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(
+1 -1
View File
@@ -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()
+35 -10
View File
@@ -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:
+4 -4
View File
@@ -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
+1 -1
View File
@@ -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"
+4 -4
View File
@@ -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" },
+3 -3
View File
@@ -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" },