Repository navigation
fix(flow): revive a method's Pydantic output after a checkpoint restore - #9536
Harvey DU (Luckyou-d) wants to merge 3 commits into
Conversation
Checkpoints serialise `_method_outputs` with `model_dump_json()`, so a method that returned a BaseModel comes back as a plain dict. A listener that reads model attributes then raises: `charge(quote)` fails on `quote.amount`, and a `@human_feedback` listener loses `result.feedback` on the resumed run, so the run that was restored to recover from a failure fails again permanently. Re-validate each restored output against the method's annotated return type, resolving the annotation on the wrapped function because the flow-method wrappers carry their own generic annotations. Outputs that do not fit (a dict that really is a dict, a method this instance does not know, a value that fails validation) are kept exactly as restored. Applied to both the checkpoint and `from_pending` restore paths. Fixes crewAIInc#8974
Strix Security ReviewWarning This pull request has 28 commits after the last Strix review ( ✅ No open security findings. PR summary: On a resumed run, a method's Pydantic return value came back as a plain dict from the checkpoint, so listeners reading attributes crashed; the PR re-validates restored method outputs against the method's annotated return type on both the checkpoint and pending-feedback restore paths, and adds a regression test. Review summaryThe change is confined to the flow checkpoint restore path: Ruled out (3)
Review 1 on this PR · Reviewed by Strix |
Summary
Checkpoints serialise
_method_outputswithmodel_dump_json(), so a method that returned a BaseModel comes back as a plain dict. A listener that reads model attributes then raises on the resumed run:charge(quote)fails onquote.amount, and a@human_feedbacklistener losesresult.feedback. The run that was restored to recover from a failure fails again, permanently.Reproduced on
main(435262c) with the issue's setup (CheckpointConfig(location=..., on_events=["method_execution_finished"])):The checkpoint file stores
{"method": "price", "output": {"amount": 42}}.Change
_revive_method_output()re-validates a restored output against the method's annotated return type. Two details that mattered:method._meth), becauseStartMethod/ListenMethodreplace__annotations__with their own generic ones, soget_type_hints(wrapper)returns{};Applied to both the checkpoint (
_restore_from_checkpoint) andfrom_pendingrestore paths.Verification
test_checkpoint_restore_revives_pydantic_listener_inputsintests/test_flow_resumability_regression.py: fails onmain(received == ["Quote", "dict"]), passes with the change (["Quote", "Quote"]), and the flow reachesstate["charged"] == 42pytest tests/test_flow.py tests/test_flow_persistence.py tests/test_flow_resumability_regression.py tests/test_flow_human_input_integration.py tests/test_flow_ask.py-> 167 passed, 1 failed; that failure istest_persist_complex_types_dict_state, which fails identically onmainin this environment (a WindowsPathseparator assertion), unrelated to this changeruff checkandruff format --checkclean on both filesFixes #8974