0
0
LangChainframework~10 mins

State schema definition in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - State schema definition
Define schema fields
Set field types and defaults
Create schema object
Use schema to validate or store state
Access or update state via schema
This flow shows how to define a state schema by specifying fields and types, creating the schema object, and then using it to manage state.
Execution Sample
LangChain
from langchain.schema import State

state = State(
  user_name=str,
  count=int,
  active=bool
)

state.user_name = "Alice"
Defines a state schema with fields user_name, count, and active, then sets user_name to 'Alice'.
Execution Table
StepActionField/VariableValue/TypeResult/State
1Define State schemauser_namestrSchema created with user_name:str
2Define State schemacountintSchema created with count:int
3Define State schemaactiveboolSchema created with active:bool
4Create State instancestateState objectstate with fields user_name, count, active
5Assign valuestate.user_name"Alice"state.user_name set to 'Alice'
6Access valuestate.user_name"Alice"Returns 'Alice'
7Assign valuestate.count5state.count set to 5
8Assign valuestate.activeTruestate.active set to True
9Access valuestate.count5Returns 5
10Access valuestate.activeTrueReturns True
11Assign wrong typestate.count"five"Error or type mismatch
12End--State schema enforces types; invalid assignment rejected
💡 Execution stops because the schema enforces types and rejects invalid assignments.
Variable Tracker
VariableStartAfter 5After 7After 8Final
state.user_nameundefined"Alice""Alice""Alice""Alice"
state.countundefinedundefined555
state.activeundefinedundefinedundefinedTrueTrue
Key Moments - 2 Insights
Why does assigning a string to state.count cause an error?
Because the schema defines count as an int type, assigning a string violates the type rule, as shown in step 11 of the execution_table.
Can we access state fields before assigning values?
No, fields start undefined until assigned, as seen in variable_tracker where initial values are undefined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what value is assigned to state.user_name?
Aundefined
B"Bob"
C"Alice"
D5
💡 Hint
Check the 'Value/Type' column at step 5 in execution_table.
At which step does the schema reject an invalid type assignment?
AStep 7
BStep 11
CStep 3
DStep 9
💡 Hint
Look for the step mentioning 'Error or type mismatch' in execution_table.
If we set state.active to False instead of True at step 8, what changes in variable_tracker?
Astate.active would be False after step 8
Bstate.active would remain undefined
Cstate.active would cause an error
Dstate.active would be True anyway
💡 Hint
Refer to variable_tracker row for state.active and consider assignment effects.
Concept Snapshot
State schema definition in langchain:
- Define fields with types inside State()
- Create a State object with these fields
- Assign values matching the declared types
- Access values via state.field_name
- Schema enforces type safety, rejects wrong types
Full Transcript
This visual execution trace shows how to define a state schema in langchain by specifying fields and their types. We create a State object with fields user_name as string, count as integer, and active as boolean. Assigning values to these fields updates the state. Accessing fields returns their current values. The schema enforces type safety, so assigning a wrong type like a string to count causes an error. The variable tracker shows how each field changes over time. Key moments clarify why type enforcement matters and when fields start undefined. The quiz tests understanding of assignments, errors, and state changes.