feat: add kickoff_stream method for FastAPI streaming integration #3740
+367
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
feat: add kickoff_stream method for FastAPI streaming integration
Summary
This PR adds a new
kickoff_stream()method to the Crew class that enables real-time streaming of crew execution events. This addresses issue #3739 where users requested guidance on integrating CrewAI with FastAPI for streaming output.Key changes:
Crew.kickoff_stream()method that yields event dictionaries as the crew executesfastapi_streaming_example.py) demonstrating Server-Sent Events (SSE) integrationHow it works:
The method registers temporary event handlers to the global event bus, runs
kickoff()in a background thread, and yields events through a queue as they occur. This makes it easy to integrate with streaming frameworks like FastAPI'sStreamingResponse.Review & Testing Checklist for Human
Event handler cleanup is broken - The code registers handlers to the global
crewai_event_bussingleton but never unregisters them. This will cause memory leaks and duplicate events on subsequent calls. Thefinallyblock only joins the thread but doesn't clean up handlers. This needs to be fixed before merge.Test the FastAPI example end-to-end - Run
python fastapi_streaming_example.pyand verify streaming works correctly athttp://localhost:8000/stream?topic=test. The example wasn't tested during development.Verify tests pass in CI - Tests are marked with
@pytest.mark.vcrbut no cassettes were recorded. These may fail on first CI run unless VCR is in record mode or you record cassettes locally first.Check for memory leaks - Call
kickoff_stream()multiple times in a row and verify event handlers aren't accumulating. Use a test like:Notes