fix: pass complete context to async tasks instead of truncating to empty/last output#6415
fix: pass complete context to async tasks instead of truncating to empty/last output#6415Om-Borse26 wants to merge 5 commits into
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis change modifies how context is built for tasks marked ChangesAsync Task Context Construction
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
(Note: As per CONTRIBUTING.md, this PR was authored with the assistance of an AI coding agent. Please apply the Steps to Reproduce / Proof of BugTo 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)Execution Logs (After this PR) |
|
Fixes #6417 |
Description
Currently, tasks configured with
async_execution=Truedo 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_outputis only populated when resuming from a skipped/conditional task (inprepare_task_execution) and is otherwiseNoneduring 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_outputslist to_get_context()for async tasks, perfectly mirroring the behavior of synchronous tasks.Root Cause
crew.py, sync tasks correctly usetask_outputsfor context generation.[last_sync_output], which is never updated during standard task execution iterations, leading to an empty context string.Fix
src/crewai/crew.pyto usetask_outputs.Testing
pytestsuite passes locally with no regressions.contextstring.