Skip to content

fix(flow,task): handle pydantic outputs in guardrail retries and checkpoint serialization#5545

Open
ibrahim1023 wants to merge 1 commit intocrewAIInc:mainfrom
ibrahim1023:fix-5544-pydantic-guardrail-checkpoint
Open

fix(flow,task): handle pydantic outputs in guardrail retries and checkpoint serialization#5545
ibrahim1023 wants to merge 1 commit intocrewAIInc:mainfrom
ibrahim1023:fix-5544-pydantic-guardrail-checkpoint

Conversation

@ibrahim1023
Copy link
Copy Markdown

@ibrahim1023 ibrahim1023 commented Apr 20, 2026

Summary

Fixes #5544 by addressing two related issues when using Pydantic outputs in flows/tasks:

  1. Guardrail retries could rebuild TaskOutput with a non-string raw value when the model returned a BaseModel.
  2. Flow checkpoint serialization could fail with PydanticSerializationError when initial_state was assigned as a model class on flow subclasses.

What Changed

  • Unified TaskOutput construction in lib/crewai/src/crewai/task.py so initial execution and guardrail retry paths use the same output-shaping logic.
  • Updated sync and async guardrail retry code paths to use that shared builder, preserving expected raw/pydantic semantics for structured outputs.
  • Ensured Flow.initial_state remains excluded from serialized flow entities and preserved inherited field metadata when flow subclasses override parent fields in FlowMeta (lib/crewai/src/crewai/flow/flow.py).
  • Added focused regression tests:
    • test_guardrail_retry_preserves_pydantic_output (lib/crewai/tests/test_task.py)
    • test_ainvoke_guardrail_retry_preserves_pydantic_output (lib/crewai/tests/task/test_async_task.py)
    • test_flow_checkpoint_handles_nested_pydantic_models (lib/crewai/tests/test_checkpoint.py)

Why

  • Guardrail retries should behave identically to first-pass execution for typed outputs.
  • Checkpoint serialization should snapshot runtime state, not class-valued configuration artifacts.

Validation

Passed targeted suites for the touched behavior:

  • uv run pytest lib/crewai/tests/test_checkpoint.py -q
  • uv run pytest lib/crewai/tests/test_task.py -q -k 'guardrail or output_pydantic'
  • uv run pytest lib/crewai/tests/task/test_async_task.py -q -k 'guardrail'

Also confirmed new regression tests pass individually after formatting.

Notes

  • Full repo CI-equivalent runs in this local environment show broad unrelated failures due to missing optional dependencies/providers (for example a2a, provider SDK extras, qdrant, oxylabs) and interactive tool prompts in some tool tests.
  • No unrelated code changes are included in this patch scope.

@ibrahim1023 ibrahim1023 changed the title fix(flow,task): handle pydantic outputs in guardrail retries and chec… fix(flow,task): handle pydantic outputs in guardrail retries and checkpoint serialization Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Checkpointing + guardrail retry incompatible with Pydantic outputs in Flow

1 participant