Skip to main content

Overview

BaseWorker is the abstract base class that all agents inherit from. It handles agent lifecycle, parent-child relationships, bus communication, and job coordination. A BaseWorker connects to a WorkerBus, registers itself in the shared registry, accepts activation/deactivation, and exchanges job requests/responses with other agents. Agents that need to run a Pipecat pipeline use a concrete subclass like LLMWorker, while BaseWorker itself operates purely through bus messages (e.g. coordinators, orchestrators).

Configuration

str | None
default:"None"
Unique name for this agent, used for bus message routing and registry lookup. If None, an auto-generated name is used (useful for instances that don’t participate in inter-agent communication).
bool
default:"True"
Whether the agent starts out accepting bus messages (see active property). Starting one inactive is for multi-worker setups where workers take turns: it stays out of the way, doing nothing, until another worker or the application activates it.
bool
default:"True"
Whether to warn about tasks left running when the worker finishes. Only applies when the worker owns its task manager; a worker sharing the runner’s task manager leaves the report to the runner.
BaseTaskManager | None
default:"None"
Optional task manager for handling asyncio tasks.
The bus is not passed in the constructor. It is provided by the runner when the agent is registered with WorkerRunner.add_workers(), which calls attach() internally.

Properties

bus

The bus this agent is attached to. Raises RuntimeError if accessed before attach() has been called.

worker_runner

The runner this agent is attached to. Use it to reach another worker on the same runner by name, rather than having the application pass the object in through app_resources:
Raises RuntimeError if accessed before attach() has been called. The same accessor is on FrameProcessor and on FunctionCallParams, so a processor or a tool handler can reach the runner too.

active

Whether this agent is accepting bus messages. An active agent takes everything addressed to it. An inactive one takes only activation, deactivation, end or cancel messages, so no job request, frame or UI event reaches it and none of its on_bus_message handling runs. It matters mainly in multi-worker setups, where a worker is put out of the way while the others carry on. Registry watches sit outside this: @worker_ready handlers fire from WorkerRegistry whatever this returns, because they never travel over the bus.

activation_args

The arguments from the most recent activation, or None if the agent is inactive. The value is cleared when the agent is deactivated.

parent

The name of the parent agent, or None if this is a root agent.

registry

The shared agent registry this agent is attached to. Raises RuntimeError if accessed before attach() has been called.

started_at

Unix timestamp when this agent became ready, or None if not yet started.

bridged

Whether this agent is bridged onto the bus (receives pipeline frames from the bus). Always False on BaseWorker; subclasses such as PipelineWorker override it.

children

The list of child agents added via add_workers().

active_jobs

Active job requests this agent is currently working on, keyed by job_id.

job_groups

Active job groups launched by this agent, keyed by job_id.

Lifecycle Hooks

Override these methods to react to lifecycle events. Always call super() when overriding.

on_activated

Called when this agent is activated. Override to react to activation.

on_deactivated

Called when this agent is deactivated.

on_worker_ready

Called when another agent is ready to receive messages. For local root agents this fires automatically. For remote agents it fires only for agents watched via watch_workers(). For child agents it fires only on the parent.

on_worker_failed

Called when a child agent reports an error.

Agent Management

add_workers

Register one or more child agents under this parent. Each child’s lifecycle (end, cancel) is automatically managed by this parent agent. By default the children are also watched, so the parent receives on_worker_ready when each one starts.

activate_worker

Activate an agent by name. The target agent’s on_activated hook will be called with the provided arguments. The target need not be active already: an activation reaches an inactive agent, which is what lets it become active. To hand off (deactivate this agent and activate the target), pass deactivate_self=True.

deactivate_worker

Deactivate an agent by name. The target agent’s on_deactivated hook will be called.

watch_workers

Request notification when one or more agents register. For each name: if the agent is already registered, on_worker_ready fires immediately. Otherwise it fires when the agent eventually registers.

end

Request a graceful end of the session.

cancel

Request an immediate cancellation of all agents.

wait

Wait for this agent to finish.

Job Coordination

request_job

