[PR #6621] fix: Reconstruct Pydantic output_schema in invoke/ainvoke (#6607) #5171

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

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

State: closed
Merged: No


Description:

Fixes issue where Graph silently converts Pydantic output_schema to Dict instead of returning the Pydantic instance. When a node returns a Pydantic model and output_schema is set to that Pydantic class, the graph was decomposing the model into fields, writing them to channels, and reconstructing as a plain dict without reconstituting the Pydantic model.

This PR adds reconstruction logic in CompiledStateGraph.invoke() and CompiledStateGraph.ainvoke() to validate and reconstruct the Pydantic model from the dict output when output_schema is a BaseModel subclass.

Changes:

  • Override invoke() and ainvoke() methods in CompiledStateGraph
  • Add _reconstruct_output() helper method that checks if output_schema is a Pydantic BaseModel and reconstructs it using model_validate()
  • Falls back gracefully to dict if reconstruction fails (maintains backward compatibility)
  • Add 3 comprehensive tests covering sync invoke, async ainvoke, and TypedDict backward compatibility

Issue: Closes #6607

Dependencies: None

Twitter handle: N/A


Testing:

  • Added test_output_schema_pydantic_reconstruction() - tests sync invoke returns Pydantic instance
  • Added test_output_schema_pydantic_reconstruction_async() - tests async ainvoke returns Pydantic instance
  • Added test_output_schema_dict_backwards_compat() - ensures TypedDict output_schema still works
  • All tests pass syntax validation
  • Changes are backward compatible - only affects Pydantic BaseModel output schemas
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6621 **State:** closed **Merged:** No --- **Description:** Fixes issue where Graph silently converts Pydantic output_schema to Dict instead of returning the Pydantic instance. When a node returns a Pydantic model and `output_schema` is set to that Pydantic class, the graph was decomposing the model into fields, writing them to channels, and reconstructing as a plain dict without reconstituting the Pydantic model. This PR adds reconstruction logic in `CompiledStateGraph.invoke()` and `CompiledStateGraph.ainvoke()` to validate and reconstruct the Pydantic model from the dict output when `output_schema` is a BaseModel subclass. **Changes:** - Override `invoke()` and `ainvoke()` methods in `CompiledStateGraph` - Add `_reconstruct_output()` helper method that checks if output_schema is a Pydantic BaseModel and reconstructs it using `model_validate()` - Falls back gracefully to dict if reconstruction fails (maintains backward compatibility) - Add 3 comprehensive tests covering sync invoke, async ainvoke, and TypedDict backward compatibility **Issue:** Closes #6607 **Dependencies:** None **Twitter handle:** N/A --- **Testing:** - ✅ Added `test_output_schema_pydantic_reconstruction()` - tests sync invoke returns Pydantic instance - ✅ Added `test_output_schema_pydantic_reconstruction_async()` - tests async ainvoke returns Pydantic instance - ✅ Added `test_output_schema_dict_backwards_compat()` - ensures TypedDict output_schema still works - ✅ All tests pass syntax validation - ✅ Changes are backward compatible - only affects Pydantic BaseModel output schemas
yindo added the pull-request label 2026-02-20 17:51:20 -05:00
yindo closed this issue 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#5171