llm_worker
LLM worker with tool registration.
Provides the LLMWorker class that extends PipelineWorker with an LLM pipeline and automatic tool registration.
- class pipecat.workers.llm.llm_worker.LLMWorkerActivationArgs(metadata: dict | None = None, messages: list | None = None, run_llm: bool | None = None)[source]
Bases:
WorkerActivationArgsActivation arguments for LLM workers.
- Parameters:
messages – LLM context messages to inject on activation.
run_llm – Whether to run the LLM after appending messages. Defaults to True when
messagesis set.
- class pipecat.workers.llm.llm_worker.LLMWorker(name: str | None = None, *, llm: LLMService[Any], pipeline: Pipeline | None = None, active: bool = False, bridged: tuple[str, ...] | None = None, defer_tool_frames: bool = True)[source]
Bases:
PipelineWorkerWorker with an LLM pipeline and automatic tool registration.
Methods decorated with
@toolare registered as direct functions on the LLM and tracked so that frames queued during tool execution can be deferred until all tools complete.Example:
class MyTask(LLMWorker): @tool async def my_function(self, params, arg: str): ... worker = MyTask("worker", bus=bus, llm=OpenAILLMService(api_key="..."))
- __init__(name: str | None = None, *, llm: LLMService[Any], pipeline: Pipeline | None = None, active: bool = False, bridged: tuple[str, ...] | None = None, defer_tool_frames: bool = True)[source]
Initialize the LLMWorker.
- Parameters:
name – Unique name for this worker on the bus. Auto-generated when omitted; give one when other workers address this one by name, as they must when it runs in another process.
llm – The LLM service.
@tooldecorated methods are automatically registered on it.pipeline – Optional pipeline override. When
None, defaults toPipeline([llm]). Subclasses can pass a custom pipeline that wraps the LLM with additional processors.active – Whether the worker starts active. Defaults to False.
bridged – Bridge configuration forwarded to
PipelineWorker. Pass()to wrap the LLM pipeline with bus edge processors so it can exchange frames with another bridged worker.defer_tool_frames – Whether to defer frames queued during tool execution until all tools complete. Defaults to True.
- property llm: LLMService
The LLM service this worker wraps.
- async on_activated(args: dict | None) None[source]
Configure the LLM with tools and activation messages.
- Parameters:
args – Optional activation arguments with messages to append.
- async queue_frame(frame: Frame, direction: FrameDirection = FrameDirection.DOWNSTREAM) None[source]
Queue a frame, holding it if a tool handler queued it.
A frame queued from inside one of this worker’s
@toolhandlers, or from anything that handler awaits, is held and delivered once the last tool finishes. Frames from anywhere else are queued immediately: the worker’s own traffic and frames arriving over the bus, which run outside any handler’s context, and frames a handler on a different worker queues here, which this worker would never release.- Parameters:
frame – Any
Frameto deliver.direction – Direction the frame should travel. Defaults to
FrameDirection.DOWNSTREAM.
- build_tools() list[source]
Return the tools for this worker’s LLM.
By default, returns all methods decorated with
@tool. Override to provide additional or different tools.- Returns:
List of tool functions.
- async end(*, reason: str | None = None, messages: list | None = None, result_callback: Callable[[...], Any] | None = None) None[source]
Request a graceful end of the session.
When called from a
@toolhandler, deliver the function call result first withawait params.result_callback(result): the LLM output it triggers is delivered before the session ends.- Parameters:
reason – Optional human-readable reason for ending.
messages –
Optional LLM messages to inject and speak before ending. The LLM runs immediately so the output is delivered before the session terminates.
Deprecated since version 1.8.0: Call
params.result_callback(result)beforeend()instead. Will be removed in 2.0.0.result_callback –
The
result_callbackfrom FunctionCallParams.Deprecated since version 1.8.0: Call
params.result_callback(result)beforeend()instead. Will be removed in 2.0.0.
- async activate_worker(worker_name: str, *, args: WorkerActivationArgs | None = None, deactivate_self: bool = False, messages: list | None = None, result_callback: Callable[[...], Any] | None = None) None[source]
Activate another worker, draining this worker’s pipeline to hand over.
When called from a
@toolhandler, deliver the function call result first withawait params.result_callback(result): the output it triggers is delivered before the target is activated. The handover itself waits until the tool call asking for it has finished.- Parameters:
worker_name – The name of the worker to activate.
args – Optional
WorkerActivationArgsforwarded to the target worker’son_activatedhandler.deactivate_self – Whether to deactivate this worker before activating the target. Deactivating this worker drains its pipeline first; staying active does not. A worker that stays active and wants to drain anyway can call
flush_pipeline()before this.messages –
Optional LLM messages to inject and deliver before activating the target. The LLM runs immediately so the output is delivered before the transfer completes.
Deprecated since version 1.8.0: Call
params.result_callback(result)beforeactivate_worker()instead. Will be removed in 2.0.0.result_callback –
The
result_callbackfrom FunctionCallParams.Deprecated since version 1.8.0: Call
params.result_callback(result)beforeactivate_worker()instead. Will be removed in 2.0.0.
- async process_deferred_tool_frames(frames: list[tuple[Frame, FrameDirection]]) list[tuple[Frame, FrameDirection]][source]
Process deferred frames before they are flushed.
Called after all in-flight tools complete, before the deferred frames are queued into the pipeline. Override to inspect, modify, reorder, or filter the frames.
- Parameters:
frames – The deferred frames collected during tool execution.
- Returns:
The frames to queue. Return the list as-is for default behavior.