Skip to content

fix: async tasks receive empty context due to last_sync_output truncation#6456

Open
rafaelipuente wants to merge 2 commits into
crewAIInc:mainfrom
rafaelipuente:fix/async-task-context
Open

fix: async tasks receive empty context due to last_sync_output truncation#6456
rafaelipuente wants to merge 2 commits into
crewAIInc:mainfrom
rafaelipuente:fix/async-task-context

Conversation

@rafaelipuente

Copy link
Copy Markdown

Description

Fixes #6417.

Tasks configured with async_execution=True do not receive the correct context from prior tasks. In both _execute_tasks and _aexecute_tasks, the context for async tasks was built from [last_sync_output] if last_sync_output else [], but last_sync_output is only populated when resuming from a skipped/conditional task in prepare_task_execution and is otherwise None during a normal run. As a result, async tasks received an empty context and could not access the outputs of previous tasks.

Fix

Pass the full task_outputs list to _get_context() for async tasks in both executors, mirroring the behavior of synchronous tasks.

This complements #6415 (same code fix) by adding regression test coverage for both execution paths.

Testing

  • Added two regression tests in lib/crewai/tests/crew/test_async_crew.py:
    • native async path (_aexecute_tasks): asserts the async task's context contains the prior sync task's output
    • thread-based path (_execute_tasks): captures the context passed to execute_async and asserts it contains the prior sync task's output
  • Both tests fail on main (async task context is empty) and pass with the fix.
  • Full lib/crewai/tests/crew/test_async_crew.py suite passes (13 tests); ruff lint/format clean on changed files.

(Note: authored with the assistance of an AI coding agent. Please apply the llm-generated label.)

Made with Cursor

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 73ce88a8-3225-42d7-a1b4-0d0bd1d2de39

📥 Commits

Reviewing files that changed from the base of the PR and between 2b90117 and 8485b5f.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/crew.py
  • lib/crewai/tests/crew/test_async_crew.py

📝 Walkthrough

Walkthrough

Modified Crew._aexecute_tasks and Crew._execute_tasks so async tasks receive context built from the full accumulated task_outputs instead of only last_sync_output, which was often None. Added two regression tests confirming async tasks now receive prior sync task outputs in their context.

Changes

Async task context fix

Layer / File(s) Summary
Async and sync executor context construction
lib/crewai/src/crewai/crew.py
Both _aexecute_tasks and _execute_tasks now derive async task context from accumulated task_outputs instead of the last_sync_output-based list.
Regression tests for context propagation
lib/crewai/tests/crew/test_async_crew.py
Added tests verifying async tasks receive "Sync result" in their context for both the async (_aexecute_tasks) and threaded (_execute_tasks) execution paths.

Sequence Diagram(s)

sequenceDiagram
  participant Crew
  participant SyncTask
  participant AsyncTask

  Crew->>SyncTask: execute_sync()
  SyncTask-->>Crew: append output to task_outputs
  Crew->>AsyncTask: aexecute_sync(context=task_outputs)
  AsyncTask-->>Crew: result reflects full prior context
Loading

Related Issues: #6417

Suggested labels: bug, llm-generated

Suggested reviewers: joaomdmoura

🐰

  A hop, a fix, a context found,
  No more truncated outputs bound.
  Async tasks now see it all,
  Sync results answer the call.
  Tests confirm the fix stands tall.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the fix: async tasks now receive full context instead of truncated last_sync_output.
Description check ✅ Passed The description matches the code changes and regression tests for async task context.
Linked Issues check ✅ Passed The PR satisfies #6417 by using task_outputs for async task context in both executors and adding regression tests.
Out of Scope Changes check ✅ Passed The changes stay within the bug fix and test coverage described by the linked issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@lorenzejay lorenzejay self-assigned this Jul 7, 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: Async tasks (async_execution=True) receive empty context due to last_sync_output truncation

2 participants