langgraph 1.2.20__tar.gz → 1.2.22__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. {langgraph-1.2.20 → langgraph-1.2.22}/PKG-INFO +1 -1
  2. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/errors.py +3 -2
  3. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/graph/message.py +18 -9
  4. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/graph/state.py +7 -3
  5. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_io.py +9 -9
  6. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_loop.py +20 -14
  7. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_runner.py +9 -1
  8. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/main.py +48 -20
  9. {langgraph-1.2.20 → langgraph-1.2.22}/pyproject.toml +1 -1
  10. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_messages_state.py +36 -0
  11. langgraph-1.2.22/tests/test_runner_futures.py +50 -0
  12. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_namespaces.py +35 -10
  13. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_subgraph_persistence.py +308 -2
  14. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_subgraph_persistence_async.py +321 -2
  15. {langgraph-1.2.20 → langgraph-1.2.22}/uv.lock +211 -124
  16. {langgraph-1.2.20 → langgraph-1.2.22}/.gitignore +0 -0
  17. {langgraph-1.2.20 → langgraph-1.2.22}/LICENSE +0 -0
  18. {langgraph-1.2.20 → langgraph-1.2.22}/Makefile +0 -0
  19. {langgraph-1.2.20 → langgraph-1.2.22}/README.md +0 -0
  20. {langgraph-1.2.20 → langgraph-1.2.22}/bench/__init__.py +0 -0
  21. {langgraph-1.2.20 → langgraph-1.2.22}/bench/__main__.py +0 -0
  22. {langgraph-1.2.20 → langgraph-1.2.22}/bench/fanout_to_subgraph.py +0 -0
  23. {langgraph-1.2.20 → langgraph-1.2.22}/bench/pydantic_state.py +0 -0
  24. {langgraph-1.2.20 → langgraph-1.2.22}/bench/react_agent.py +0 -0
  25. {langgraph-1.2.20 → langgraph-1.2.22}/bench/sequential.py +0 -0
  26. {langgraph-1.2.20 → langgraph-1.2.22}/bench/serde_allowlist.py +0 -0
  27. {langgraph-1.2.20 → langgraph-1.2.22}/bench/wide_dict.py +0 -0
  28. {langgraph-1.2.20 → langgraph-1.2.22}/bench/wide_state.py +0 -0
  29. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/__init__.py +0 -0
  30. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_cache.py +0 -0
  31. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_config.py +0 -0
  32. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_constants.py +0 -0
  33. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_fields.py +0 -0
  34. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_future.py +0 -0
  35. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_pydantic.py +0 -0
  36. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_queue.py +0 -0
  37. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_replay.py +0 -0
  38. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_retry.py +0 -0
  39. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_runnable.py +0 -0
  40. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_scratchpad.py +0 -0
  41. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_serde.py +0 -0
  42. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_timeout.py +0 -0
  43. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/_internal/_typing.py +0 -0
  44. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/callbacks.py +0 -0
  45. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/__init__.py +0 -0
  46. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/any_value.py +0 -0
  47. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/base.py +0 -0
  48. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/binop.py +0 -0
  49. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/delta.py +0 -0
  50. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/ephemeral_value.py +0 -0
  51. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/last_value.py +0 -0
  52. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/named_barrier_value.py +0 -0
  53. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/topic.py +0 -0
  54. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/channels/untracked_value.py +0 -0
  55. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/config.py +0 -0
  56. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/constants.py +0 -0
  57. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/func/__init__.py +0 -0
  58. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/graph/__init__.py +0 -0
  59. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/graph/_branch.py +0 -0
  60. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/graph/_node.py +0 -0
  61. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/graph/ui.py +0 -0
  62. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/managed/__init__.py +0 -0
  63. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/managed/base.py +0 -0
  64. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/managed/is_last_step.py +0 -0
  65. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/__init__.py +0 -0
  66. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_algo.py +0 -0
  67. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_call.py +0 -0
  68. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_checkpoint.py +0 -0
  69. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_config.py +0 -0
  70. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_draw.py +0 -0
  71. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_executor.py +0 -0
  72. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_log.py +0 -0
  73. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_messages.py +0 -0
  74. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_read.py +0 -0
  75. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_remote_run_stream.py +0 -0
  76. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_retry.py +0 -0
  77. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_task_status.py +0 -0
  78. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_tools.py +0 -0
  79. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_utils.py +0 -0
  80. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_validate.py +0 -0
  81. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/_write.py +0 -0
  82. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/debug.py +0 -0
  83. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/protocol.py +0 -0
  84. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/remote.py +0 -0
  85. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/pregel/types.py +0 -0
  86. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/py.typed +0 -0
  87. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/runtime.py +0 -0
  88. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/stream/__init__.py +0 -0
  89. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/stream/_convert.py +0 -0
  90. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/stream/_mux.py +0 -0
  91. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/stream/_types.py +0 -0
  92. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/stream/run_stream.py +0 -0
  93. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/stream/stream_channel.py +0 -0
  94. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/stream/transformers.py +0 -0
  95. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/types.py +0 -0
  96. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/typing.py +0 -0
  97. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/utils/__init__.py +0 -0
  98. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/utils/config.py +0 -0
  99. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/utils/runnable.py +0 -0
  100. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/version.py +0 -0
  101. {langgraph-1.2.20 → langgraph-1.2.22}/langgraph/warnings.py +0 -0
  102. {langgraph-1.2.20 → langgraph-1.2.22}/tests/__init__.py +0 -0
  103. {langgraph-1.2.20 → langgraph-1.2.22}/tests/__snapshots__/test_large_cases.ambr +0 -0
  104. {langgraph-1.2.20 → langgraph-1.2.22}/tests/__snapshots__/test_pregel.ambr +0 -0
  105. {langgraph-1.2.20 → langgraph-1.2.22}/tests/__snapshots__/test_pregel_async.ambr +0 -0
  106. {langgraph-1.2.20 → langgraph-1.2.22}/tests/agents.py +0 -0
  107. {langgraph-1.2.20 → langgraph-1.2.22}/tests/any_int.py +0 -0
  108. {langgraph-1.2.20 → langgraph-1.2.22}/tests/any_str.py +0 -0
  109. {langgraph-1.2.20 → langgraph-1.2.22}/tests/compose-postgres.yml +0 -0
  110. {langgraph-1.2.20 → langgraph-1.2.22}/tests/compose-redis.yml +0 -0
  111. {langgraph-1.2.20 → langgraph-1.2.22}/tests/conftest.py +0 -0
  112. {langgraph-1.2.20 → langgraph-1.2.22}/tests/conftest_checkpointer.py +0 -0
  113. {langgraph-1.2.20 → langgraph-1.2.22}/tests/conftest_store.py +0 -0
  114. {langgraph-1.2.20 → langgraph-1.2.22}/tests/example_app/example_graph.py +0 -0
  115. {langgraph-1.2.20 → langgraph-1.2.22}/tests/example_app/langgraph.json +0 -0
  116. {langgraph-1.2.20 → langgraph-1.2.22}/tests/example_app/requirements.txt +0 -0
  117. {langgraph-1.2.20 → langgraph-1.2.22}/tests/fake_chat.py +0 -0
  118. {langgraph-1.2.20 → langgraph-1.2.22}/tests/fake_tracer.py +0 -0
  119. {langgraph-1.2.20 → langgraph-1.2.22}/tests/memory_assert.py +0 -0
  120. {langgraph-1.2.20 → langgraph-1.2.22}/tests/messages.py +0 -0
  121. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_algo.py +0 -0
  122. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_channels.py +0 -0
  123. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_checkpoint_migration.py +0 -0
  124. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_config_async.py +0 -0
  125. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_benchmark.py +0 -0
  126. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_exit_mode.py +0 -0
  127. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_fork.py +0 -0
  128. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_id_stability.py +0 -0
  129. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_migration.py +0 -0
  130. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_node_cache.py +0 -0
  131. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_parallel_order.py +0 -0
  132. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_run_input.py +0 -0
  133. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_seal_subscribers.py +0 -0
  134. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_subgraph.py +0 -0
  135. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_supersteps_bound.py +0 -0
  136. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_to_plain.py +0 -0
  137. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_update_overwrite.py +0 -0
  138. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_update_state.py +0 -0
  139. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_delta_channel_write_failures.py +0 -0
  140. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_deprecation.py +0 -0
  141. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_graph_callbacks.py +0 -0
  142. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_interleave_arrival_order.py +0 -0
  143. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_interrupt_migration.py +0 -0
  144. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_interrupt_state.py +0 -0
  145. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_interruption.py +0 -0
  146. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_large_cases.py +0 -0
  147. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_large_cases_async.py +0 -0
  148. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_managed_values.py +0 -0
  149. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_parent_command.py +0 -0
  150. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_parent_command_async.py +0 -0
  151. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_pregel.py +0 -0
  152. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_pregel_async.py +0 -0
  153. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_pregel_debug.py +0 -0
  154. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_pregel_stream_events_v3.py +0 -0
  155. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_prune_postgres.py +0 -0
  156. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_pydantic.py +0 -0
  157. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_remote_graph.py +0 -0
  158. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_remote_graph_v3.py +0 -0
  159. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_retry.py +0 -0
  160. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_runnable.py +0 -0
  161. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_runtime.py +0 -0
  162. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_serde_allowlist.py +0 -0
  163. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_state.py +0 -0
  164. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_before_builtins.py +0 -0
  165. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_data_transformers.py +0 -0
  166. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_events_v3.py +0 -0
  167. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_events_v3_e2e.py +0 -0
  168. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_events_v3_kwarg_forwarding.py +0 -0
  169. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_lifecycle_transformer.py +0 -0
  170. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_messages_transformer.py +0 -0
  171. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_stream_subgraph_transformer.py +0 -0
  172. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_strict_resume.py +0 -0
  173. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_subgraph_detection.py +0 -0
  174. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_time_travel.py +0 -0
  175. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_time_travel_async.py +0 -0
  176. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_tool_stream_handler.py +0 -0
  177. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_trace_policy.py +0 -0
  178. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_tracing_interops.py +0 -0
  179. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_type_checking.py +0 -0
  180. {langgraph-1.2.20 → langgraph-1.2.22}/tests/test_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: langgraph
