Require state_schema in StateGraph.__init__ (#4897)

This commit is contained in:
Sydney Runkle
2025-06-02 01:52:35 +00:00
committed by GitHub
parent 21f762140a
commit eaa1b37645
2 changed files with 12 additions and 19 deletions
+6 -2
View File
@@ -21,6 +21,8 @@ def fanout_to_subgraph() -> StateGraph:
class JokeOutput(TypedDict):
jokes: list[str]
class JokeState(JokeInput, JokeOutput): ...
async def bump(state: JokeOutput):
return {"jokes": [state["jokes"][0] + " a"]}
@@ -35,7 +37,7 @@ def fanout_to_subgraph() -> StateGraph:
return END if state["jokes"][0].endswith(" a" * 10) else "bump"
# subgraph
subgraph = StateGraph(input=JokeInput, output=JokeOutput)
subgraph = StateGraph(JokeState, input=JokeInput, output=JokeOutput)
subgraph.add_node("edit", edit)
subgraph.add_node("generate", generate)
subgraph.add_node("bump", bump)
@@ -69,6 +71,8 @@ def fanout_to_subgraph_sync() -> StateGraph:
class JokeOutput(TypedDict):
jokes: list[str]
class JokeState(JokeInput, JokeOutput): ...
def bump(state: JokeOutput):
return {"jokes": [state["jokes"][0] + " a"]}
@@ -83,7 +87,7 @@ def fanout_to_subgraph_sync() -> StateGraph:
return END if state["jokes"][0].endswith(" a" * 10) else "bump"
# subgraph
subgraph = StateGraph(input=JokeInput, output=JokeOutput)
subgraph = StateGraph(JokeState, input=JokeInput, output=JokeOutput)
subgraph.add_node("edit", edit)
subgraph.add_node("generate", generate)
subgraph.add_node("bump", bump)
+6 -17
View File
@@ -25,7 +25,6 @@ from langchain_core.runnables import Runnable, RunnableConfig
from pydantic import BaseModel
from typing_extensions import Self
from langgraph._api.deprecation import LangGraphDeprecationWarning
from langgraph.cache.base import BaseCache
from langgraph.channels.base import BaseChannel
from langgraph.channels.binop import BinaryOperatorAggregate
@@ -170,27 +169,17 @@ class StateGraph:
def __init__(
self,
state_schema: Optional[type[Any]] = None,
state_schema: type[Any],
config_schema: Optional[type[Any]] = None,
*,
input: Optional[type[Any]] = None,
output: Optional[type[Any]] = None,
) -> None:
if state_schema is None:
if input is None or output is None:
raise ValueError("Must provide state_schema or input and output")
state_schema = input
warnings.warn(
"Initializing StateGraph without state_schema is deprecated. "
"Please pass in an explicit state_schema instead of just an input and output schema.",
LangGraphDeprecationWarning,
stacklevel=2,
)
else:
if input is None:
input = state_schema
if output is None:
output = state_schema
if input is None:
input = state_schema
if output is None:
output = state_schema
self.nodes = {}
self.edges = set[tuple[str, str]]()
self.branches = defaultdict(dict)