turn_tracking_observer

Turn tracking observer for conversation flow monitoring.

This module provides an observer that monitors conversation turns in a pipeline, tracking when turns start and end based on user and bot speech patterns.

class pipecat.observers.turn_tracking_observer.TurnTrackingObserver(max_frames: int | None = None, turn_end_timeout_secs: float = 2.5, **kwargs)[source]

Bases: BaseObserver

Observer that tracks conversation turns in a pipeline.

This observer monitors the flow of conversation by tracking when turns start and end based on user and bot speaking patterns. It handles interruptions, timeouts, and maintains turn state throughout the pipeline.

Turn tracking logic:

  • The first turn starts immediately when the pipeline starts (StartFrame)

  • Subsequent turns start when the user starts speaking

  • A turn ends when the bot stops speaking and either:

    • The user starts speaking again

    • A timeout period elapses with no more bot speech

Events:

  • on_turn_started: Triggered when a turn starts, providing the turn number

  • on_turn_ended: Triggered when a turn ends, providing the turn number, the turn’s duration in seconds, and whether it was interrupted

__init__(max_frames: int | None = None, turn_end_timeout_secs: float = 2.5, **kwargs)[source]

Initialize the turn tracking observer.

Parameters:
  • max_frames –

    Unused.

    Deprecated since version 1.12.0: No replacement. The observer receives each frame once. Will be removed in 2.0.0.

  • turn_end_timeout_secs – Timeout in seconds after bot stops speaking before automatically ending the turn. Defaults to 2.5.

  • **kwargs – Additional arguments passed to the parent observer.

async on_push_frame(data: FramePushed)[source]

Process frame events for turn tracking.

Parameters:

data – Frame push event data containing the frame and metadata.

async cleanup()[source]

Cancel the pending turn end timer.

The pipeline can be torn down before this observer receives its EndFrame or CancelFrame, and the timer’s callback holds the pipeline until it fires.