Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect state validation when there's generic types in pydantic.BaseModel state schema #4060

Open
4 tasks done
hon-gyu opened this issue Mar 27, 2025 · 5 comments
Open
4 tasks done

Comments

@hon-gyu
Copy link

hon-gyu commented Mar 27, 2025

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

from pydantic import BaseModel
from langgraph.graph import StateGraph, START, END

class A(BaseModel):
    a: str

class B(BaseModel):
    b: str

class C[AorB](BaseModel):
    c: AorB

class State(BaseModel):
    text: str
    count: int
    c: C[A]
    
def process_node(state: State):
    new_text = ", the type of c is " + str(type(state.c.c))
    print(state.c.c)

    return {"text": state.text + new_text, "count": state.count + 1}


builder = StateGraph(State)
builder.add_node("process", process_node)
builder.add_edge(START, "process")
builder.add_edge("process", END)
g = builder.compile()

input_state = {'text': '1', 'count': 0, 'c': {'c': {'a': '1'}}}

g.invoke(input_state)

# output
# {'a': '1'}
# .../pydantic/main.py:426: UserWarning: Pydantic serializer warnings:
#   Expected `A` but got `dict` with value `{'a': '1'}` - serialized value may not be as expected
#   return self.__pydantic_serializer__.to_python(

# {'text': "1, the type of c is <class 'dict'>",
#  'count': 1,
#  'c': {'c': {'a': '1'}}}

g.invoke(State.model_validate(input_state))
# output
# a='1'

Error Message and Stack Trace (if applicable)

