Skip to content

API Reference parrot.tools.compression.protocol

github-actions[bot] edited this page Nov 10, 2026 · 4 revisions

parrot.tools.compression.protocol

View source

Frozen contract for the tool-result compression pipeline.

Defines :class:CompressionOutcome (the return value of every codec), the :class:ResultCompressor Protocol every codec must satisfy, and the process-wide codec-class registry (:func:register_codec, :func:get_codec, :func:known_codecs).

This module has no dependency on the rest of parrot.tools — it exists so third-party packages can implement ResultCompressor without importing anything beyond stdlib/pydantic (G6), and so this package can be imported without pulling in parrot.tools.manager.

Class CompressionOutcome

Result of a single codec's compress() call.

Fields:

Field Type Default Description
payload Any The compressed result (or the original payload, unchanged, when the codec passed through).
lossy bool True when the transformation is not fully reversible from payload alone — the caller must tee the original payload to working memory so it stays recoverable (G3).
bytes_before int Serialized size of the input, in bytes.
bytes_after int Serialized size of payload, in bytes.
est_tokens_saved int bytes/4 heuristic estimate of tokens saved. Approximate by design — no tokenizer is available to the pipeline.
codec_name str Name of the codec that produced this outcome.

Alias FilterLevel

Re-exported from parrot.tools.compression.levels.FilterLevel.

Class ResultCompressor

Structural contract every compression codec must satisfy.

A plain Protocol (not an ABC) so third-party codecs need no parrot-specific base class (G6, extensibility without touching core Python).

Attributes:

Attribute Type Description
codec_name str

Method ResultCompressor.compress

Compress result at the given level.

Must be synchronous and deterministic (G4 — no LLM, no nondeterministic source): the inline path has a sub-millisecond budget and must never await. Off-loop execution is the caller's decision (the latency-budget router), not the codec's.

compress(result: Any, *, level: FilterLevel, params: dict[str, Any]) -> CompressionOutcome

Parameter Type Default Description
result Any The unserialized tool result to compress.
level FilterLevel The effective :class:FilterLevel to apply.
params dict[str, Any] Codec-specific parameters resolved from configuration.

Returns: CompressionOutcome — class:CompressionOutcome describing the transformation.

Function get_codec

Look up a registered codec class by name.

get_codec(name: str) -> type | None

Parameter Type Default Description
name str The codec's codec_name.

Returns: type | None — The registered class, or None if no codec is registered under name.

Function known_codecs

Return the set of currently registered codec names.

known_codecs() -> frozenset[str]

Returns: frozenset[str] — A frozen snapshot of registered codec_name values, used by the TOML config loader for load-time validation of codec entries.

Attribute logger

Function register_codec

Class decorator that registers a codec class by its codec_name.

register_codec(cls: type) -> type

Returns: type — cls unchanged (decorator pattern).

Raises:

  • ValueError — If a codec with the same codec_name is already registered.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally