Dataclass coercion #318

Open
opened 2026-02-20 17:36:20 -05:00 by yindo · 2 comments
Owner

Originally created by @eyurtsev on GitHub (Nov 25, 2024).

Privileged issue

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

Issue Content


from dataclasses import dataclass
from typing import List

@dataclass
class Foo:
    """Represents an item with an index."""
    index: int

@dataclass
class State:
    foos: List[Foo]

def map_to_node(state: State):
    """Prepare all items in the state for processing."""
    return [describe_foo(foo) for foo in state.foos]

def describe_foo(foo: Foo): # <-- foo is a DICT here instead of a dataclass
    """Raise a TypeError if foo is not a dataclass instance."""
    raise TypeError(f"Expected Foo, got {type(foo).__name__}")  # Assume foo is a dict

System Information

OS: Linux
OS Version: #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2
Python Version: 3.11.4 (main, Sep 25 2023, 10:06:23) [GCC 11.4.0]

Package Information

langchain_core: 0.3.19
langsmith: 0.1.144
langchain_openai: 0.2.9
langgraph: 0.2.53

Optional packages not installed

langserve

Other Dependencies

httpx: 0.27.2
jsonpatch: 1.33
langgraph-checkpoint: 2.0.5
langgraph-sdk: 0.1.36
openai: 1.55.0
orjson: 3.10.11
packaging: 24.2
pydantic: 2.10.0
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
tenacity: 9.0.0
tiktoken: 0.8.0
typing-extensions: 4.12.2

Originally created by @eyurtsev on GitHub (Nov 25, 2024). ### Privileged issue - [X] I am a LangChain maintainer, or was asked directly by a LangChain maintainer to create an issue here. ### Issue Content ```python from dataclasses import dataclass from typing import List @dataclass class Foo: """Represents an item with an index.""" index: int @dataclass class State: foos: List[Foo] def map_to_node(state: State): """Prepare all items in the state for processing.""" return [describe_foo(foo) for foo in state.foos] def describe_foo(foo: Foo): # <-- foo is a DICT here instead of a dataclass """Raise a TypeError if foo is not a dataclass instance.""" raise TypeError(f"Expected Foo, got {type(foo).__name__}") # Assume foo is a dict ``` --- System Information ------------------ > OS: Linux > OS Version: #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 > Python Version: 3.11.4 (main, Sep 25 2023, 10:06:23) [GCC 11.4.0] Package Information ------------------- > langchain_core: 0.3.19 > langsmith: 0.1.144 > langchain_openai: 0.2.9 > langgraph: 0.2.53 Optional packages not installed ------------------------------- > langserve Other Dependencies ------------------ > httpx: 0.27.2 > jsonpatch: 1.33 > langgraph-checkpoint: 2.0.5 > langgraph-sdk: 0.1.36 > openai: 1.55.0 > orjson: 3.10.11 > packaging: 24.2 > pydantic: 2.10.0 > PyYAML: 6.0.2 > requests: 2.32.3 > requests-toolbelt: 1.0.0 > tenacity: 9.0.0 > tiktoken: 0.8.0 > typing-extensions: 4.12.2
yindo added the change label 2026-02-20 17:36:20 -05:00
Author
Owner

@Barbacamanitu commented on GitHub (Sep 12, 2025):

Any movement on this? I deal with this issue every day.

@Barbacamanitu commented on GitHub (Sep 12, 2025): Any movement on this? I deal with this issue every day.
Author
Owner

@AnhQuanTrl commented on GitHub (Sep 29, 2025):

Can this issue be fixed? Because of this, using data class for state is really dangerous because anyone without awareness of this issue might create nested data class to hold the state of the graph -> which leads to all sorts of possible problems.

@AnhQuanTrl commented on GitHub (Sep 29, 2025): Can this issue be fixed? Because of this, using data class for state is really dangerous because anyone without awareness of this issue might create nested data class to hold the state of the graph -> which leads to all sorts of possible problems.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#318