base_llm_adapter

Base adapter for LLM provider integration.

This module provides the abstract base class for implementing LLM provider-specific adapters that handle tool format conversion and standardization.

exception pipecat.adapters.llm.base_llm_adapter.LLMContextConversionError(cause: Exception)[source]

Bases: Exception

Raised when converting a universal LLMContext to a provider’s message format fails.

Adapters that transform context messages into a provider-specific format raise this from their conversion routine, wrapping the underlying error (preserved as __cause__). Its message identifies the failure as a context-mapping problem and carries the underlying cause. The corresponding LLM service catches this and surfaces it in the ErrorFrame it pushes upstream.

__init__(cause: Exception)[source]

Initialize the error.

Parameters:

cause – The underlying exception raised during message conversion.

class pipecat.adapters.llm.base_llm_adapter.BaseLLMAdapter[source]

Bases: ABC, Generic[TLLMInvocationParams]

Abstract base class for LLM provider adapters.

Provides a standard interface for converting to provider-specific formats.

Handles:

  • Extracting provider-specific parameters for LLM invocation from a universal LLM context

  • Converting standardized tools schema to provider-specific tool formats.

  • Extracting messages from the LLM context for the purposes of logging about the specific provider.

  • Resolving conflicts between system_instruction and initial system/developer messages in the conversation context.

Subclasses must implement provider-specific conversion logic.

__init__()[source]

Initialize the adapter.

property builtin_tools: dict[str, FunctionSchema]

Built-in tools automatically merged into every inference request.

Keyed by tool name for O(1) lookup, insertion, and removal. The service injects tools here so they are sent transparently on every inference request without the user having to add them to their ToolsSchema.

Returns:

Mutable dict mapping tool name to FunctionSchema.

abstract property id_for_llm_specific_messages: str

Get the identifier used in LLMSpecificMessage instances for this LLM provider.

Returns:

The identifier string.

abstractmethod async get_llm_invocation_params(context: LLMContext, **kwargs) → TLLMInvocationParams[source]

Get provider-specific LLM invocation parameters from a universal LLM context.

An adapter whose provider takes files awaits prepare_file_content() first, so the files its conversion inlines are in the file_resolver’s caches.

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

  • **kwargs – Additional provider-specific arguments that subclasses can use.

Returns:

Provider-specific parameters for invoking the LLM.

abstractmethod to_provider_tools_format(tools_schema: ToolsSchema) → list[Any][source]

Convert tools schema to the provider’s specific format.

Parameters:

tools_schema – The standardized tools schema to convert.

Returns:

List of tools in the provider’s expected format.

abstractmethod get_messages_for_logging(context: LLMContext) → list[dict[str, Any]][source]

Get messages from a universal LLM context in a format ready for logging about this provider.

Parameters:

context – The LLM context containing messages.

Returns:

List of messages in a format ready for logging about this provider.

prefers_raw_file_bytes: bool = False
property file_resolver: FileResolver | None

The resolver that fetches and caches file content for this adapter.

Set once by the owning LLM service. Every conversion this adapter performs — invocation params and logging alike — reads the same resolver caches. Without one, conversion still passes through URLs the provider consumes directly, but fails on any file that would need fetching.

supports_file_url(url: str, mime_type: str) → bool[source]

Whether the provider can be handed url to fetch itself.

This covers both URLs the provider fetches over the public internet and cloud-storage URIs it resolves through its own IAM (e.g. Bedrock reading s3://). A URL the provider can’t consume is instead fetched into the file resolver’s cache and inlined at conversion — see prepare_file_content().

Parameters:
  • url – The file URL from a file_url context content item.

  • mime_type – The file’s MIME type.

Returns:

True if conversion can pass url through to the provider.

async prepare_file_content(context: LLMContext) → None[source]

Fetch the file content this provider will need into the resolver’s caches.

A file_url item whose URL the provider consumes directly (supports_file_url(), and — for http(s) — publicly routable) needs nothing. Any other file_url item is fetched into the file_resolver’s cache, in the form this provider’s conversion reads: raw bytes when prefers_raw_file_bytes, a base64 data URL otherwise. Inline file_base64 items get their decoded bytes cached for raw-bytes providers. The resolver caches by URL, so each file is fetched once no matter how many turns — or how many adapters sharing the resolver — consume it; only the per-provider pass-through decision is re-evaluated here on every run.

An adapter whose conversion inlines files (through inlined_file_content() or decoded_file_bytes()) awaits this at the start of get_llm_invocation_params(). An adapter for a provider that doesn’t take files doesn’t call it, so nothing is fetched for that provider.

Parameters:

context – The LLM context whose messages to resolve.

Raises:

LLMContextConversionError – If a file URL can’t be resolved (refused by the reachability policy, download failure, unresolvable scheme, corrupt base64). The service handles it like any other conversion failure, including invalid-file-message cleanup.

inlined_file_content(file_data: Mapping[str, Any]) → bytes | str | None[source]

Return the content to inline for a file_url item, or None when the URL passes through.

Parameters:

file_data – The item’s file mapping (url, mime_type, …).

Returns:

None when the URL passes through for the provider to fetch itself; otherwise the resolved content from the file_resolver’s caches — raw bytes when prefers_raw_file_bytes, a base64 data URL otherwise.

Raises:

ValueError – If the provider can’t consume the URL and no resolved content is cached (wrapped as LLMContextConversionError by the conversion routine’s caller).

decoded_file_bytes(file_data: Mapping[str, Any]) → bytes[source]

Return a file_base64 item’s decoded bytes, from the resolver’s cache when prepared.

Falls back to decoding inline for conversion-only calls where no resolution has run.

Parameters:

file_data – The item’s file mapping (file_data, …).

create_llm_specific_message(message: Any) → LLMSpecificMessage[source]

Create an LLM-specific message (as opposed to a standard message) for use in an LLMContext.

Parameters:

message – The message content.

Returns:

A LLMSpecificMessage instance.

get_messages(context: LLMContext, *, truncate_large_values: bool = False) → list[Any | LLMSpecificMessage][source]

Get messages from the LLM context, including standard and LLM-specific messages.

Parameters:
  • context – The LLM context containing messages.

  • truncate_large_values – If True, return deep copies of messages with large values replaced by short placeholders.

Returns:

List of messages including standard and LLM-specific messages.

from_standard_tools(tools: Any) → list[Any] | NotGiven | None[source]

Convert tools from standard format to provider format.

Built-in tools are automatically merged into the schema before conversion so that every inference request receives them without the user having to declare them explicitly; when there are no other tools, they are the tool set.

Parameters:

tools – Tools in standard format or provider-specific format.

Returns:

List of tools converted to provider format, or original tools (possibly None) if not in standard format.