user_turn_controller
This module defines a controller for managing user turn lifecycle.
- class pipecat.turns.user_turn_controller.UserTurnController(*, user_turn_strategies: UserTurnStrategies, user_turn_strategies_params: UserTurnStrategiesParams | None = None, user_turn_stop_timeout: float = 5.0)[source]
Bases:
BaseObjectController for managing user turn lifecycle.
This class manages user turn state (active/inactive), handles start and stop strategies, and emits events when user turns begin, end, or timeout occurs.
How a turn starts and ends follows the
start_modeandstop_modeof itsUserTurnStrategiesParams. WithStrategyMode.FIRSTthe first strategy to decide starts or ends the turn. WithStrategyMode.ALLthe turn starts or ends once every strategy in the list has decided. Turns start on the first start strategy and end once every stop strategy agrees by default. A stop strategy that waits for the turn’s inference gets its say in either mode: in aFIRSTlist, the first stop strategy to decide runs the inference, and the turn ends once a strategy waiting for that inference decides. A cancelled answer voids the stop decisions made on it.In an
ALLlist the controller starts the turn’s inference itself, once every stop strategy that listens to the user has decided. A stop strategy that waits for the inference instead, such as one reading the LLM’s turn-completion marker, then decides on that inference. An eager end of turn still starts its speculation at once.Event handlers available:
on_user_turn_started: Emitted when a user turn starts.
on_user_turn_inference_triggered: Emitted when enough signal exists to start LLM inference, carrying a UserTurnSpeculation when the turn it answers hasn’t ended yet. Fires together with on_user_turn_stopped for most strategies; fires alone when the turn’s end waits for a decision on the inference, or for an eager end of turn to be confirmed.
on_user_turn_stopped: Emitted when a user turn is semantically final.
on_user_turn_resumed: Emitted when the user starts speaking again in an open turn, after they stopped or after the turn’s inference was triggered. Its UserTurnResumedParams say whether that inference was triggered, since the new speech voids the answer it started.
on_user_turn_stop_timeout: Emitted when the stop timeout ends a user turn its stop strategies didn’t end.
on_user_turn_speculation_cancelled: Emitted when a stop strategy withdraws its speculative inference, such as an eager end of turn whose prediction turned out wrong.
on_reset_aggregation: Emitted when a start strategy wants the user’s aggregated text dropped, as the wake phrase strategy does for speech before the wake phrase.
on_push_frame: Emitted when a strategy wants to push a frame.
on_broadcast_frame: Emitted when a strategy wants to broadcast a frame.
Example:
@controller.event_handler("on_user_turn_started") async def on_user_turn_started(controller, strategy: BaseUserTurnStartStrategy, params: UserTurnStartedParams): ... @controller.event_handler("on_user_turn_inference_triggered") async def on_user_turn_inference_triggered( controller, strategy: BaseUserTurnStopStrategy, speculation: UserTurnSpeculation | None, ): ... @controller.event_handler("on_user_turn_stopped") async def on_user_turn_stopped(controller, strategy: BaseUserTurnStopStrategy, params: UserTurnStoppedParams): ... @controller.event_handler("on_user_turn_resumed") async def on_user_turn_resumed(controller, params: UserTurnResumedParams): ... @controller.event_handler("on_user_turn_stop_timeout") async def on_user_turn_stop_timeout(controller): ... @controller.event_handler("on_user_turn_speculation_cancelled") async def on_user_turn_speculation_cancelled(controller): ... @controller.event_handler("on_reset_aggregation") async def on_reset_aggregation(controller, strategy: BaseUserTurnStartStrategy): ... @controller.event_handler("on_push_frame") async def on_push_frame(controller, frame: Frame, direction: FrameDirection): ... @controller.event_handler("on_broadcast_frame") async def on_broadcast_frame(controller, frame_cls: Type[Frame], **kwargs): ...
- __init__(*, user_turn_strategies: UserTurnStrategies, user_turn_strategies_params: UserTurnStrategiesParams | None = None, user_turn_stop_timeout: float = 5.0)[source]
Initialize the user turn controller.
- Parameters:
user_turn_strategies – Configured strategies for starting and stopping user turns.
user_turn_strategies_params – How the strategies decide. Defaults to
UserTurnStrategiesParams.user_turn_stop_timeout – Seconds without user activity before an open user turn is ended, unless a stop strategy has a decision scheduled.
- property user_turn_strategies: UserTurnStrategies
The currently active user turn strategies.
- async setup(setup: FrameProcessorSetup)[source]
Set up the controller.
- Parameters:
setup – Configuration object containing setup parameters.
- async start()[source]
Start watching for a turn that stops without the user stopping.
Paired with
stop().
- async stop()[source]
Stop the turn stop timeout, leaving the strategies alone.
Called at session end. The strategies may be shared, so cleaning them up waits for
cleanup().
- async update_strategies(strategies: UserTurnStrategies)[source]
Replace the current strategies with the given ones.
- Parameters:
strategies – The new user turn strategies the controller should use.
- property resolves_proposed_turn_start_frames: bool
Whether any active start strategy resolves proposed turn starts.
A proposal is resolved once, so a caller holding this controller should stop forwarding
ProposedUserStartedSpeakingFramewhen this is True — passing it along would let a resolver further down the pipeline decide the same turn a second time.
- property resolves_proposed_turn_stop_frames: bool
Whether any active stop strategy resolves proposed turn stops.
The end-of-turn counterpart to
resolves_proposed_turn_start_frames.
- async process_frame(frame: Frame)[source]
Process an incoming frame to detect user turn start or stop.
The frame is passed to the configured user turn strategies, which are responsible for deciding when a user turn starts or stops and emitting the corresponding events.
- Parameters:
frame – The frame to be processed.