Implement comprehensive streaming support for CrewAI

- Add streaming events: CrewStreamChunkEvent, TaskStreamChunkEvent, AgentStreamChunkEvent
- Extend Crew.kickoff() with stream parameter and callback support
- Propagate streaming through task and agent execution chains
- Integrate with existing LLM streaming infrastructure
- Add comprehensive tests and examples
- Maintain backward compatibility

Fixes #2950

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-06-04 07:00:54 +00:00
parent 2bd6b72aae
commit b3b2b1e25f
14 changed files with 1225 additions and 6 deletions

68
docs/streaming.md Normal file
View File

@@ -0,0 +1,68 @@
# Streaming Support in CrewAI
CrewAI now supports real-time streaming output during crew execution, allowing you to see the progress of agents and tasks as they work.
## Basic Usage
```python
from crewai import Agent, Task, Crew
from crewai.llm import LLM
def stream_callback(chunk, agent_role, task_description, step_type):
print(f"[{agent_role}] {step_type}: {chunk}", end="", flush=True)
llm = LLM(model="gpt-4o-mini", stream=True)
agent = Agent(
role="Writer",
goal="Write content",
backstory="You are a skilled writer.",
llm=llm
)
task = Task(
description="Write a short story",
expected_output="A creative story",
agent=agent
)
crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff(
stream=True,
stream_callback=stream_callback
)
```
## Multi-Agent Streaming
```python
def enhanced_callback(chunk, agent_role, task_description, step_type):
print(f"[{agent_role}] {task_description[:20]}... - {step_type}: {chunk}")
researcher = Agent(role="Researcher", ...)
writer = Agent(role="Writer", ...)
research_task = Task(description="Research topic", agent=researcher)
write_task = Task(description="Write article", agent=writer, context=[research_task])
crew = Crew(agents=[researcher, writer], tasks=[research_task, write_task])
result = crew.kickoff(stream=True, stream_callback=enhanced_callback)
```
## Stream Callback Parameters
- `chunk`: The streaming text chunk
- `agent_role`: Role of the agent producing the chunk
- `task_description`: Description of the current task
- `step_type`: Type of step ("agent_thinking", "final_answer", "llm_response")
## Events
The streaming system emits `CrewStreamChunkEvent`, `TaskStreamChunkEvent`, and `AgentStreamChunkEvent` that can be handled using the event bus.
## Requirements
- Enable streaming on your LLM: `LLM(model="...", stream=True)`
- Use the `stream=True` parameter in `crew.kickoff()`
- Provide a callback function to handle streaming chunks