3
- Version: 1.2.20
3
+ Version: 1.2.22
4
4
  Summary: Building stateful, multi-actor applications with LLMs
5
5
  Project-URL: Homepage, https://docs.langchain.com/oss/python/langgraph/overview
6
6
  Project-URL: Documentation, https://reference.langchain.com/python/langgraph/
@@ -164,8 +164,9 @@ class UnknownInterruptError(Exception):
164
164
  A single value with no interrupt pending is accepted, because it resumes a
165
165
  graph paused at a static breakpoint (`interrupt_before` or `interrupt_after`).
166
166
 
167
- The interrupts one task raises share an id, so an answer retried after the
168
- task asked its next question answers that question.
167
+ Each `interrupt()` call in a task has its own id, so an answer retried after
168
+ the task asked its next question is refused instead of answering that
169
+ question.
169
170
  """
170
171
 
171
172
  reason: Literal["not_pending", "empty_map", "ambiguous"]
@@ -194,14 +194,8 @@ def add_messages(
194
194
  if not isinstance(right, list):
195
195
  right = [right] # type: ignore[assignment]
196
196
  # coerce to message
197
- left = [
198
- message_chunk_to_message(cast(BaseMessageChunk, m))
199
- for m in convert_to_messages(left)
200
- ]
201
- right = [
202
- message_chunk_to_message(cast(BaseMessageChunk, m))
203
- for m in convert_to_messages(right)
204
- ]
197
+ left = _coerce_to_messages(left)
198
+ right = _coerce_to_messages(right)
205
199
  # assign missing ids
206
200
  for m in left:
207
201
  if m.id is None:
@@ -234,7 +228,8 @@ def add_messages(
234
228
 
235
229
  merged_by_id[m.id] = len(merged)
236
230
  merged.append(m)
237
- merged = [m for m in merged if m.id not in ids_to_remove]
231
+ if ids_to_remove:
232
+ merged = [m for m in merged if m.id not in ids_to_remove]
238
233
 
239
234
  if format == "langchain-openai":
240
235
  merged = _format_messages(merged)
@@ -247,6 +242,20 @@ def add_messages(
247
242
  return merged
248
243
 
249
244
 
245
+ def _coerce_to_messages(messages: list) -> list[Any]:
246
+ # Both conversions return a plain message unchanged, and a long history is
247
+ # all plain messages, so check the few distinct types instead of each item.
248
+ if all(
249
+ issubclass(t, BaseMessage) and not issubclass(t, BaseMessageChunk)
250
+ for t in set(map(type, messages))
251
+ ):
252
+ return messages
253
+ return [
254
+ message_chunk_to_message(cast(BaseMessageChunk, m))
255
+ for m in convert_to_messages(messages)
256
+ ]
257
+
258
+
250
259
  @deprecated(
251
260
  "MessageGraph is deprecated in langgraph 1.0.0, to be removed in 2.0.0. Please use StateGraph with a `messages` key instead.",
252
261
  category=None,
@@ -1452,6 +1452,8 @@ class CompiledStateGraph(
1452
1452
  output_keys = list(self.builder.channels) + [
1453
1453
  k for k, v in self.builder.managed.items()
1454
1454
  ]
1455
+ # for membership checks, which run on every key of every node update
1456
+ output_key_set = frozenset(output_keys)
1455
1457
 
1456
1458
  def _get_updates(
1457
1459
  input: None | dict | Any,
@@ -1459,12 +1461,12 @@ class CompiledStateGraph(
1459
1461
  if input is None:
1460
1462
  return None
1461
1463
  elif isinstance(input, dict):
1462
- return [(k, v) for k, v in input.items() if k in output_keys]
1464
+ return [(k, v) for k, v in input.items() if k in output_key_set]
1463
1465
  elif isinstance(input, Command):
1464
1466
  if input.graph == Command.PARENT:
1465
1467
  return None
1466
1468
  return [
1467
- (k, v) for k, v in input._update_as_tuples() if k in output_keys
1469
+ (k, v) for k, v in input._update_as_tuples() if k in output_key_set
1468
1470
  ]
1469
1471
  elif (
1470
1472
  isinstance(input, (list, tuple))
@@ -1477,7 +1479,9 @@ class CompiledStateGraph(
1477
1479
  if i.graph == Command.PARENT:
1478
1480
  continue
1479
1481
  updates.extend(
1480
- (k, v) for k, v in i._update_as_tuples() if k in output_keys
1482
+ (k, v)
1483
+ for k, v in i._update_as_tuples()
1484
+ if k in output_key_set
1481
1485
  )
1482
1486
  else:
1483
1487
  updates.extend(_get_updates(i) or ())
@@ -130,35 +130,35 @@ def map_output_updates(
130
130
  ]
131
131
  if not output_tasks:
132
132
  return
133
+ # a set: a wide state would otherwise scan every key for each write
134
+ output_keys = (
135
+ frozenset(output_channels) if not isinstance(output_channels, str) else None
136
+ )
133
137
  updated: list[tuple[str, Any]] = []
134
138
  for task, writes in output_tasks:
135
139
  rtn = next((value for chan, value in writes if chan == RETURN), MISSING)
136
140
  if rtn is not MISSING:
137
141
  updated.append((task.name, rtn))
138
- elif isinstance(output_channels, str):
142
+ elif output_keys is None:
139
143
  updated.extend(
140
144
  (task.name, value) for chan, value in writes if chan == output_channels
141
145
  )
142
- elif any(chan in output_channels for chan, _ in writes):
146
+ elif any(chan in output_keys for chan, _ in writes):
143
147
  counts = Counter(chan for chan, _ in writes)
144
- if any(counts[chan] > 1 for chan in output_channels):
148
+ if any(n > 1 for chan, n in counts.items() if chan in output_keys):
145
149
  updated.extend(
146
150
  (
147
151
  task.name,
148
152
  {chan: value},
149
153
  )
150
154
  for chan, value in writes
151
- if chan in output_channels
155
+ if chan in output_keys
152
156
  )
153
157
  else:
154
158
  updated.append(
155
159
  (
156
160
  task.name,
157
- {
158
- chan: value
159
- for chan, value in writes
160
- if chan in output_channels
161
- },
161
+ {chan: value for chan, value in writes if chan in output_keys},
162
162
  )
163
163
  )
164
164
  grouped: dict[str, Any] = {t.name: [] for t, _ in output_tasks}
@@ -358,6 +358,14 @@ class PregelLoop:
358
358
  self.retry_policy = retry_policy
359
359
  self.cache_policy = cache_policy
360
360
  self.durability = durability
361
+ # Channel types are fixed for the run, so per-step code reads these
362
+ # instead of scanning every channel.
363
+ self._has_untracked_channels = any(
364
+ isinstance(spec, UntrackedValue) for spec in specs.values()
365
+ )
366
+ self._delta_channel_names = [
367
+ k for k, spec in specs.items() if isinstance(spec, DeltaChannel)
368
+ ]
361
369
  self._has_graph_lifecycle_callbacks = has_graph_lifecycle_callbacks
362
370
  self._graph_lifecycle_events = deque()
363
371
  if self.stream is not None and CONFIG_KEY_STREAM in config[CONF]:
@@ -496,9 +504,7 @@ class PregelLoop:
496
504
  writes_to_save = writes
497
505
 
498
506
  # check if any writes are to an UntrackedValue channel
499
- if any(
500
- isinstance(channel, UntrackedValue) for channel in self.channels.values()
501
- ):
507
+ if self._has_untracked_channels:
502
508
  # we do not persist untracked values in checkpoints
503
509
  writes_to_save = [
504
510
  # sanitize UntrackedValues that are nested within Send packets
@@ -840,10 +846,12 @@ class PregelLoop:
840
846
  Unfinished (failed or interrupted) tasks keep empty writes, so the
841
847
  runner re-executes them or routes them to error handlers.
842
848
  """
