Lorenze/imp/conversational flow traces#6044
Conversation
- Introduced `ConversationMessageAddedEvent` and `ConversationRouteSelectedEvent` to enhance conversational flow tracking. - Updated event listeners to emit these events during message handling and routing decisions. - Enhanced the `_ConversationalMixin` class to emit events for user and assistant messages, as well as selected routes. - Added tests to verify the correct emission of these events during conversational turns.
…onversational-flow-traces
📝 WalkthroughWalkthroughAdds two conversation FlowEvent types, emits them during routing and message appends, wires handlers into tracing, and refactors Flow kickoff to support deferred session-level FlowStartedEvent across multi-turn conversations. ChangesConversational flow event observability
Sequence DiagramsequenceDiagram
participant ConversationalMixin
participant EventBus
participant TraceListener
participant Flow
ConversationalMixin->>EventBus: emit conversation_message_added (user, session_id, message_index)
EventBus->>TraceListener: forward conversation_message_added -> _handle_action_event
ConversationalMixin->>EventBus: emit conversation_route_selected (route, previous_intent, session_id)
EventBus->>TraceListener: forward conversation_route_selected -> _handle_action_event
ConversationalMixin->>EventBus: emit conversation_message_added (assistant, session_id, message_index)
EventBus->>TraceListener: forward conversation_message_added -> _handle_action_event
Flow->>Flow: kickoff_async() (may restore deferred FlowStartedEvent scope)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/crewai/src/crewai/events/listeners/tracing/trace_listener.py`:
- Around line 260-270: Conversation events (ConversationMessageAddedEvent and
ConversationRouteSelectedEvent) may arrive before FlowStartedEvent creates the
tracing batch, so update the on_conversation_message_added and
on_conversation_route_selected handlers to ensure the trace batch is initialized
before calling _handle_trace_event; specifically, call the same batch
initialization logic used for FlowStartedEvent (or extract that logic into a
helper like _ensure_trace_batch_exists and invoke it here), falling back to
creating a batch from the event’s flow/run identifiers if needed, then proceed
to call
self._handle_trace_event("conversation_message_added"/"conversation_route_selected",
source, event).
In `@lib/crewai/src/crewai/experimental/conversational_mixin.py`:
- Around line 482-483: The code sets message_index using max(len(state.messages)
- 1, 0), which yields 0 for empty transcripts and may point to a non-existent
message; change this to emit None when state.messages is empty (e.g.,
message_index = (len(state.messages) - 1) if state.messages else None) in the
place where message_index is set (the conversational mixin method that builds
the event/state using state.messages and previous_intent in ConversationalMixin
/ conversational_mixin.py).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 1de92f62-edb3-4176-84d8-16823a68fecc
📒 Files selected for processing (7)
lib/crewai/src/crewai/events/__init__.pylib/crewai/src/crewai/events/event_types.pylib/crewai/src/crewai/events/listeners/tracing/trace_listener.pylib/crewai/src/crewai/events/types/flow_events.pylib/crewai/src/crewai/experimental/conversational_mixin.pylib/crewai/src/crewai/flow/runtime.pylib/crewai/tests/test_flow_conversation.py
…onversational-flow-traces
… handlers Updated the class to replace with for and events, improving clarity in event handling. Additionally, adjusted comments in the class to clarify the application of pending user messages in relation to state restoration and flow scope initialization.
Updated the message index handling in the class to return when there are no messages. Added tests to ensure that route events do not reference index zero when the transcript is empty, and verified the correct emission of conversation message events during flow handling.
…onversational-flow-traces
…onversational-flow-traces
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 5fbcbe0. Configure here.

Note
Low Risk
Changes are limited to event emission, trace listener registration, and kickoff ordering for conversational/deferred tracing; no auth or data-path changes, with new tests covering the behavior.
Overview
Adds first-class observability for conversational flows:
ConversationMessageAddedEventandConversationRouteSelectedEventare defined, exported, wired into the global event union, and recorded by the trace listener. The conversational mixin emits these when users/assistants append messages and whenroute_conversationpicks a route (includingprevious_intent).Kickoff ordering in
runtime.pyis adjusted so deferred multi-turn sessions emit oneflow_startedper session (later turns restore the stashed event scope instead of re-opening), and pending user messages are applied after flow scope init so transcript events parent correctly under the conversation trace.Tests cover event payloads, single
flow_startedunderdefer_trace_finalization, andmessage_index=Nonewhen routing with an empty transcript.Reviewed by Cursor Bugbot for commit 5fbcbe0. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by CodeRabbit