Skip to content

fix: pass complete context to async tasks instead of truncating to empty/last output#6415

Open
Om-Borse26 wants to merge 5 commits into
crewAIInc:mainfrom
Om-Borse26:fix/async-task-context-truncation
Open

fix: pass complete context to async tasks instead of truncating to empty/last output#6415
Om-Borse26 wants to merge 5 commits into
crewAIInc:mainfrom
Om-Borse26:fix/async-task-context-truncation

Conversation

@Om-Borse26

Copy link
Copy Markdown

Description

Currently, tasks configured with async_execution=True do not receive the correct context from prior tasks.
In _execute_tasks (and _aexecute_tasks), the context for async tasks is generated using [last_sync_output] if last_sync_output else []. However, last_sync_output is only populated when resuming from a skipped/conditional task (in prepare_task_execution) and is otherwise None during a normal execution run.
As a result, async tasks receive a completely empty context and are unable to access the outputs of previous tasks in the pipeline.

This PR fixes the issue by passing the full task_outputs list to _get_context() for async tasks, perfectly mirroring the behavior of synchronous tasks.

Root Cause

  • In crew.py, sync tasks correctly use task_outputs for context generation.
  • Async tasks were hardcoded to use [last_sync_output], which is never updated during standard task execution iterations, leading to an empty context string.

Fix

  • Modified lines 1343-1345 and 1555-1557 in src/crewai/crew.py to use task_outputs.

Testing

  • pytest suite passes locally with no regressions.
  • Verified manually that async tasks now receive the expected concatenated output of all preceding tasks in their context string.

async_execution=True tasks were receiving only the last synchronous
task's output as context, silently discarding outputs from all earlier
tasks in the pipeline.

Root cause: both _execute_tasks() and _aexecute_tasks() passed
[last_sync_output] to _get_context() for async tasks, while sync
tasks correctly received the full task_outputs list.

Fix: pass task_outputs (all completed outputs) to _get_context()
for async tasks, matching the behavior of sync tasks.
@coderabbitai

coderabbitai Bot commented Jul 1, 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: 3a903a3b-da12-4479-9261-65183cf34ce7

📥 Commits

Reviewing files that changed from the base of the PR and between 1452ee2 and 548ca1e.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/crew.py

📝 Walkthrough

Walkthrough

This change modifies how context is built for tasks marked async_execution in both Crew._aexecute_tasks() and Crew._execute_tasks(). Instead of passing only the last synchronous output, the full accumulated task_outputs list is now passed as context.

Changes

Async Task Context Construction

Layer / File(s) Summary
Accumulated context for async tasks
lib/crewai/src/crewai/crew.py
Both _aexecute_tasks() and _execute_tasks() now construct async task context from the full accumulated task_outputs list rather than only the most recent synchronous output (last_sync_output).
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: async tasks now receive full context instead of only the last/empty output.
Description check ✅ Passed The description matches the change and explains the async context bug and fix accurately.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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.

@Om-Borse26

Copy link
Copy Markdown
Author

(Note: As per CONTRIBUTING.md, this PR was authored with the assistance of an AI coding agent. Please apply the llm-generated label to this PR as I do not have permissions to add labels myself.)

Steps to Reproduce / Proof of Bug

To make it easier for reviewers to verify this bug, here is a minimal reproduction script and the execution logs proving that async tasks currently receive a completely empty context string, and that this PR successfully fixes it by passing the full task outputs.

Reproduction Script (demo.py)
import os
from crewai import Agent, Task, Crew, Process

agent = Agent(
    role="Data Compiler",
    goal="Compile specs",
    backstory="You compile specifications.",
    llm="openai/gpt-4o-mini"
)

# 1. Sync Task
task1 = Task(
    description="Output exactly: '[SPECS] V8 Engine'",
    expected_output="A spec string",
    agent=agent,
)

# 2. Sync Task
task2 = Task(
    description="Output exactly: '[SPECS] Carbon Fiber Chassis'",
    expected_output="A spec string",
    agent=agent,
)

# 3. Async Task that relies on context from 1 and 2
task3 = Task(
    description="Review the provided context. If it is empty or missing the V8 Engine and Chassis, output 'FAILED: Context is empty'. If you see them, output them.",
    expected_output="The final combined specs",
    agent=agent,
    async_execution=True, 
    context=[task1, task2] 
)

crew = Crew(
    agents=[agent],
    tasks=[task1, task2, task3],
    process=Process.sequential
)

crew.kickoff()

print("\n--- Context actually handed to Task 3 (Async): ---")
print(task3.output.raw)
Execution Logs (Before this PR - Main Branch)
# The context passed to Task 3 is entirely lost.
--- Context actually handed to Task 3 (Async): ---
FAILED: Context is empty
Execution Logs (After this PR)
# Task 3 successfully receives the accumulated output of all prior tasks in the pipeline.
--- Context actually handed to Task 3 (Async): ---
[SPECS] V8 Engine
[SPECS] Carbon Fiber Chassis

@Om-Borse26

Copy link
Copy Markdown
Author

Fixes #6417

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