843
- for tid, status in read_task_statuses(self.checkpoint_pending_writes).items():
844
- if task := tasks.get(tid):
845
- task.writes.extend(status.output)
846
- self._reapplied_task_ids.add(tid)
849
+ # Filter first: this runs once per task call, and reading the status of
850
+ # every task that wrote would make runs with many task calls quadratic.
851
+ writes = (w for w in self.checkpoint_pending_writes if w[0] in tasks)
852
+ for tid, status in read_task_statuses(writes).items():
853
+ tasks[tid].writes.extend(status.output)
854
+ self._reapplied_task_ids.add(tid)
847
855
 
848
856
  def _resume_error_handlers_if_applicable(self) -> None:
849
857
  """On resume, schedule error handlers for tasks that failed in a prior run.
@@ -1300,9 +1308,7 @@ class PregelLoop:
1300
1308
  )
1301
1309
  new_counters: dict[str, tuple[int, int]] = {}
1302
1310
  updated = self.updated_channels or set()
1303
- for ch_name, ch in self.channels.items():
1304
- if not isinstance(ch, DeltaChannel):
1305
- continue
1311
+ for ch_name in self._delta_channel_names:
1306
1312
  u, s = prev_counters.get(ch_name, (0, 0))
1307
1313
  s += 1
1308
1314
  if ch_name in updated:
@@ -1322,7 +1328,9 @@ class PregelLoop:
1322
1328
  # create new checkpoint
1323
1329
  channels_to_snapshot = (
1324
1330
  delta_channels_to_snapshot(
1325
- self.channels, new_counters, self.checkpoint["channel_versions"]
1331
+ {k: self.channels[k] for k in self._delta_channel_names},
1332
+ new_counters,
1333
+ self.checkpoint["channel_versions"],
1326
1334
  )
1327
1335
  | self._delta_channels_forced_snapshot
1328
1336
  if do_checkpoint
@@ -1351,9 +1359,7 @@ class PregelLoop:
1351
1359
  elif "counters_since_delta_snapshot" in self.checkpoint_metadata:
1352
1360
  del self.checkpoint_metadata["counters_since_delta_snapshot"]
1353
1361
  # sanitize TASK channel in the checkpoint before saving (durability=="exit")
1354
- if TASKS in self.checkpoint["channel_values"] and any(
1355
- isinstance(channel, UntrackedValue) for channel in self.channels.values()
1356
- ):
1362
+ if TASKS in self.checkpoint["channel_values"] and self._has_untracked_channels:
1357
1363
  sanitized_tasks = [
1358
1364
  sanitize_untracked_values_in_send(value, self.channels)
1359
1365
  if isinstance(value, Send)
@@ -78,6 +78,7 @@ class FuturesDict(Generic[F, E], dict[F, PregelExecutableTask | None]):
78
78
  should_stop: Callable[[set[F]], bool]
79
79
  counter: int
80
80
  done: set[F]
81
+ failed: set[F]
81
82
  lock: threading.Lock
82
83
 
83
84
  def __init__(
@@ -97,6 +98,7 @@ class FuturesDict(Generic[F, E], dict[F, PregelExecutableTask | None]):
97
98
  self.should_stop = should_stop
98
99
  self.counter = 0
99
100
  self.done: set[F] = set()
101
+ self.failed: set[F] = set()
100
102
 
101
103
  def __setitem__(
102
104
  self,
@@ -123,9 +125,13 @@ class FuturesDict(Generic[F, E], dict[F, PregelExecutableTask | None]):
123
125
  with self.lock:
124
126
  self.done.add(fut)
125
127
  self.counter -= 1
128
+ # Only a future that raised can meet the stop condition, so check
129
+ # those instead of every done future (quadratic in wide steps).
130
+ if not fut.cancelled() and fut.exception() is not None:
131
+ self.failed.add(fut)
126
132
  # Wake waiter when all tracked futures are done, or when runner-level
127
133
  # stop condition is met (for example, a non-handled fatal exception).
128
- if self.counter == 0 or self.should_stop(self.done):
134
+ if self.counter == 0 or self.should_stop(self.failed):
129
135
  self.event.set()
130
136
 
131
137
 
@@ -234,6 +240,7 @@ class PregelRunner:
234
240
  fut: concurrent.futures.Future = concurrent.futures.Future()
235
241
  fut.set_exception(exc)
236
242
  futures.done.add(fut)
243
+ futures.failed.add(fut)
237
244
  elif reraise and id(exc) not in self._handled_exception_ids:
238
245
  # let the caller stream this task's result before it raises
239
246
  yield
@@ -427,6 +434,7 @@ class PregelRunner:
427
434
  fut: asyncio.Future = loop.create_future()
428
435
  fut.set_exception(exc)
429
436
  futures.done.add(fut)
437
+ futures.failed.add(fut)
430
438
  elif reraise and id(exc) not in self._handled_exception_ids:
431
439
  # let the caller stream this task's result before it raises
432
440
  yield
@@ -884,11 +884,23 @@ class Pregel(
884
884
  )
885
885
 
886
886
  def _subgraph_for_namespace(
887
- self, config: RunnableConfig, checkpointer: BaseCheckpointSaver
887
+ self,
888
+ config: RunnableConfig,
889
+ checkpointer: BaseCheckpointSaver,
890
+ *,
891
+ for_update: bool = False,
888
892
  ) -> tuple[PregelProtocol, RunnableConfig] | None:
889
893
  """The subgraph a state method's namespaced config addresses, and the