pydantic/main.py:426: UserWarning: Pydantic serializer warnings:
  Expected `A` but got `dict` with value `{'a': '1'}` - serialized value may not be as expected
  return self.__pydantic_serializer__.to_python(

Description

When graph state schema has pydantic.BaseModel with generic types invovled. The validation does not perform correctly.

In the repro example provided, C instead of C[A] is used in validation, resulting in the type of state.c.c being dict instead of A.

Manual validation (g.invoke(State.model_validate(input_state))) gives the correct result.

The output of g.invoke(input_state) should be consistent with g.invoke(State.model_validate(input_state))

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:33:00 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6031
Python Version: 3.12.8 (main, Dec 19 2024, 14:22:58) [Clang 18.1.8 ]

Package Information

langchain_core: 0.3.48
langchain: 0.3.21
langchain_community: 0.3.20
langsmith: 0.3.19
langchain_anthropic: 0.3.10
langchain_google_vertexai: 2.0.17
langchain_neo4j: 0.4.0
langchain_openai: 0.3.10
langchain_text_splitters: 0.3.7
langgraph_api: 0.0.34
langgraph_cli: 0.1.80
langgraph_license: Installed. No version info available.
langgraph_sdk: 0.1.59
langgraph_storage: Installed. No version info available.

Optional packages not installed

langserve

Other Dependencies

aiohttp<4.0.0,>=3.8.3: Installed. No version info available.
anthropic<1,>=0.49.0: Installed. No version info available.
anthropic[vertexai]: Installed. No version info available.
async-timeout<5.0.0,>=4.0.0;: Installed. No version info available.
click: 8.1.8
cryptography: 43.0.3
dataclasses-json<0.7,>=0.5.7: Installed. No version info available.
google-cloud-aiplatform: 1.85.0
google-cloud-storage: 2.19.0
httpx: 0.28.1
httpx-sse: 0.4.0
httpx-sse<1.0.0,>=0.4.0: Installed. No version info available.
jsonpatch<2.0,>=1.33: Installed. No version info available.
jsonschema-rs: 0.29.1
langchain-anthropic;: Installed. No version info available.
langchain-aws;: Installed. No version info available.
langchain-azure-ai;: Installed. No version info available.
langchain-cohere;: Installed. No version info available.
langchain-community;: Installed. No version info available.
langchain-core<1.0.0,>=0.3.45: Installed. No version info available.
langchain-core<1.0.0,>=0.3.48: Installed. No version info available.
langchain-deepseek;: Installed. No version info available.
langchain-fireworks;: Installed. No version info available.
langchain-google-genai;: Installed. No version info available.
langchain-google-vertexai;: Installed. No version info available.
langchain-groq;: Installed. No version info available.
langchain-huggingface;: Installed. No version info available.
langchain-mistralai: Installed. No version info available.
langchain-mistralai;: Installed. No version info available.
langchain-ollama;: Installed. No version info available.
langchain-openai;: Installed. No version info available.
langchain-text-splitters<1.0.0,>=0.3.7: Installed. No version info available.
langchain-together;: Installed. No version info available.
langchain-xai;: Installed. No version info available.
langchain<1.0.0,>=0.3.21: Installed. No version info available.
langgraph: 0.3.20
langgraph-checkpoint: 2.0.23
langsmith-pyo3: Installed. No version info available.
langsmith<0.4,>=0.1.125: Installed. No version info available.
langsmith<0.4,>=0.1.17: Installed. No version info available.
neo4j: 5.28.1
neo4j-graphrag: 1.6.1
numpy<3,>=1.26.2: Installed. No version info available.
openai-agents: Installed. No version info available.
openai<2.0.0,>=1.68.2: Installed. No version info available.
opentelemetry-api: 1.31.1
opentelemetry-exporter-otlp-proto-http: 1.31.1
opentelemetry-sdk: 1.31.1
orjson: 3.10.16
packaging: 24.2
packaging<25,>=23.2: Installed. No version info available.
pydantic: 2.10.6
pydantic-settings<3.0.0,>=2.4.0: Installed. No version info available.
pydantic<3.0.0,>=2.5.2;: Installed. No version info available.
pydantic<3.0.0,>=2.7.4: Installed. No version info available.
pydantic<3.0.0,>=2.7.4;: Installed. No version info available.
pyjwt: 2.10.1
pytest: 8.3.5
python-dotenv: 1.1.0
PyYAML>=5.3: Installed. No version info available.
requests: 2.32.3
requests-toolbelt: 1.0.0
requests<3,>=2: Installed. No version info available.
rich: 13.9.4
SQLAlchemy<3,>=1.4: Installed. No version info available.
sse-starlette: 2.1.3
starlette: 0.46.1
structlog: 25.2.0
tenacity: 9.0.0
tenacity!=8.4.0,<10,>=8.1.0: Installed. No version info available.
tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
tiktoken<1,>=0.7: Installed. No version info available.
typing-extensions>=4.7: Installed. No version info available.
uvicorn: 0.34.0
validators: 0.34.0
watchfiles: 1.0.4
zstandard: 0.23.0

@gbaian10
Copy link
Contributor

I get the same output, but without the UserWarning.

@NathanAP
Copy link

NathanAP commented Mar 28, 2025

I'm not sure this is closely related to what I posted in #4058 , but it seems that something changed in States that uses Pydantic model and broke it...

Edit: it seems my problem is related to the supervisor.

@hon-gyu
Copy link
Author

hon-gyu commented Mar 28, 2025

@gbaian10 the UserWarning shows when using a deployed graph (in my case, via langgraph dev)

@hon-gyu hon-gyu changed the title State validation error with generic Pydantic model Incorrect state validation when there's generic types in pydantic.BaseModel state schema Mar 28, 2025
@gbaian10
Copy link
Contributor

gbaian10 commented Mar 28, 2025

I think this is the same issue as the one mentioned in #4074 (comment).
Langgraph currently only validates content without modifying the values.

@hon-gyu
Copy link
Author

hon-gyu commented Mar 28, 2025

@gbaian10 thanks for looking into this. But I believe this is a different issue. The serialization itself is incorrect. It's using the incorrect type (C instead of C[A]) to serialize python dict into pydantic BaseModel. There's no modification involved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants