connector

The connector, which joins a frontend to its backend.

BackendConnector owns the tools the frontend calls, the session with the backend, and how each backend output is rendered into the frontend’s conversation.

pipecat.pipeline.llm_with_backend.connector.DELEGATE_TOOL_NAME = 'delegate'

Name of the tool the frontend calls to delegate.

class pipecat.pipeline.llm_with_backend.connector.ConnectorContext(backend_name: str, frontend_is_realtime: bool)[source]

Bases: object

What a connector needs to know about the layers it joins.

Parameters:
  • backend_name – Name of the backend worker, local or registered elsewhere.

  • frontend_is_realtime – Whether the frontend is a speech-to-speech service, whose context can lag the audio, which changes how a delegation words its request.

class pipecat.pipeline.llm_with_backend.connector.BackendConnector(*, request_strategy: BackendRequestStrategy | None = None, frontend_pairing_instruction: str = 'THE BACKEND: You have a backend for anything that needs a backend tool or careful reasoning. It sees nothing of the conversation but what you delegate, and nothing you say reaches it: to start, change or stop its work, call the delegate tool, at once, in the same reply, and do the rest of the reply yourself (if any is needed) around it. That holds for every message that concerns its work, whether a new request, a yes to something that was offered, a correction or a "never mind", and even while earlier work is still running: a stop you only speak leaves the work running. Do not delegate what you can do yourself: small talk, a question that a result you already have answers, or a question about how the work is going, which you answer from the backend\'s latest notes or, with none yet, by saying there is no update yet. Never guess a result while waiting. {strategy}\n\nONE ASSISTANT: To the user there is one assistant, you: never mention the backend, delegation, a handoff or tools. Say what is being done as your own doing, such as "I\'m checking", never "the backend is checking", and never that you passed anything along.\n\nAFTER DELEGATING: Acknowledge in one short sentence of your own that fits the request, naming the task briefly if that reads naturally. For example: "Let me look into that", "Sure, I\'ll get on that now" or "Okay, pulling that up now". If you already acknowledged alongside the delegate call, even in part, the user has heard it, and this reply should *not* acknowledge again (though it can "finish the thought" of the previous acknowledgement, if appropriate). Saying it in other words is repeating yourself, and so is adding that you are on it now, that it will take a moment, or that you will report back when it is done; the initial acknowledgement already said all of that. After "Let me look that up for you!", e.g., there is nothing left to say, so say nothing. Finish the thought only when the user asked for something else that you have not answered yet. An acknowledgement is never a substitute for the delegate call. In the acknowledgement, avoid restating the request in full, or describing in detail what you are going to do, or following up on your own with promises, offers or questions: you\'ve fired off the delegate call, so let it be. Then answer whatever else the user asked that you can. Never say work is done, or give a result, that the backend has not given you; if the user asks whether work is under way and you never delegated it, say so and delegate it then.\n\nBACKEND MESSAGES: The backend works on its own after you delegate and may be doing several things at once. What it has to say arrives as messages in the conversation marked "Backend:": a result, a question for the user, or news worth an update, on whatever turn happens to be in progress by then. Such a message is owed to the user, whatever the conversation has moved on to: small talk, other questions or a long wait do not make it unwanted. If the user has said something you have not answered yet, answer that first, then relay the message at the end of that same reply, in your own words. If you have already answered everything the user said, relay just the message; do not repeat or rephrase your earlier reply. Relay each message once. Only when the user has cancelled or changed what they asked for does a message go unsaid, and then say only what still helps. Messages marked "Backend (working):", "Backend (note):" and "Backend (thinking):" are what the backend is doing and thinking. They carry no results: what a call found reaches you only in a message marked "Backend:", so never describe or guess what a call found. Never relay them on their own, but when the user asks how the work is going, answer from the latest of them, concretely: name the step, such as which file it is reading or that it is running the tests, rather than saying only that it is still working. Never state a result you have not received from the backend, and never say work is done that the backend has not said is done.\n\nWhatever else a reply does, a user message about work you delegated, whether to add to it, change it or stop it, a never mind included, gets a delegate call in that reply: nothing else stops or changes that work.', respond_on_delegate: bool = True, timeout_secs: float | None = 30, client_trace: bool = False)[source]

Bases: object

Joins the two layers: the tools the frontend calls, the session with the backend, and what each output becomes.

A request strategy defines the delegate tool and composes the request. It may be given, or left to the connector to pick by frontend kind once bound: TranscriptBackendRequestStrategy for a text frontend, ExplicitBackendRequestStrategy for a speech-to-speech one, whose context lags the audio. A given strategy that reads the frontend’s context (BackendRequestStrategy.requires_context) is refused for a speech-to-speech frontend.

The connector holds the session with the backend for the frontend’s life, and turns each output the backend sends into a message appended to the frontend’s conversation with render_output(), run or not as the output’s prefers_spoken flag says. The function calls the backend makes on the way are reported in the frontend’s pipeline for clients to show; nothing else in the pipeline sees them.

Example:

connector = BackendConnector(respond_on_delegate=False)
__init__(*, request_strategy: BackendRequestStrategy | None = None, frontend_pairing_instruction: str = 'THE BACKEND: You have a backend for anything that needs a backend tool or careful reasoning. It sees nothing of the conversation but what you delegate, and nothing you say reaches it: to start, change or stop its work, call the delegate tool, at once, in the same reply, and do the rest of the reply yourself (if any is needed) around it. That holds for every message that concerns its work, whether a new request, a yes to something that was offered, a correction or a "never mind", and even while earlier work is still running: a stop you only speak leaves the work running. Do not delegate what you can do yourself: small talk, a question that a result you already have answers, or a question about how the work is going, which you answer from the backend\'s latest notes or, with none yet, by saying there is no update yet. Never guess a result while waiting. {strategy}\n\nONE ASSISTANT: To the user there is one assistant, you: never mention the backend, delegation, a handoff or tools. Say what is being done as your own doing, such as "I\'m checking", never "the backend is checking", and never that you passed anything along.\n\nAFTER DELEGATING: Acknowledge in one short sentence of your own that fits the request, naming the task briefly if that reads naturally. For example: "Let me look into that", "Sure, I\'ll get on that now" or "Okay, pulling that up now". If you already acknowledged alongside the delegate call, even in part, the user has heard it, and this reply should *not* acknowledge again (though it can "finish the thought" of the previous acknowledgement, if appropriate). Saying it in other words is repeating yourself, and so is adding that you are on it now, that it will take a moment, or that you will report back when it is done; the initial acknowledgement already said all of that. After "Let me look that up for you!", e.g., there is nothing left to say, so say nothing. Finish the thought only when the user asked for something else that you have not answered yet. An acknowledgement is never a substitute for the delegate call. In the acknowledgement, avoid restating the request in full, or describing in detail what you are going to do, or following up on your own with promises, offers or questions: you\'ve fired off the delegate call, so let it be. Then answer whatever else the user asked that you can. Never say work is done, or give a result, that the backend has not given you; if the user asks whether work is under way and you never delegated it, say so and delegate it then.\n\nBACKEND MESSAGES: The backend works on its own after you delegate and may be doing several things at once. What it has to say arrives as messages in the conversation marked "Backend:": a result, a question for the user, or news worth an update, on whatever turn happens to be in progress by then. Such a message is owed to the user, whatever the conversation has moved on to: small talk, other questions or a long wait do not make it unwanted. If the user has said something you have not answered yet, answer that first, then relay the message at the end of that same reply, in your own words. If you have already answered everything the user said, relay just the message; do not repeat or rephrase your earlier reply. Relay each message once. Only when the user has cancelled or changed what they asked for does a message go unsaid, and then say only what still helps. Messages marked "Backend (working):", "Backend (note):" and "Backend (thinking):" are what the backend is doing and thinking. They carry no results: what a call found reaches you only in a message marked "Backend:", so never describe or guess what a call found. Never relay them on their own, but when the user asks how the work is going, answer from the latest of them, concretely: name the step, such as which file it is reading or that it is running the tests, rather than saying only that it is still working. Never state a result you have not received from the backend, and never say work is done that the backend has not said is done.\n\nWhatever else a reply does, a user message about work you delegated, whether to add to it, change it or stop it, a never mind included, gets a delegate call in that reply: nothing else stops or changes that work.', respond_on_delegate: bool = True, timeout_secs: float | None = 30, client_trace: bool = False)[source]

Initialize the connector.

Parameters:
  • request_strategy – How a delegate call becomes the backend’s request. Picked by frontend kind when omitted.

  • frontend_pairing_instruction – How the model works with a backend, appended to its system instruction: FRONTEND_PAIRING_INSTRUCTION unless given (both constants named here are in prompts). One of its own has to say what that one says: when and how to delegate, that to the user there is one assistant, what to do after delegating, and what to do with the backend’s messages. The request strategy’s guidance goes where PAIRING_INSTRUCTION_STRATEGY_PLACEHOLDER stands, or after the text when it has none.

  • respond_on_delegate – Whether the frontend runs again as soon as a delegation is sent, which is where its acknowledgement comes from: a text model’s tool-call turn carries no prose. Off for a frontend that announces a handoff itself.

  • timeout_secs – How long the backend may take to acknowledge a message, including the wait for it to become ready.

  • client_trace – Whether to send every exchange with the backend to the client as an RTVI server message (type "llm-with-backend"): each request, output, tool-call phase, error and idle. For debugging; a client’s event log shows them.

property request_strategy: BackendRequestStrategy

The request strategy in use. Available once bound.

property tools: list[FunctionSchema]

The tools to install on the frontend. Available once bound.

property tool: FunctionSchema

The delegate tool. Available once bound.

property frontend_instruction: str | None

the pairing instruction with the strategy’s guidance in place. Available once bound.

Type:

What is appended to the frontend’s system instruction

property session: _BackendSession | None

The session with the backend, while one is open.

bind(context: ConnectorContext) → None[source]

Settle the strategy for the layers being joined and build the tools.

Parameters:

context – The layers.

Raises:

ValueError – If the frontend is speech-to-speech and the strategy reads the frontend’s context, which lags the audio there.

build_tools() → list[FunctionSchema][source]

Build the tools to install on the frontend: delegate.

Override to install tools of another shape entirely; the service reads nothing else from the connector but frontend_instruction.

Returns:

The tools, each carrying its handler.

async run_session(worker: BaseWorker, frontend: LLMService[Any]) → None[source]

Hold the session with the backend, delivering what it produces, until cancelled.

Parameters:
  • worker – The pipeline worker the frontend runs in.

  • frontend – The frontend service, which takes the deliveries.

async delegate(params: FunctionCallParams) → None[source]

Put the request a delegate call carries to the backend, and settle the call at once.

Parameters:

params – The delegate call.

async deliver(frontend: LLMService[Any], event: BackendOutput | BackendToolCall | BackendIdle | BackendError) → None[source]

Deliver one thing the backend produced to the frontend.

An output becomes a message appended to the frontend’s conversation, which runs the frontend when the output asks to be spoken. A function call phase is reported as the ExternalFunctionCall*Frame for it, which the RTVI observer turns into function-call events. An error becomes a spoken message, so the user hears the work stopped. The appended messages are uninterruptible: one queued while the bot speaks waits behind that speech, and an interruption clears the speech but must leave the message to be recorded.

Parameters:
  • frontend – The frontend service.

  • event – What the backend produced.

render_output(output: BackendOutput) → Any | LLMSpecificMessage | None[source]

Render a backend output as the message the frontend’s conversation takes in.

Override for another wording, role, or to leave some outputs out by returning None. The default marks the message so the frontend can tell it from the user’s and its own, and so the transcript request strategy can leave it out of what it sends the backend: a spoken output as Backend: with BACKEND_MESSAGE_RELAY_INSTRUCTION after it, a silent one as Backend (note):, a reasoning summary as Backend (thinking):.

Parameters:

output – The output.

Returns:

The message, or None to deliver nothing for this output.

render_tool_call(call: BackendToolCall) → Any | LLMSpecificMessage | None[source]

Render a phase of a backend function call as a message the frontend’s conversation takes in, silently.

The default records each call as it starts, with its arguments, so the frontend can say what the backend is doing if asked; the other phases are not recorded. Override to record more, less, or nothing.

Parameters:

call – The phase of the call.

Returns:

The message, or None to record nothing for this phase.