Skip to content

fix(task): handle agent assignment and empty JSON output#6271

Open
wufengwind wants to merge 1 commit into
crewAIInc:mainfrom
wufengwind:fix/task-agent-null-order
Open

fix(task): handle agent assignment and empty JSON output#6271
wufengwind wants to merge 1 commit into
crewAIInc:mainfrom
wufengwind:fix/task-agent-null-order

Conversation

@wufengwind

@wufengwind wufengwind commented Jun 21, 2026

Copy link
Copy Markdown

Prevent self.agent from being set to None before the guard check, which would break subsequent retries or replay.

Also fix falsy check on json_output to use 'is not None' so that empty dict {} is preserved as valid output.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed validation timing for agent assignment to ensure proper error handling when no agent is configured
    • Improved JSON output file serialization to correctly handle and preserve empty JSON objects during task execution

Prevent self.agent from being set to None before the guard
check, which would break subsequent retries or replay.

Also fix falsy check on json_output to use 'is not None'
so that empty dict {} is preserved as valid output.

@corridor-security corridor-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary: This PR adjusts task execution state handling and preserves empty JSON outputs when writing task results; no exploitable security vulnerabilities were identified.

Risk: Low risk. The changes do not introduce new public endpoints, authentication or authorization logic, external integrations, or unsafe handling of untrusted input.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Two behavioral fixes are applied symmetrically to Task._aexecute_core and Task._execute_core in task.py: the missing-agent validation is reordered to raise before self.agent = agent executes, and the output file branch switches from a truthiness check to an explicit json_output is not None comparison.

Changes

Task execution fixes

Layer / File(s) Summary
Agent validation before assignment (async + sync)
lib/crewai/src/crewai/task.py
In both _aexecute_core (line 648) and _execute_core (line 773), the guard raising on a missing agent is moved before self.agent = agent, preventing the assignment from occurring when validation fails.
Output file: json_output is not None check (async + sync)
lib/crewai/src/crewai/task.py
In both _aexecute_core (line 743) and _execute_core (line 868), the condition selecting saved output file content changes from if json_output to if json_output is not None, so an empty-dict JSON result is written as JSON rather than falling back to the pydantic/result serialization path.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title directly summarizes both main changes: agent assignment handling and JSON output validation fixes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@wufengwind wufengwind changed the title fix(task): reorder agent assignment before null check fix(task): handle agent assignment and empty JSON output Jun 21, 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.

1 participant