file_resolver

Resolution of file URLs in an LLM context into bytes the provider can consume.

A file reaches the context as a URL — pipecat:<id> from the development runner’s upload endpoint, gs:///s3:// from cloud storage, or plain http(s). Whether that URL can be passed to the LLM provider as-is, or must first be fetched by this server and inlined as bytes, depends on the provider (each adapter declares what it can consume) and on who can reach the URL. FileResolver is the fetching half of that decision: it downloads the bytes when the provider can’t, using the configured FileStorage for URLs the storage backend minted and plain HTTP for the rest, guarded by an SSRF reachability policy.

The context itself is never modified: everything the resolver produces — downloaded bytes, base64 data URLs, reachability verdicts — is held in in-memory caches on the resolver, keyed by URL, where adapters read it synchronously during context conversion. The caches make resolved files provider-neutral: LLM services that share one resolver instance (e.g. services switched between mid-session) reuse each other’s downloads, re-deciding only the per-provider question of whether a URL can be passed through.

The resolver is configured on the LLM service (file_resolver=...), which resolves and converts in one step right before each completion — see get_llm_invocation_params().

exception pipecat.utils.file_resolver.FileResolverError[source]

Bases: Exception

Raised when a file URL in the LLM context cannot be resolved to bytes.

class pipecat.utils.file_resolver.FileResolver(*, file_storage: FileStorage | None = None, allowed_url_networks: list[str] | None = None, max_fetch_bytes: int = 52428800, fetch_timeout_secs: float = 30)[source]

Bases: object

Fetches and caches the bytes behind file URLs the LLM provider can’t fetch itself.

Handles three kinds of URL:

  • data: URLs are decoded directly.

  • http(s) URLs are fetched by this server, but only when the resolved address is publicly routable or falls within allowed_url_networks — a client-supplied URL must not be able to point this server at arbitrary private address space (SSRF). Redirects are refused rather than followed.

  • Any other scheme is delegated to file_storage, which resolves the URLs it minted (pipecat:<id> for LocalFileStorage; a custom backend resolves whatever save() returned, e.g. gs:// with the deployment’s own credentials).

Each URL is fetched once and served from the cache afterwards: a URL is assumed to identify one immutable piece of content, the same assumption the rest of the web’s caches make. Content that changes should live at a new URL (uploads through the development runner mint a fresh URL per upload; a cache-busting query string works for external URLs), or use forget() when the application knows a URL’s content changed. Note the flip side for URLs a provider consumes directly: the provider fetches those itself, on its own schedule, so their freshness is out of this resolver’s hands entirely.

A stored file whose backend sets delete_after_load is deleted only after its bytes are cached, so the cache entry becomes the surviving copy — which is why LLM services that may consume the same files (e.g. services switched between mid-session) should share one resolver instance rather than each constructing their own. Scope a resolver to one session: an instance shared across sessions would hold every session’s files in memory and stretch the one-fetch-per-URL assumption across all of them.

Subclass and override fetch() to support additional schemes or credentialed fetches beyond what the storage backend provides.

__init__(*, file_storage: FileStorage | None = None, allowed_url_networks: list[str] | None = None, max_fetch_bytes: int = 52428800, fetch_timeout_secs: float = 30)[source]

Initialize the file resolver.

Parameters:
  • file_storage – Storage backend used to resolve URLs it minted (e.g. pipecat:<id> from the development runner’s upload endpoints — pass runner_args.file_storage there). Without it, only data: and http(s) URLs can be resolved.

  • allowed_url_networks – CIDR ranges (e.g. ["10.0.0.0/8"]) this server is trusted to fetch from, in addition to the public internet. An http(s) URL that resolves outside both is refused. Defaults to the PIPECAT_ALLOWED_FILE_URL_NETWORKS env var (comma-separated).

  • max_fetch_bytes – Maximum size of a fetched file.

  • fetch_timeout_secs – Total timeout for an http(s) fetch.

property file_storage: FileStorage | None

The storage backend used to resolve URLs it minted, if any.

cached_bytes(url: str) → bytes | None[source]

Return the cached bytes for url, or None if it hasn’t been fetched.

cached_data_url(url: str) → str | None[source]

Return the cached data: form for url, or None if not prepared.

A data: URL is its own data-URL form.

cached_reachability(url: str) → UrlReachability | None[source]

Return the cached reachability verdict for url, or None if unclassified.

forget(url: str) → None[source]

Drop everything cached for url, forcing a re-fetch on next use.

For applications that know a URL’s content has changed. A forgotten delete_after_load upload can’t be re-fetched — its stored copy was deleted when first cached — so forgetting one makes it unresolvable.

Parameters:

url – The URL whose cached content and reachability to drop.

async classify(url: str) → UrlReachability[source]

Classify who can reach an http(s) URL, honoring allowed_url_networks.

Classified once per URL; later calls return the cached verdict.

async fetch(url: str) → bytes[source]

Return the bytes behind url, fetching and caching them on first use.

Parameters:

url – A data: URL, an http(s) URL, or a URL minted by the configured storage backend. A storage-resolved file is deleted after a successful load when the backend sets delete_after_load — the bytes are cached first, so the cache entry becomes the surviving copy.

Raises:

FileResolverError – If the URL is refused by the reachability policy, exceeds the size limit, fails to download, or has a scheme nothing is configured to resolve.

async ensure_data_url(url: str, mime_type: str) → str[source]

Return the data: form of url’s bytes, encoding and caching it on first use.

For adapters whose provider consumes base64 rather than raw bytes, so the encode happens once, off the event loop, rather than on every context conversion.

Parameters:
  • url – A URL whose bytes are cached or fetchable — see fetch().

  • mime_type – The file’s MIME type, used in the data URL.

Raises:

FileResolverError – If the bytes can’t be fetched.