Fix Flow initialization with Pydantic models having required fields #3745
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Flow initialization with Pydantic models having required fields
Summary
Fixes issue #3744 where Flow initialization fails with
ValidationErrorwhen using Pydantic BaseModel state classes that have required (non-optional) fields.Root cause: The
_create_initial_state()method was instantiating Pydantic models without any arguments (state_type()), causing validation to fail for required fields. The kwargs passed toFlow.__init__()were only applied afterwards via_initialize_state(), which was too late for Pydantic's validation.Solution: Modified
_create_initial_state()to accept and use kwargs during state instantiation, allowing required fields to be provided upfront and pass Pydantic validation.Changes made:
_create_initial_state(self, kwargs: dict[str, Any] | None = None)to accept kwargs parameterFlow.__init__()to pass kwargs directly to_create_initial_state()_initialize_state(kwargs)call from__init__since kwargs are now handled during state creationBackwards compatibility:
test_flow.py, 8 intest_flow_default_override.pyandtest_flow_persistence.py)_initialize_state()method remains unchanged and continues to be used bykickoff_async()for runtime state updatesReview & Testing Checklist for Human
MyFlow(name="John", age=30)flow.kickoff(inputs={...})still properly updates state during executionTest Plan
Notes