refactor: convert Flow to Pydantic BaseModel#5190
Merged
greysonlalonde merged 16 commits intomainfrom Mar 31, 2026
Merged
Conversation
FlowMeta inherits ModelMetaclass. Flow uses Field/PrivateAttr with model_validator and model_post_init. AgentExecutor uses proper Fields with eager init, removing deferred _ensure_flow_initialized.
…lures - _flow_post_init tracks _flow_post_init_done to prevent duplicate trace listener setup and event emission from MRO paths - model_rebuild failure in __init__.py logs a warning instead of silently passing
…IInc/crewAI into refactor/flow-to-basemodel
model_rebuild now resolves SystemPromptResult/StandardPromptResult at runtime, which triggers Pydantic's TypedDict version check.
Restores the original condition so explicit initial_state values are not silently ignored when _initial_state_t is set. The BaseModel instance path now wraps with FlowState when id is missing instead of raising, matching the type-parameter path behavior.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 4 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
iris-clawd
approved these changes
Mar 31, 2026
Contributor
iris-clawd
left a comment
There was a problem hiding this comment.
LGTM — solid refactor converting Flow to Pydantic BaseModel. The stop_words bug fix alone is worth it. Previously reviewed concerns were addressed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Summary
_ensure_flow_initializedTest plan
Note
High Risk
High risk because it refactors core orchestration (
Flow) and execution (AgentExecutor) initialization/state semantics, which can affect persistence/resumption, threading locks, and event emission across many call sites.Overview
Converts
Flowinto a PydanticBaseModelwithField/PrivateAttrdefinitions, aModelMetaclass-basedFlowMeta, and a new post-init path (model_post_init/_flow_post_init) to initialize state, tracing, memory, and method registration.Refactors
AgentExecutorto be fully Pydantic-initialized (typedFields +model_validator), removes the deferred_ensure_flow_initializedpath, and standardizes onstop_words(fixing a bug inAgentthat was settingstop).Updates flow persistence usage from
_persistencetopersistence, addsMemory.__deepcopy__to avoid copying unpickleable executors/locks, rebuildsAgentExecutorforward refs at import time in__init__.py, and adjusts tests to construct executors viamodel_constructand to use the renamed persistence field.Written by Cursor Bugbot for commit c0ced12. This will update automatically on new commits. Configure here.