Skip to content

API Reference parrot.stores.utils.contextual

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

parrot.stores.utils.contextual

View source

Contextual embedding header helper.

Builds a deterministic, LLM-free contextual header from a Document's metadata['document_meta'] sub-dict and prepends it to the chunk text for embedding only. No I/O, no ML dependencies — stdlib + Pydantic.

Spec: FEAT-127 — Metadata-Driven Contextual Embedding Headers.

Attribute ContextualTemplate

A template is either a str (format-map style) or a Callable that receives the raw document_meta dict and returns the full embedded text.

Attribute DEFAULT_MAX_HEADER_TOKENS

Safety cap: whitespace-tokenised header words, not sub-word tokens.

Type: int

Attribute DEFAULT_TEMPLATE

Default template shipped with the feature.

Empty / None fields are collapsed (no orphan pipes). Override at the store level via contextual_template= kwarg.

Type: str

Alias Document

Re-exported from parrot.stores.models.Document.

Attribute KNOWN_PLACEHOLDERS

Recognised template placeholders (informational — not enforced at runtime).

Function build_contextual_text

Build the text that will be embedded plus the header used.

Reads document.metadata['document_meta'] (canonical, from metadata-standardisation). Renders the template, dropping empty fields and collapsing the resulting separators. Caps the header at max_header_tokens (whitespace-tokenised approximation — no real tokeniser dependency; the cap is a safety belt, not an exact limit).

This function is a pure function: no I/O, no logging, no side-effects.

build_contextual_text(document: Document, template: ContextualTemplate = DEFAULT_TEMPLATE, max_header_tokens: int = DEFAULT_MAX_HEADER_TOKENS) -> tuple[str, str]

Parameter Type Default Description
document Document The Document whose page_content will be embedded.
template ContextualTemplate DEFAULT_TEMPLATE A format-map style string (placeholders: title, section, category, page, language, source, content) or a callable that receives the raw document_meta dict and returns the header string. document.page_content is always appended automatically after a "\n\n" separator, so the callable must NOT include the chunk text itself. If the callable accidentally returns "header\n\ncontent" form, the content portion is discarded and the document's original page_content is used instead.
max_header_tokens int DEFAULT_MAX_HEADER_TOKENS Approximate upper bound on header length measured in whitespace-tokenised words. Prevents header blow-up for documents with extremely long titles.

Returns: tuple[str, str] — A (text_to_embed, header) tuple where: - text_to_embed is the string to pass to the embedding model. - header is the rendered + cleaned header that was prepended (empty string if no usable metadata was found).

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