[PR #6634] feat(langgraph): support Pydantic state with aliased fields #5182

Open
opened 2026-02-20 17:51:20 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/6634

State: open
Merged: No


Summary

Adds support for Pydantic BaseModel state schemas with aliased fields (Field(alias=...)).

Problem

When using Pydantic models with aliased fields, users couldn't provide input using alias keys:

class State(BaseModel):
    foo: str = Field(alias='bar')

graph = StateGraph(State).compile()
graph.invoke({'bar': 'hello'})  # Failed - alias not recognized

The issue was that channels are named by field names (e.g., foo), but input came with alias keys (e.g., bar), so map_input couldn't match them.

Solution

  1. Extract alias-to-field-name mappings from Pydantic schemas via _get_pydantic_alias_map()
  2. Translate input alias keys to field names in the START node's _get_updates() closure
  3. Translate field names back to aliases when instantiating Pydantic models via _coerce_state() (since Pydantic expects aliases for aliased fields)

Changes

  • langgraph/graph/state.py

    • Added _get_pydantic_alias_map() helper function
    • Modified attach_node() to translate alias keys for START nodes
    • Modified _coerce_state() to translate field names to aliases for Pydantic instantiation
  • langgraph/pregel/_io.py

    • Extended map_input() with optional alias_map parameter
  • langgraph/pregel/main.py and langgraph/pregel/_loop.py

    • Added input_alias_map attribute for future extensibility

Test plan

  • test_pydantic_aliased_fields - Basic aliased field test
  • test_pydantic_multiple_aliased_fields - Multiple aliased fields (camelCase)
  • test_pydantic_mixed_alias_and_fieldname - Mix of aliased and non-aliased fields
  • test_pydantic_alias_with_reducer - Aliased fields with reducer annotations
  • test_pydantic_aliased_fields_multinode_with_checkpointer - Integration test with multi-node graph, checkpointing, and interrupt/resume
  • test_pydantic_aliased_fields_async - Async graph execution
  • test_get_pydantic_alias_map - Unit test for alias extraction
TEST="tests/test_pydantic.py tests/test_state.py::test_get_pydantic_alias_map -v" make test
# Result: 10 passed

TEST="tests/test_pregel.py -v -k 'not stress'" make test
# Result: 420 passed

Fixes #2555

**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6634 **State:** open **Merged:** No --- ## Summary Adds support for Pydantic `BaseModel` state schemas with aliased fields (`Field(alias=...)`). ### Problem When using Pydantic models with aliased fields, users couldn't provide input using alias keys: ```python class State(BaseModel): foo: str = Field(alias='bar') graph = StateGraph(State).compile() graph.invoke({'bar': 'hello'}) # Failed - alias not recognized ``` The issue was that channels are named by field names (e.g., `foo`), but input came with alias keys (e.g., `bar`), so `map_input` couldn't match them. ### Solution 1. Extract alias-to-field-name mappings from Pydantic schemas via `_get_pydantic_alias_map()` 2. Translate input alias keys to field names in the START node's `_get_updates()` closure 3. Translate field names back to aliases when instantiating Pydantic models via `_coerce_state()` (since Pydantic expects aliases for aliased fields) ### Changes - **`langgraph/graph/state.py`** - Added `_get_pydantic_alias_map()` helper function - Modified `attach_node()` to translate alias keys for START nodes - Modified `_coerce_state()` to translate field names to aliases for Pydantic instantiation - **`langgraph/pregel/_io.py`** - Extended `map_input()` with optional `alias_map` parameter - **`langgraph/pregel/main.py`** and **`langgraph/pregel/_loop.py`** - Added `input_alias_map` attribute for future extensibility ## Test plan - [x] `test_pydantic_aliased_fields` - Basic aliased field test - [x] `test_pydantic_multiple_aliased_fields` - Multiple aliased fields (camelCase) - [x] `test_pydantic_mixed_alias_and_fieldname` - Mix of aliased and non-aliased fields - [x] `test_pydantic_alias_with_reducer` - Aliased fields with reducer annotations - [x] `test_pydantic_aliased_fields_multinode_with_checkpointer` - Integration test with multi-node graph, checkpointing, and interrupt/resume - [x] `test_pydantic_aliased_fields_async` - Async graph execution - [x] `test_get_pydantic_alias_map` - Unit test for alias extraction ```bash TEST="tests/test_pydantic.py tests/test_state.py::test_get_pydantic_alias_map -v" make test # Result: 10 passed TEST="tests/test_pregel.py -v -k 'not stress'" make test # Result: 420 passed ``` Fixes #2555
yindo added the pull-request label 2026-02-20 17:51:20 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#5182