perplexity_adapter

Perplexity LLM adapter for Pipecat.

Perplexity’s API uses an OpenAI-compatible interface but enforces stricter constraints on conversation history structure:

  1. Strict role alternation — Messages must alternate between “user”/”tool” and “assistant” roles. Consecutive messages with the same role (e.g. two “user” messages in a row) are rejected with: "messages must be an alternating sequence of user/tool and assistant messages"

  2. No non-initial system messages — “system” messages are only allowed at the start of the conversation. A system message after a non-system message causes: "only the initial message can have the system role"

  3. Last message must be user/tool — The final message in the conversation must have role “user” or “tool”. A trailing “assistant” message causes: "the last message must have the user or tool role"

This adapter transforms the message list to satisfy all three constraints before the messages are sent to Perplexity’s API.

class pipecat.adapters.services.perplexity_adapter.PerplexityLLMAdapter[source]

Bases: OpenAILLMAdapter

Adapter that transforms messages to satisfy Perplexity’s API constraints.

Perplexity’s API is stricter than OpenAI about message structure. This adapter extends OpenAILLMAdapter and applies message transformations to ensure compliance with Perplexity’s constraints (role alternation, no non-initial system messages, last message must be user/tool).

The transformations are applied in get_llm_invocation_params after the parent adapter extracts messages from the LLM context (including any system_instruction prepend).

get_llm_invocation_params(context: LLMContext, *, system_instruction: str | None = None, convert_developer_to_user: bool) OpenAILLMInvocationParams[source]

Get OpenAI-compatible invocation parameters with Perplexity message fixes applied.

Parameters:
  • context – The LLM context containing messages, tools, etc.

  • system_instruction – Optional system instruction from service settings or run_inference. Forwarded to the parent adapter.

  • convert_developer_to_user – If True, convert “developer”-role messages to “user”-role messages. Forwarded to the parent adapter.

Returns:

Dictionary of parameters for Perplexity’s ChatCompletion API, with messages transformed to satisfy Perplexity’s constraints.