-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Pydantic graph state no longer deserializes UUIDs correctly #4184
Comments
I think this is the same issue as #4074.
import uuid
from typing import Any
from uuid import UUID
from langgraph.graph import StateGraph
from pydantic import BaseModel
from rich import get_console
class State(BaseModel):
id: UUID
def my_node(state: State) -> dict[str, Any]:
get_console().print(state)
assert isinstance(state.id, UUID), type(state.id) # assertion error
return {"id": state.id}
workflow = StateGraph(State)
workflow.add_node("my_node", my_node)
workflow.add_edge("__start__", "my_node")
graph = workflow.compile()
# graph.invoke({"id": "fe096781-5601-53d2-b2f6-0d3403f7e9ca"})
graph.invoke({"id": uuid.uuid4()}) If we pass in a UUID object directly, it works fine. |
@gbaian10 Thanks for the quick response. Indeed, looks like the root cause is the same. That said, is this something you'd consider changing? It seems odd to allow using Pydantic models to specify State without actually coercing inputs according to what the models specify. As a workaround, I'm currently forced to-reparse the state in certain nodes: def my_entrypoint_node(state: State):
state = State.model_validate(state.model_dump())
... |
Currently, I'm using I'm unsure if the following approach is good: using the entire import uuid
from langgraph.graph import StateGraph
from pydantic import BaseModel
from rich import get_console
console = get_console()
class State(BaseModel):
id: uuid.UUID
name: str
def my_node(state: State) -> State:
console.print(state)
state.name = "Bob"
return state
graph = StateGraph(State).set_entry_point(my_node.__name__).add_node(my_node).compile()
input_data = State(id=uuid.uuid4(), name="Alice")
output_data = graph.invoke(input_data)
print(type(output_data))
console.print(State.model_validate(output_data)) |
We do have logic to coerce inputs to the declared types, see https://github.com/langchain-ai/langgraph/blob/main/libs/langgraph/langgraph/graph/schema_utils.py |
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
UUID
objects (as per normal Pydantic behavior and like they used to in previous LangGraph versions)str
objectsSystem Info
System Information
Package Information
Optional packages not installed
Other Dependencies
The text was updated successfully, but these errors were encountered: