Skip to content

API Reference parrot.tools.compression.codecs.columnar

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.tools.compression.codecs.columnar

View source

columnar codec — row-oriented list[dict] -> split columnar form.

The codec that pays for the feature (spec Sec 3 Module 4). The most expensive known case is DatabaseQueryToolkit: QueryResult.rows is list[dict[str, Any]], so N column names repeat in each of M rows — for 500 rows x 12 columns that is ~6,000 repetitions of field names the model only needs to see once.

Output shape is deliberately identical to PandasTable (parrot/bots/data.py:70), which the PandasAgentResponse prompt already teaches the LLM to read — no new prompt engineering needed. Its shape is NOT imported here (that would invert the dependency direction); only matched.

This pure-Python implementation is the executable specification: the optional Rust path (TASK-1955, parrot_codec) must pass this exact test suite byte-for-byte.

Rust dispatch (TASK-1955): only a bytes/str result — an ALREADY-serialized JSON row array (or QueryResult-shaped JSON object) — is eligible for the Rust transform, in a single FFI crossing. A native dict/list result NEVER crosses the FFI boundary: per-row extract() under the GIL can be slower than pure Python, so crossing with materialized Python objects would be a pessimization (spec Sec 3 Module 6). The Rust extension is entirely optional (G8) — absent, this module logs exactly once at debug and every call takes the pure-Python path below.

Class ColumnarCodec

Row-oriented list[dict] -> column-split {columns, rows, constants} form.

Transformations:

  1. Column extraction — keys hoisted once into columns (stable, first-seen order across rows — never set iteration order, G4); values become positional lists aligned to columns.
  2. Constant-column factoring — a column whose value is identical across every row (when there is more than one row) moves to constants and drops out of columns/rows.
  3. Null-column elision — an all-null column is dropped entirely (logged for discoverability). NORMAL/AGGRESSIVE only: at MINIMAL (the lossless-only level, and what the stage caps NORMAL down to when no tee is available, G3) an all-null column is factored into constants as None instead of dropped, so the outcome is never lossy.

Passthrough guards, each a "no gain" outcome:

  • fewer than min_rows rows (default 20, per-tool configurable via params) — the ORIGINAL object is returned unchanged.
  • heterogeneous rows (key union/intersection ratio > 1.5, configurable) — null-key elision only, no columnarization (unchanged passthrough at MINIMAL: per-row null-key elision is lossy).
  • any nested dict/list value — no flattening; null-key elision only (unchanged passthrough at MINIMAL).
  • non-list[dict] (and non-QueryResult-shaped) input — unchanged passthrough.

Accepts a bare list[dict] OR a QueryResult-shaped dict (a dict with a rows key holding list[dict]): only rows is transformed, sibling fields (driver, row_count, columns, execution_time_ms, ...) are left untouched. Also accepts the same two shapes pre-serialized as bytes/str (JSON) — the ONLY input form eligible for the optional Rust transform (see module docstring).

Attributes:

Attribute Type Description
codec_name str

Method ColumnarCodec.compress

Compress result. See class docstring for the transformation and passthrough-guard rules.

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

Parameter Type Default Description
result Any The unwrapped tool result payload — a list[dict], a QueryResult-shaped dict, or either pre-serialized as bytes/str JSON.
level FilterLevel Effective :class:FilterLevel. NONE is a strict passthrough. MINIMAL restricts the codec to lossless transformations only (column split + constant factoring; no null elision of any kind), so the outcome is never lossy and never needs the tee.
params dict[str, Any] Recognized keys: min_rows (int, default 20), heterogeneity_ratio (float, default 1.5).

Returns: CompressionOutcome — class:CompressionOutcome. lossy is True whenever constant factoring or null elision dropped anything (arms the tee, TASK-1953); False for a pure shape change with no information dropped, or any passthrough. payload is always a native Python object (never re-encoded to bytes/str, even when the input was bytes/str).

Alias CompressionOutcome

Re-exported from parrot.tools.compression.protocol.CompressionOutcome.

Alias FilterLevel

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

Alias lazy_import

Re-exported from parrot._imports.lazy_import.

Attribute logger

Alias register_codec

Re-exported from parrot.tools.compression.protocol.register_codec.

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