Skip to content

Using Pydantic state with aliased fields #2555

@eyurtsev

Description

@eyurtsev

Privileged issue

  • I am a LangChain maintainer, or was asked directly by a LangChain maintainer to create an issue here.

Issue Content

MRE

from pydantic import BaseModel, Field
from langgraph.graph import StateGraph

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

# Define a new graph
graph_builder = StateGraph(
    Foo
)


def node(foo: Foo):
    print(foo)
    return None

# Add the node to the graph
graph_builder.add_node("node", node)
# Set the entrypoint as `call_model`
graph_builder.add_edge("__start__", "node")

# Compile the workflow into an executable graph
graph = graph_builder.compile()


graph.invoke({'bar': 'hello'})

Expected behavior

The alias should be respected and the field foo should be only updateable by alias.

Observed behavior

Validation error.

Context

  • Mostly a problem for IO boundaries (since that's where I actually use pydantic models)
  • alias is needed in some cases to avoid name collisions (e.g., schema field is already taken by pydantic)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions