fix(streaming): prefer event.task_id when building StreamChunk.task_id#5375
Open
llaoj wants to merge 2 commits intocrewAIInc:mainfrom
Open
fix(streaming): prefer event.task_id when building StreamChunk.task_id#5375llaoj wants to merge 2 commits intocrewAIInc:mainfrom
llaoj wants to merge 2 commits intocrewAIInc:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR makes
StreamChunk.task_iduseevent.task_idas the primary source, with a backward-compatible fallback tocurrent_task_info["id"].StreamChunk.task_idcould be empty whencurrent_task_info["id"]was empty.StreamChunk.task_id = event.task_id or current_task_info["id"] or "".Why this matters
LLMStreamChunkEvent.task_idis populated fromfrom_taskin event normalization, whilecurrent_task_info["id"]may remain empty in some streaming contexts.So even when task identity exists in the event, downstream consumers can still receive chunks with empty
chunk.task_idifStreamChunk.task_idis built only fromcurrent_task_info.Real downstream example (SSE filtering)
In a downstream FastAPI SSE endpoint, we collect crew task IDs and filter stream chunks by
chunk.task_id:If
chunk.task_idis empty, valid chunks can be dropped by filtering logic.Related concurrency concern (important, not fully solved by this PR)
While each request/run typically has its own queue, stream handlers are registered on the singleton
crewai_event_bus.That means with concurrent streaming runs:
handler_A-> writes toqueue_Ahandler_B-> writes toqueue_BLLMStreamChunkEventcan be dispatched to all active handlersSo this is not a "shared queue" problem; it is an event fan-out to multiple handlers problem on a global event bus, which can cause cross-run chunk contamination if handler-level scoping/filtering is missing.
This PR addresses task_id correctness in
StreamChunk.A follow-up improvement could add stronger run/stream scoping for handler dispatch.
Compatibility
Backward compatible:
If
event.task_idis missing, existing fallback behavior (current_task_info["id"]) is preserved.Test plan
task_idwhen event contains task context.event.task_idis unavailable.task_id.