mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-11-20 04:41:02 +01:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7830b91a24 | ||
|
|
a03c71268e | ||
|
|
292106d223 |
No files matched your search
@@ -37,6 +37,10 @@ CONFIG_KEY_SEND = sys.intern("__pregel_send")
|
||||
# holds the `write` function that accepts writes to state/edges/reserved keys
|
||||
CONFIG_KEY_READ = sys.intern("__pregel_read")
|
||||
# holds the `read` function that returns a copy of the current state
|
||||
CONFIG_KEY_READ_STATE = sys.intern("__pregel_read_state")
|
||||
# holds a function that returns the state as the task's node gets it from an edge,
|
||||
# set on Send tasks, whose input is the Send arg instead. Other tasks set it to None
|
||||
# so a graph running inside a Send task never reads its parent's state
|
||||
CONFIG_KEY_CALL = sys.intern("__pregel_call")
|
||||
# holds the `call` function that accepts a node/func, args and returns a future
|
||||
CONFIG_KEY_CHECKPOINTER = sys.intern("__pregel_checkpointer")
|
||||
|
||||
@@ -42,6 +42,7 @@ from langgraph._internal._constants import (
|
||||
CONFIG_KEY_CHECKPOINTER,
|
||||
CONFIG_KEY_NODE_ERROR,
|
||||
CONFIG_KEY_READ,
|
||||
CONFIG_KEY_READ_STATE,
|
||||
CONFIG_KEY_RESUME_MAP,
|
||||
CONFIG_KEY_RUNTIME,
|
||||
CONFIG_KEY_SCRATCHPAD,
|
||||
@@ -734,6 +735,7 @@ def prepare_single_task(
|
||||
triggers,
|
||||
),
|
||||
),
|
||||
CONFIG_KEY_READ_STATE: None,
|
||||
CONFIG_KEY_CHECKPOINTER: (
|
||||
checkpointer
|
||||
or configurable.get(CONFIG_KEY_CHECKPOINTER)
|
||||
@@ -911,6 +913,7 @@ def prepare_push_task_functional(
|
||||
managed,
|
||||
PregelTaskWrites(in_progress_task_path, name, writes, triggers),
|
||||
),
|
||||
CONFIG_KEY_READ_STATE: None,
|
||||
CONFIG_KEY_CHECKPOINTER: (
|
||||
checkpointer or configurable.get(CONFIG_KEY_CHECKPOINTER)
|
||||
),
|
||||
@@ -1081,6 +1084,15 @@ def prepare_push_task_send(
|
||||
translated_task_path, packet.node, writes, triggers
|
||||
),
|
||||
),
|
||||
CONFIG_KEY_READ_STATE: partial(
|
||||
_proc_input,
|
||||
proc,
|
||||
managed,
|
||||
channels,
|
||||
for_execution=True,
|
||||
scratchpad=scratchpad,
|
||||
input_cache=None,
|
||||
),
|
||||
CONFIG_KEY_CHECKPOINTER: (
|
||||
checkpointer or configurable.get(CONFIG_KEY_CHECKPOINTER)
|
||||
),
|
||||
@@ -1222,6 +1234,7 @@ def prepare_node_error_handler_task(
|
||||
PUSH_TRIGGER,
|
||||
),
|
||||
),
|
||||
CONFIG_KEY_READ_STATE: None,
|
||||
CONFIG_KEY_CHECKPOINTER: (
|
||||
checkpointer or configurable.get(CONFIG_KEY_CHECKPOINTER)
|
||||
),
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph"
|
||||
version = "1.2.22"
|
||||
version = "1.2.23"
|
||||
description = "Building stateful, multi-actor applications with LLMs"
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
@@ -27,7 +27,7 @@ dependencies = [
|
||||
"langchain-core>=1.4.7,<2",
|
||||
"langgraph-checkpoint>=4.3.0,<5.0.0",
|
||||
"langgraph-sdk>=0.4.6,<0.5.0",
|
||||
"langgraph-prebuilt>=1.1.0,<1.2.0",
|
||||
"langgraph-prebuilt>=1.1.3,<1.2.0",
|
||||
"xxhash>=3.5.0",
|
||||
"pydantic>=2.7.4",
|
||||
]
|
||||
|
||||
Generated
+2
-2
@@ -1440,7 +1440,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.22"
|
||||
version = "1.2.23"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -1827,7 +1827,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.2"
|
||||
version = "1.1.3"
|
||||
source = { editable = "../prebuilt" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
@@ -107,6 +107,12 @@ except ImportError: # `langgraph` before `is_invalid_resume` never marks resume
|
||||
return False
|
||||
|
||||
|
||||
try:
|
||||
from langgraph._internal._constants import CONFIG_KEY_READ_STATE
|
||||
except ImportError: # `langgraph` before `CONFIG_KEY_READ_STATE` never sets the key
|
||||
CONFIG_KEY_READ_STATE = "__pregel_read_state"
|
||||
|
||||
|
||||
# right now we use a dict as the default, can change this to AgentState, but depends
|
||||
# on if this lives in LangChain or LangGraph... ideally would have some typed
|
||||
# messages key
|
||||
@@ -1332,7 +1338,8 @@ class ToolNode(RunnableCallable):
|
||||
- `ToolCallWithContext` dict — legacy Send payload carrying an inlined
|
||||
state snapshot; return `input["state"]`.
|
||||
- list of `ToolCall` dicts — new Send payload with no inlined state;
|
||||
hydrate state from channels via `CONFIG_KEY_READ`.
|
||||
read the state this node would get from an edge, coerced to its
|
||||
input schema (raw channel values on older `langgraph`).
|
||||
- regular graph state (dict/list/BaseModel) — return `input` as-is.
|
||||
"""
|
||||
if isinstance(input, dict) and input.get("__type") == "tool_call_with_context":
|
||||
@@ -1343,7 +1350,10 @@ class ToolNode(RunnableCallable):
|
||||
and isinstance(input[-1], dict)
|
||||
and input[-1].get("type") == "tool_call"
|
||||
):
|
||||
read = config.get(CONF, {}).get(CONFIG_KEY_READ)
|
||||
conf = config.get(CONF, {})
|
||||
if (read_state := conf.get(CONFIG_KEY_READ_STATE)) is not None:
|
||||
return read_state()
|
||||
read = conf.get(CONFIG_KEY_READ)
|
||||
if read is None:
|
||||
return {}
|
||||
# Pregel installs CONFIG_KEY_READ as
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.2"
|
||||
version = "1.1.3"
|
||||
description = "Library with high-level APIs for creating and executing LangGraph agents and tools."
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
|
||||
@@ -3,6 +3,7 @@ import dataclasses
|
||||
import json
|
||||
import sys
|
||||
import warnings
|
||||
from collections.abc import Callable
|
||||
from functools import partial
|
||||
from typing import (
|
||||
Annotated,
|
||||
@@ -1573,6 +1574,66 @@ def test_tool_node_inject_state(schema_: type[T]) -> None:
|
||||
assert tool_message.content == "hi?"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("schema_", _INJECTED_STATE_SCHEMAS)
|
||||
def test_tool_node_send_tool_call_gets_same_state_as_edge(schema_: type[T]) -> None:
|
||||
call = ToolCall(name="record", args={}, id="call_1", type="tool_call")
|
||||
seen: list[tuple[type, type]] = []
|
||||
|
||||
def record(runtime: ToolRuntime, state: Annotated[T, InjectedState]) -> str:
|
||||
"""Record the state types the tool gets."""
|
||||
seen.append((type(runtime.state), type(state)))
|
||||
return "ok"
|
||||
|
||||
def agent(state: Any) -> dict:
|
||||
return {"messages": [AIMessage("", tool_calls=[call])]}
|
||||
|
||||
def state_types(route: Callable[[Any], str | list[Send]]) -> tuple[type, type]:
|
||||
seen.clear()
|
||||
graph = (
|
||||
StateGraph(schema_)
|
||||
.add_node("agent", agent)
|
||||
.add_node("tools", ToolNode([record]))
|
||||
.add_edge(START, "agent")
|
||||
.add_conditional_edges("agent", route, ["tools"])
|
||||
.compile()
|
||||
)
|
||||
graph.invoke({"messages": [], "foo": "bar"})
|
||||
return seen[0]
|
||||
|
||||
by_send = state_types(lambda _: [Send("tools", [call])])
|
||||
by_edge = state_types(lambda _: "tools")
|
||||
assert by_send == by_edge
|
||||
|
||||
|
||||
def test_tool_node_in_graph_run_by_send_gets_that_graphs_state() -> None:
|
||||
call = ToolCall(name="record", args={}, id="call_1", type="tool_call")
|
||||
seen: list[Any] = []
|
||||
|
||||
def record(runtime: ToolRuntime) -> str:
|
||||
"""Record the state the tool gets."""
|
||||
seen.append(runtime.state)
|
||||
return "ok"
|
||||
|
||||
class State(TypedDict):
|
||||
value: str
|
||||
|
||||
def call_tools(state: State) -> dict:
|
||||
ToolNode([record]).invoke([call])
|
||||
return {}
|
||||
|
||||
inner = StateGraph(State).add_node(call_tools).add_edge(START, "call_tools")
|
||||
outer = (
|
||||
StateGraph(State)
|
||||
.add_node("inner", inner.compile())
|
||||
.add_conditional_edges(
|
||||
START, lambda _: [Send("inner", {"value": "inner"})], ["inner"]
|
||||
)
|
||||
.compile()
|
||||
)
|
||||
outer.invoke({"value": "outer"})
|
||||
assert seen == [{"value": "inner"}]
|
||||
|
||||
|
||||
def test_tool_node_inject_store() -> None:
|
||||
store = InMemoryStore()
|
||||
namespace = ("test",)
|
||||
|
||||
Generated
+2
-2
@@ -381,7 +381,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.22"
|
||||
version = "1.2.23"
|
||||
source = { editable = "../langgraph" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -607,7 +607,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.2"
|
||||
version = "1.1.3"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
Generated
+2
-2
@@ -394,7 +394,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.22"
|
||||
version = "1.2.23"
|
||||
source = { editable = "../langgraph" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
@@ -526,7 +526,7 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "1.1.2"
|
||||
version = "1.1.3"
|
||||
source = { editable = "../prebuilt" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
Reference in new issue
Block a user