890
894
  config to call it with, lending it `checkpointer`. `None` when the
891
- config is for this graph."""
895
+ config is for this graph, or names no subgraph this graph declares or
896
+ detects (such as one a tool invokes), so a read uses this graph's
897
+ channels.
898
+
899
+ Raises:
900
+ ValueError: If `for_update` and the namespace names no such
901
+ subgraph: this graph's nodes and channels would write the wrong
902
+ state into it.
903
+ """
892
904
  checkpoint_ns = config[CONF].get(CONFIG_KEY_CHECKPOINT_NS, "")
893
905
  if not checkpoint_ns or CONFIG_KEY_CHECKPOINTER in config[CONF]:
894
906
  return None
@@ -897,7 +909,9 @@ class Pregel(
897
909
  return pregel, patch_configurable(
898
910
  config, {CONFIG_KEY_CHECKPOINTER: checkpointer}
899
911
  )
900
- raise ValueError(f"Subgraph {recast} not found")
912
+ if for_update:
913
+ raise ValueError(f"Subgraph {recast} not found")
914
+ return None
901
915
 
902
916
  def _apply_checkpointer_allowlist(
903
917
  self, checkpointer: BaseCheckpointSaver | None
@@ -1143,7 +1157,9 @@ class Pregel(
1143
1157
  """Get the subgraphs of the graph.
1144
1158
 
1145
1159
  Args:
1146
- namespace: The namespace to filter the subgraphs by.
1160
+ namespace: Yield only the subgraph at this exact namespace, such
1161
+ as `parent|child` (without task IDs). A nested namespace needs
1162
+ `recurse=True`.
1147
1163
  recurse: Whether to recurse into the subgraphs.
1148
1164
  If `False`, only the immediate subgraphs will be returned.
1149
1165
 
@@ -1151,9 +1167,9 @@ class Pregel(
1151
1167
  An iterator of the `(namespace, subgraph)` pairs.
1152
1168
  """
1153
1169
  for name, node in self.nodes.items():
1154
- # filter by prefix
1170
+ # filter by the namespace's first segment, matched whole
1155
1171
  if namespace is not None:
1156
- if not namespace.startswith(name):
1172
+ if namespace != name and not namespace.startswith(name + NS_SEP):
1157
1173
  continue
1158
1174
 
1159
1175
  # find the subgraph, if any
@@ -1167,12 +1183,13 @@ class Pregel(
1167
1183
  if namespace is None:
1168
1184
  yield name, graph
1169
1185
  if recurse and isinstance(graph, Pregel):
1170
- if namespace is not None:
1171
- namespace = namespace[len(name) + 1 :]
1186
+ child_namespace = (
1187
+ None if namespace is None else namespace[len(name) + 1 :]
1188
+ )
1172
1189
  yield from (
1173
1190
  (f"{name}{NS_SEP}{n}", s)
1174
1191
  for n, s in graph.get_subgraphs(
1175
- namespace=namespace, recurse=recurse
1192
+ namespace=child_namespace, recurse=recurse
1176
1193
  )
1177
1194
  )
1178
1195
 
@@ -1182,7 +1199,9 @@ class Pregel(
1182
1199
  """Get the subgraphs of the graph.
1183
1200
 
1184
1201
  Args:
1185
- namespace: The namespace to filter the subgraphs by.
1202
+ namespace: Yield only the subgraph at this exact namespace, such
1203
+ as `parent|child` (without task IDs). A nested namespace needs
1204
+ `recurse=True`.
1186
1205
  recurse: Whether to recurse into the subgraphs.
1187
1206
  If `False`, only the immediate subgraphs will be returned.
1188
1207
 
@@ -1641,7 +1660,9 @@ class Pregel(
1641
1660
  if any(len(u) == 0 for u in supersteps):
1642
1661
  raise ValueError("No updates provided")
1643
1662
 
1644
- if subgraph := self._subgraph_for_namespace(config, checkpointer):
1663
+ if subgraph := self._subgraph_for_namespace(
1664
+ config, checkpointer, for_update=True
1665
+ ):
1645
1666
  pregel, subgraph_config = subgraph
1646
1667
  return pregel.bulk_update_state(subgraph_config, supersteps)
1647
1668
 
@@ -2142,7 +2163,9 @@ class Pregel(
2142
2163
  if any(len(u) == 0 for u in supersteps):
2143
2164
  raise ValueError("No updates provided")
2144
2165
 
2145
- if subgraph := self._subgraph_for_namespace(config, checkpointer):
2166
+ if subgraph := self._subgraph_for_namespace(
2167
+ config, checkpointer, for_update=True
2168
+ ):
2146
2169
  pregel, subgraph_config = subgraph
2147
2170
  return await pregel.abulk_update_state(subgraph_config, supersteps)
2148
2171
 
@@ -2879,16 +2902,17 @@ class Pregel(
2879
2902
  if CONFIG_KEY_TASK_ID not in config[CONF]:
2880
2903
  # Not run as a task (a child invoked with its own thread, say),
2881
2904
  # this graph's namespace starts at the root, so a caller's
2882
- # handler would take this graph's events for its own graph's.
2905
+ # handler without `subgraphs` would take this graph's events
2906
+ # for its own graph's. One with `subgraphs` asked for them.
2883
2907
  callback_manager.handlers = [
2884
2908
  h
2885
2909
  for h in callback_manager.handlers
2886
- if not _is_stream_mode_handler(h)
2910
+ if not _streams_only_its_graph(h)
2887
2911
  ]
2888
2912
  callback_manager.inheritable_handlers = [
2889
2913
  h
2890
2914
  for h in callback_manager.inheritable_handlers
2891
- if not _is_stream_mode_handler(h)
2915
+ if not _streams_only_its_graph(h)
2892
2916
  ]
2893
2917
  if "ls_integration" not in callback_manager.metadata:
2894
2918
  callback_manager.add_metadata({"ls_integration": "langgraph"})
@@ -3296,16 +3320,17 @@ class Pregel(
3296
3320
  if CONFIG_KEY_TASK_ID not in config[CONF]:
3297
3321
  # Not run as a task (a child invoked with its own thread, say),
3298
3322
  # this graph's namespace starts at the root, so a caller's
3299
- # handler would take this graph's events for its own graph's.
3323
+ # handler without `subgraphs` would take this graph's events
3324
+ # for its own graph's. One with `subgraphs` asked for them.
3300
3325
  callback_manager.handlers = [
3301
3326
  h
3302
3327
  for h in callback_manager.handlers
3303
- if not _is_stream_mode_handler(h)
3328
+ if not _streams_only_its_graph(h)
3304
3329
  ]
3305
3330
  callback_manager.inheritable_handlers = [
3306
3331
  h
3307
3332
  for h in callback_manager.inheritable_handlers
3308
- if not _is_stream_mode_handler(h)
3333
+ if not _streams_only_its_graph(h)
3309
3334
  ]
3310
3335
  if "ls_integration" not in callback_manager.metadata:
3311
3336
  callback_manager.add_metadata({"ls_integration": "langgraph"})
@@ -4457,8 +4482,11 @@ def _coerce_checkpoint_values(payload: Any, mapper: Callable[[Any], Any]) -> Non
4457
4482
  payload["values"] = mapper(payload["values"])
4458
4483
 
4459
4484
 
4460
- def _is_stream_mode_handler(handler: object) -> bool:
4461
- return isinstance(handler, (StreamMessagesHandler, StreamToolCallHandler))
4485
+ def _streams_only_its_graph(handler: object) -> bool:
4486
+ return (
4487
+ isinstance(handler, (StreamMessagesHandler, StreamToolCallHandler))
4488
+ and not handler.subgraphs
4489
+ )
4462
4490
 
4463
4491
 
4464
4492
  def _resolve_parent_ns(
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "langgraph"
7
- version = "1.2.20"
7
+ version = "1.2.22"
8
8
  description = "Building stateful, multi-actor applications with LLMs"
9
9
  authors = []
10
10
  requires-python = ">=3.10"
@@ -5,6 +5,7 @@ import langchain_core
5
5
  import pytest
6
6
  from langchain_core.messages import (
7
7
  AIMessage,
8
+ AIMessageChunk,
8
9
  AnyMessage,
9
10
  HumanMessage,
10
11
  RemoveMessage,
@@ -168,6 +169,41 @@ def test_non_list_inputs():
168
169
  assert result == expected_result
169
170
 
170
171
 
172
+ def test_message_likes_and_chunks_are_converted_on_both_sides():
173
+ left = [
174
+ HumanMessage(content="Hello", id="1"),
175
+ {"role": "assistant", "content": "Hi", "id": "2"},
176
+ ]
177
+ right = [AIMessageChunk(content="chunk", id="3"), ("user", "Thanks")]
178
+ result = add_messages(left, right)
179
+ assert [type(m) for m in result] == [
180
+ HumanMessage,
181
+ AIMessage,
182
+ AIMessage,
183
+ HumanMessage,
184
+ ]
185
+ assert result[:3] == [
186
+ HumanMessage(content="Hello", id="1"),
187
+ AIMessage(content="Hi", id="2"),
188
+ AIMessage(content="chunk", id="3"),
189
+ ]
190
+ assert result[3].content == "Thanks"
191
+
192
+
193
+ def test_chunk_only_lists_are_converted():
194
+ left = [AIMessageChunk(content="a", id="1")]
195
+ right = [AIMessageChunk(content="b", id="2")]
196
+ result = add_messages(left, right)
197
+ assert [type(m) for m in result] == [AIMessage, AIMessage]
198
+
199
+
200
+ def test_result_is_a_new_list():
201
+ left = [HumanMessage(content="Hello", id="1")]
202
+ result = add_messages(left, [])
203
+ assert result == left
204
+ assert result is not left
205
+
206
+
171
207
  def test_delete_all():
172
208
  left = [
173
209
  HumanMessage(content="Hello", id="1"),
@@ -0,0 +1,50 @@
1
+ import concurrent.futures
2
+ import threading
3
+ import weakref
4
+ from functools import partial
5
+
6
+ from langgraph.pregel._runner import FuturesDict, _should_stop_others
7
+
8
+
9
+ def _on_done(task: object, exc: BaseException | None) -> None:
10
+ pass
11
+
12
+
13
+ def _futures(handled_exception_ids: set[int]) -> FuturesDict:
14
+ return FuturesDict(
15
+ event=threading.Event(),
16
+ callback=weakref.ref(_on_done),
17
+ should_stop=partial(
18
+ _should_stop_others, handled_exception_ids=handled_exception_ids
19
+ ),
20
+ future_type=concurrent.futures.Future,
21
+ )
22
+
23
+
24
+ def test_failed_task_wakes_waiter_before_others_finish() -> None:
25
+ futures = _futures(set())
26
+ ok, bad, pending = (concurrent.futures.Future() for _ in range(3))
27
+ for fut in (ok, bad, pending):
28
+ futures[fut] = object()
29
+
30
+ ok.set_result(None)
31
+ assert not futures.event.is_set()
32
+
33
+ bad.set_exception(ValueError("boom"))
34
+ assert futures.event.is_set()
35
+
36
+
37
+ def test_handled_failure_waits_for_all_tasks() -> None:
38
+ handled: set[int] = set()
39
+ futures = _futures(handled)
40
+ bad, pending = (concurrent.futures.Future() for _ in range(2))
41
+ for fut in (bad, pending):
42
+ futures[fut] = object()
43
+
44
+ exc = ValueError("handled by an error handler")
45
+ handled.add(id(exc))
46
+ bad.set_exception(exc)
47
+ assert not futures.event.is_set()
48
+
49
+ pending.set_result(None)
50
+ assert futures.event.is_set()
@@ -73,6 +73,15 @@ _CHILD_CONFIGS = pytest.mark.parametrize(
73
73
  ids=["no_config", "own_thread", "caller_thread"],
74
74
  )
75
75
 
76
+ _OWN_THREAD_CONFIGS = pytest.mark.parametrize(
77
+ "child_config",
78
+ [
79
+ {"configurable": {"thread_id": "child"}},
80
+ {"configurable": {"thread_id": "parent"}},
81
+ ],
82
+ ids=["own_thread", "caller_thread"],
83
+ )
84
+
76
85
 
77
86
  _TASK_CALLERS = pytest.mark.parametrize(
78
87
  "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(
202
211
  assert len(received) == len(_CHAT_AND_ECHO_EVENTS)
203
212
 
204
213
 
205
- @pytest.mark.parametrize(
206
- "child_config",
207
- [
208
- {"configurable": {"thread_id": "child"}},
209
- {"configurable": {"thread_id": "parent"}},
210
- ],
211
- ids=["own_thread", "caller_thread"],
212
- )
213
- def test_child_graph_on_its_own_thread_skips_a_caller_streaming_subgraphs(
214
+ @_OWN_THREAD_CONFIGS
215
+ def test_child_graph_on_its_own_thread_streams_to_a_caller_streaming_subgraphs(
214
216
  child_config: dict,
215
217
  ) -> None:
216
218
  child = _one_node_graph(_chat_and_echo)
@@ -224,9 +226,32 @@ def test_child_graph_on_its_own_thread_skips_a_caller_streaming_subgraphs(
224
226
  {"configurable": {"thread_id": "parent"}},
225
227
  stream_mode=["messages", "tools"],
226
228
  subgraphs=True,
229
+ version="v2",
227
230
  )
228
231
 
229
- assert list(parts) == []
232
+ assert [_summary(part) for part in parts] == _CHAT_AND_ECHO_EVENTS
233
+
234
+
235
+ @NEEDS_CONTEXTVARS
236
+ @_OWN_THREAD_CONFIGS
237
+ async def test_child_graph_on_its_own_thread_streams_to_a_caller_streaming_subgraphs_async(
238
+ child_config: dict,
239
+ ) -> None:
240
+ child = _one_node_graph(_achat_and_echo)
241
+
242
+ async def node(state: MessagesState) -> dict:
243
+ await child.ainvoke({"messages": []}, child_config)
244
+ return {}
245
+
246
+ parts = _one_node_graph(node).astream(
247
+ {"messages": []},
248
+ {"configurable": {"thread_id": "parent"}},
249
+ stream_mode=["messages", "tools"],
250
+ subgraphs=True,
251
+ version="v2",
252
+ )
253
+
254
+ assert [_summary(part) async for part in parts] == _CHAT_AND_ECHO_EVENTS
230
255
 
231
256
 
232
257
  def test_node_gets_the_events_of_every_subgraph_it_streams() -> None: