speculation_gate

Decision engine holding a speculative bot response until its turn is confirmed.

class pipecat.turns.speculation_gate.SpeculationState(*values)[source]

Bases: Enum

What the gate is doing with the frames passing through it.

  • OPEN: emitting everything.

  • HOLDING: holding a speculative response until it is confirmed.

  • DROPPING: discarding the rest of a withdrawn speculative response.

OPEN = 'open'
HOLDING = 'holding'
DROPPING = 'dropping'
class pipecat.turns.speculation_gate.SpeculationGate(*, name: str = 'SpeculationGate')[source]

Bases: object

Decides which frames of a speculative bot response may be emitted, and when.

A speculative response is generated from an eager end of turn — a provisional guess that the user has finished talking — so it may answer a transcript the user never actually completed. This holds everything such a response produces until the turn is confirmed, then releases it, or discards it if the guess is withdrawn.

A host calls begin_speculation() when it starts an inference for a turn that may not have ended, and the response that follows is held from its LLMFullResponseStartFrame onward. A UserStoppedSpeakingFrame releases it — the turn it answers has ended — and a UserTurnResponseCancelFrame discards it. Neither names a response: only one speculation is ever in flight, since producing one takes a whole user turn.

Whether an inference is still answering an unconfirmed turn is therefore the gate’s to answer, through is_speculating, rather than something a host tracks alongside it. A host that kept its own copy would have to clear it on every path that settles a speculation, and the two would disagree on the paths it missed.

The gate also tracks whether the turn has ended, because a confirmation can arrive before the inference it confirms — Pipecat dispatches system frames ahead of the queued frames they pass — and a response for a turn already over is not held at all.

Both of those resolving frames reach the gate ahead of anything queued behind them, which is what keeps a withdrawal in front of the turn end that follows it. A withdrawal arriving second would find the response already released.

While holding, everything is held in arrival order except system frames, which are out-of-band throughout Pipecat — and which carry the verdicts the gate is waiting for, so holding them would deadlock it. That includes uninterruptible frames (interruptible False), which are ordered like any other frame; discarding a speculation keeps them and emits them on, since they can belong to work started before it.

This decides rather than processes frames: process() is synchronous and returns the frames its caller should push, in order. A host can therefore push from several tasks at once — every state transition completes without an await for another task to interleave with. LLMService hosts one, gating every response it produces.

Holding is unbounded here. Whoever started the speculation bounds it and withdraws it if it goes unresolved — see EagerUserTurnStopStrategy — so every way a speculation ends reaches this gate as a frame, and the gate never has to abandon one on its own.

Example:

for frame, direction in gate.process(frame, direction):
    await self.push_frame(frame, direction)
__init__(*, name: str = 'SpeculationGate')[source]

Initialize the speculation gate.

Parameters:

name – Label used in log messages, typically the owning service’s.

property state: SpeculationState

What the gate is currently doing with frames passing through it.

property is_speculating: bool

Whether the inference in flight answers an unconfirmed turn.

Set from begin_speculation() until the turn settles it, so it covers the whole inference rather than only the stretch with frames in flight. False means whatever is generating now answers a turn that has ended, and its side effects can be let through. Use state to ask the narrower question of whether frames are being held right now.

begin_speculation(speculation: bool)[source]

Take note of an inference whose turn may not have ended.

Called when the inference starts rather than when its first response frame arrives, so a turn confirmed in between is already accounted for by the time the response shows up.

Parameters:

speculation – Whether the inference answers a turn that may still be open, rather than an ordinary one against a turn that has ended.

process(frame: Frame, direction: FrameDirection) → list[tuple[Frame, FrameDirection]][source]

Decide what a frame passing through the gate releases.

Parameters:
  • frame – The frame to process.

  • direction – The direction the frame travels in.

Returns:

The frames to push, in order. Empty while a frame is held or dropped; longer than one frame when a verdict releases what was held behind it.