Skip to content

fix: ensure TaskOutput.pydantic is populated on first guardrail invocation#4370

Open
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1770231622-fix-guardrail-pydantic-parsing
Open

fix: ensure TaskOutput.pydantic is populated on first guardrail invocation#4370
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1770231622-fix-guardrail-pydantic-parsing

Conversation

@devin-ai-integration
Copy link
Contributor

fix: ensure TaskOutput.pydantic is populated on first guardrail invocation

Summary

Fixes #4369 where TaskOutput.pydantic and TaskOutput.json_dict were None on the first guardrail invocation but correctly parsed on retry attempts.

The root cause was that _export_output() was intentionally skipped when guardrails were present on the first attempt. This fix removes that conditional skip in both _execute_core() (sync) and _aexecute_core() (async), ensuring consistent behavior across all guardrail invocations.

Before: Guardrail functions received TaskOutput with pydantic=None on first call
After: Guardrail functions receive properly parsed TaskOutput.pydantic on all calls

Review & Testing Checklist for Human

  • Verify the async execution path (_aexecute_core) works correctly with guardrails - the new tests only cover sync execution
  • Confirm there wasn't a specific reason (e.g., performance optimization or edge case handling) for the original conditional skip of _export_output() when guardrails exist
  • Test with a real agent and LLM to verify end-to-end behavior matches expectations

Suggested test plan:

from crewai import Agent, Task, Crew
from pydantic import BaseModel

class MyOutput(BaseModel):
    message: str
    status: str

def my_guardrail(task_output):
    print(f"Pydantic: {task_output.pydantic}")  # Should NOT be None
    return (True, task_output)

agent = Agent(role="Test", goal="Test", backstory="Test")
task = Task(
    description="Return JSON with message and status fields",
    expected_output="JSON object",
    output_pydantic=MyOutput,
    guardrail=my_guardrail,
    agent=agent,
)
crew = Crew(agents=[agent], tasks=[task])
crew.kickoff()

Notes

…ation

Previously, when a task had guardrails configured, _export_output() was
intentionally skipped on the first attempt, causing TaskOutput.pydantic
and TaskOutput.json_dict to be None. This made it difficult to write
guardrail functions that need to access the structured Pydantic output.

This fix removes the conditional skip of _export_output() in both
_execute_core() and _aexecute_core(), ensuring consistent behavior
across all guardrail invocations.

Fixes #4369

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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] TaskOutput.pydantic is None on first guardrail attempt but parsed on retries

0 participants