Skip to content

API Reference parrot.bots.prompts.builder

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

parrot.bots.prompts.builder

View source

Composable system prompt builder.

PromptBuilder manages a collection of PromptLayer instances and orchestrates two-phase rendering: CONFIGURE (static vars resolved once) and REQUEST (dynamic vars resolved per call).

Replaces the monolithic system_prompt_template + create_system_prompt() string concatenation approach.

See spec: sdd/specs/composable-prompt-layer.spec.md (Section 3.3)

Alias CacheableSegment

Re-exported from parrot.bots.prompts.segments.CacheableSegment.

Alias LayerPriority

Re-exported from parrot.bots.prompts.layers.LayerPriority.

Class PromptBuilder

Composable system prompt builder.

Parameter Type Default Description
layers Optional[List[PromptLayer]] None
prompt_caching bool False

Attributes:

Attribute Type Description
prompt_caching bool
is_configured bool Whether configure() has been called.
layer_names List[str] List of layer names currently in the builder.

Method PromptBuilder.default

Standard layer stack for most bots.

default() -> PromptBuilder

Returns: PromptBuilder

Method PromptBuilder.minimal

Lightweight stack: identity + security + user_session only.

minimal() -> PromptBuilder

Returns: PromptBuilder

Method PromptBuilder.voice

Voice-optimized stack with voice behavior layer.

voice() -> PromptBuilder

Returns: PromptBuilder

Method PromptBuilder.agent

Agent stack with general-purpose grounding behavior.

agent() -> PromptBuilder

Returns: PromptBuilder

Method PromptBuilder.rag

RAG-only stack: anchors the agent to <knowledge_context> and the backstory-declared KB scope, forbids out-of-context knowledge.

Tools layer is removed because RAG-only agents have no callable tools beyond the retriever (whose output already lands in the knowledge layer).

rag() -> PromptBuilder

Returns: PromptBuilder

Method PromptBuilder.from_system_prompt

Default stack with the identity layer replaced by system_prompt.

Targets YAML-defined agents whose system_prompt already declares the agent's identity ("You are an assistant for X...") and would therefore conflict with the canned IDENTITY_LAYER. The user's text takes over the identity slot; the rest of the default stack (security, knowledge, user_session, tools, output, behavior) wraps around it unchanged.

from_system_prompt(system_prompt: str) -> PromptBuilder

Parameter Type Default Description
system_prompt str Raw system prompt text. $variable placeholders are still substituted via string.Template at render time.

Returns: PromptBuilder — A fresh PromptBuilder with the identity layer overridden.

Method PromptBuilder.add

Add or replace a layer by name.

add(layer: PromptLayer) -> PromptBuilder

Parameter Type Default Description
layer PromptLayer The PromptLayer to add.

Returns: PromptBuilder — Self for method chaining.

Method PromptBuilder.remove

Remove a layer by name. No-op if not present.

remove(name: str) -> PromptBuilder

Parameter Type Default Description
name str The layer name to remove.

Returns: PromptBuilder — Self for method chaining.

Method PromptBuilder.replace

Replace an existing layer. Raises KeyError if not found.

replace(name: str, layer: PromptLayer) -> PromptBuilder

Parameter Type Default Description
name str The layer name to replace.
layer PromptLayer The new PromptLayer.

Returns: PromptBuilder — Self for method chaining.

Raises:

  • KeyError — If the named layer is not in the builder.

Method PromptBuilder.get

Get a layer by name.

get(name: str) -> Optional[PromptLayer]

Parameter Type Default Description
name str The layer name to retrieve.

Returns: Optional[PromptLayer] — The PromptLayer, or None if not found.

Method PromptBuilder.clone

Deep copy for per-agent customization.

clone() -> PromptBuilder

Returns: PromptBuilder — A new independent PromptBuilder with the same layers.

Method PromptBuilder.configure

Phase 1: Resolve CONFIGURE-phase variables once.

Called during bot.configure(). Resolves static variables (name, role, goal, backstory, rationale, dynamic_values, etc.) via partial_render(), caching the partially-resolved layers. REQUEST-phase $placeholders survive intact for build().

configure(context: Dict[str, Any]) -> None

Parameter Type Default Description
context Dict[str, Any] Dictionary of static variable values.

Method PromptBuilder.build

Phase 2: Resolve REQUEST-phase variables and assemble final prompt.

Called on every ask()/ask_stream(). Only resolves dynamic variables (knowledge_content, user_context, chat_history, etc.) because CONFIGURE-phase layers already have their static variables baked in from configure().

If configure() was never called, all layers are rendered with the full context (single-phase fallback).

build(context: Dict[str, Any]) -> str

Parameter Type Default Description
context Dict[str, Any] Dictionary of dynamic variable values.

Returns: str — The assembled system prompt string.

Method PromptBuilder.build_segments

Phase 2 (caching): Produce a list of CacheableSegment objects.

FEAT-181 — Provider-Agnostic Prompt Caching.

Mirrors the iteration in :meth:build but produces :class:~parrot.bots.prompts.segments.CacheableSegment objects tagged with layer.cacheable instead of joining strings. Empty rendered layers are excluded (same behaviour as :meth:build).

This method can be called regardless of the prompt_caching flag value. The flag is inspected by AbstractBot to decide whether to call build() or build_segments().

build_segments(context: Dict[str, Any]) -> List[CacheableSegment]

Parameter Type Default Description
context Dict[str, Any] Dictionary of dynamic variable values (same as :meth:build).

Returns: List[CacheableSegment] — Ordered list of :class:CacheableSegment objects corresponding to non-empty rendered layers, preserving priority order.

Alias PromptLayer

Re-exported from parrot.bots.prompts.layers.PromptLayer.

Alias RenderPhase

Re-exported from parrot.bots.prompts.layers.RenderPhase.

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