Send a job request to a single agent (fire-and-forget). Waits for the agent to be ready before sending the request. Does not wait for the job to complete; use callbacks (on_job_response, on_job_completed) or job() for that. Returns: The generated job_id.

job

Create a single-agent job context manager. Waits for the agent to be ready, sends a job request, and waits for the response on exit. Supports async for inside the block to receive intermediate events. Returns: A JobContext to use with async with.

request_job_group

Send a job request to multiple agents (fire-and-forget). Waits for all agents to be ready before sending requests. Returns: The generated job_id shared by all agents in the group.

job_group

Create a job group context manager. Waits for agents to be ready, sends job requests, and waits for all responses on exit. Supports async for inside the block to receive intermediate events. Returns: A JobGroupContext to use with async with.

cancel_job_group

Cancel a running job group. Cancellation the worker decides on itself — on shutdown, on a timeout, or through cancel_on_error — goes through this method and is never refused. For a request from outside the worker, use request_cancel_job_group.

request_cancel_job_group

Cancel a running job group on behalf of something outside the worker — a client UI, an operator endpoint, anything reaching in. The request is honored only for a group dispatched with JobGroupParams(cancellable=True). Returns: Whether the group was cancelled.

request_job_update

Request a progress update from a worker agent.

send_job_response

Send a job response back to the requester. After sending, the job is removed from the set of active jobs.

send_job_update

Send a progress update to the requester.

send_job_stream_start

Begin streaming job results back to the requester.

send_job_stream_data

Send a streaming chunk to the requester.

send_job_stream_end

End the current stream and mark this agent’s job as complete.

create_job_group_and_request_job

Wait for agents to be ready, create a job group, and send requests. Does not wait for the group to complete; call group.wait() or use job_group() for that. Used internally by job() and job_group(). Returns: The created JobGroup.

Job Hooks

Override these methods to handle job events. Always call super() when overriding.

on_job_request

Called when this agent receives a job request that does not match a named @job handler. Override to perform work. Use send_job_update() to report progress and send_job_response() to return results.

on_job_response

Called when a worker agent sends a response. Override to process individual results as they arrive.

on_job_update

Called when a worker agent sends a progress update.

on_job_update_requested

Called when the requester asks for a progress update. Override to send back a progress update via send_job_update().

on_job_completed

Called when all agents in a job group have responded.

on_job_error

Called when a job group is cancelled due to a worker error. Fires when a worker responds with ERROR or FAILED status and cancel_on_error is set.

on_job_stream_start

Called when a worker agent begins streaming.

on_job_stream_data

Called for each streaming chunk from a worker agent.

on_job_stream_end

Called when a worker agent finishes streaming.

on_job_cancelled

Called when this agent’s job is cancelled by the requester. Override to clean up resources or stop in-progress work.

Bus

accepts_bus_message

Whether this agent should be handed this message. Checked by the bus before every delivery. Returning False drops the message for this agent alone; others still receive it. An inactive agent takes only activation, deactivation, end or cancel messages. Override to implement custom filtering. Returns: True to deliver the message, False to drop it.

send_bus_message

Send a message on the bus.

send_bus_error_message

Report an error on the bus. Child agents send a local-only message to the parent. Root agents broadcast over the network.

on_bus_message

Called for every bus message after built-in lifecycle handling. Override to handle custom message types.

Decorators

@job

Mark an agent method as a job handler. Decorated methods are automatically collected at initialization and dispatched when matching job requests arrive. Each request runs in its own asyncio task so the bus message loop is never blocked.
The decorator requires a name argument:
str
required
Job name to match. The handler only receives requests with a matching name.
bool
default:"False"
When True, requests with this name run one at a time in FIFO order. When False (the default), multiple requests run concurrently. The wait time counts against the requester’s timeout.
Job handler methods receive a BusJobRequestMessage.

@worker_ready

Mark a method as a handler for a specific agent becoming ready. Decorated methods are collected at initialization; when the agent starts, it calls watch_workers for each handler, and the method is called when the watched agent registers.
str
required
The name of the agent to watch.
The handler receives a WorkerReadyData instance.