[GH-ISSUE #572] [langgraph]: Known limitations with Pydantic BaseModel as State #95

Closed
opened 2026-02-17 17:19:10 -05:00 by yindo · 1 comment
Owner

Originally created by @sundaraa-deshaw on GitHub (Sep 18, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/572

Type of issue

issue / bug

Language

Python

Description

The documentation for using Pydantic model for Graph State states that:

Run-time validation only occurs on inputs into nodes, not on the outputs.

However, it seems that the validation occurs only for the input to the first node but not to subsequent nodes. e.g.

from pydantic import BaseModel

class PydanticState(BaseModel):
    messages: list[AnyMessage] = []
    state_1: str = ""
    state_2: str = ""
    source_documents: list[Document] = []

def node_1(state: PydanticState):
    return {
        "state_1": 1,
        "messages": "hi"
    }

def node_2(state: PydanticState):
    print(state)
    print(PydanticState.model_validate(state))
    return {}

pydantic_graph = (
    StateGraph(PydanticState)
    .add_node(node_1)
    .add_node(node_2)
    .add_edge(START, "node_1")
    .add_edge("node_1", "node_2")
    .add_edge("node_2", END)
    .compile()
)

pydantic_graph.invoke({})

produces:

messages='hi' state_1=1 state_2='' source_documents=[]
messages='hi' state_1=1 state_2='' source_documents=[]
{'messages': 'hi', 'state_1': 1, 'state_2': 'from node_2'}

Expected behavior is for Pydantic to throw the validation for messages and state_1

Originally created by @sundaraa-deshaw on GitHub (Sep 18, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/572 ### Type of issue issue / bug ### Language Python ### Description The [documentation for using Pydantic model](https://langchain-ai.github.io/langgraph/how-tos/graph-api/#use-pydantic-models-for-graph-state) for Graph State states that: > Run-time validation only occurs on inputs into nodes, not on the outputs. However, it seems that the validation occurs only for the input to the first node but not to subsequent nodes. e.g. ``` from pydantic import BaseModel class PydanticState(BaseModel): messages: list[AnyMessage] = [] state_1: str = "" state_2: str = "" source_documents: list[Document] = [] def node_1(state: PydanticState): return { "state_1": 1, "messages": "hi" } def node_2(state: PydanticState): print(state) print(PydanticState.model_validate(state)) return {} pydantic_graph = ( StateGraph(PydanticState) .add_node(node_1) .add_node(node_2) .add_edge(START, "node_1") .add_edge("node_1", "node_2") .add_edge("node_2", END) .compile() ) pydantic_graph.invoke({}) ``` produces: ``` messages='hi' state_1=1 state_2='' source_documents=[] messages='hi' state_1=1 state_2='' source_documents=[] {'messages': 'hi', 'state_1': 1, 'state_2': 'from node_2'} ``` Expected behavior is for Pydantic to throw the validation for `messages` and `state_1`
yindo added the langgraph label 2026-02-17 17:19:10 -05:00
yindo closed this issue 2026-02-17 17:19:10 -05:00
Author
Owner

@mdrxy commented on GitHub (Oct 31, 2025):

Fixing

@mdrxy commented on GitHub (Oct 31, 2025): Fixing
yindo changed title from [langgraph]: Known limitations with Pydantic BaseModel as State to [GH-ISSUE #572] [langgraph]: Known limitations with Pydantic BaseModel as State 2026-06-05 17:25:02 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#95