types — Shared Contracts

On this page

All enumerations and dataclasses used across the package. No logic beyond three convenience methods/properties on BuiltPrompt.

Table of contents


Enumerations

All enums subclass (str, Enum), so instances are interchangeable with their string .value almost everywhere in the API — e.g. strategy="multi_hop" and strategy=PromptStrategy.MULTI_HOP are both accepted by PromptEngine.build().

PromptType

Canonical prompt archetypes.

MemberValueMeaning
QA"qa"Grounded question-answering (default)
CONVERSATIONAL"conversational"Multi-turn dialogue
REASONING"reasoning"Chain-of-thought / multi-hop
AGENT"agent"ReAct / plan-and-execute
TOOL_USE"tool_use"Function-calling description
SAFETY"safety"Content moderation / guard
SUMMARIZATION"summarization"Document summarization
EXTRACTION"extraction"Structured data extraction
COMPARISON"comparison"Compare/contrast multiple docs

Used by: PromptBuilder._resolve_strategy (maps type → default strategy), PromptEngine._auto_detect (infers type from query keywords), GuardrailEngine._select (skips STAY_ON_TOPIC for AGENT/TOOL_USE).

PromptStrategy

Building strategy layered on top of the prompt type.

MemberValueMeaning
SIMPLE"simple"Single-hop, straightforward (default)
MULTI_HOP"multi_hop"Decompose → gather → synthesize
CHAIN_OF_THOUGHT"cot"Step-by-step reasoning
SELF_CONSISTENT"self_consistent"Generate N answers, pick majority
STEP_BACK"step_back"Abstract first, then answer
REACT"react"Reasoning + Acting (agent)
LEAST_TO_MOST"least_to_most"Break into sub-questions

Each member has a concrete BaseStrategy implementation registered in STRATEGY_REGISTRY.

OutputFormat

Desired output format enforced by format-specific guardrail instructions (see GuardrailEngine._FORMAT_INSTRUCTIONS).

MemberValue
TEXT"text"
JSON"json"
MARKDOWN"markdown"
BULLET_LIST"bullet_list"
STRUCTURED"structured"
CITATION"citation"

STRUCTURED combined with a non-None PromptRequest.output_schema causes GuardrailEngine._render to append a json.dumps-formatted schema block to the format instruction.

QueryComplexity

MemberValue
SIMPLE"simple"
MODERATE"moderate"
COMPLEX"complex"
EXPERT"expert"

PromptEngine._auto_detect sets this from query word count and document count. PromptBuilder._COMPLEXITY_UPGRADE maps "complex" → CHAIN_OF_THOUGHT and "expert" → MULTI_HOP (only applied when the type's default strategy is SIMPLE). GuardrailEngine._select adds SELF_CHECK for COMPLEX/EXPERT.

UserProfile

MemberValue
GENERAL"general"
TECHNICAL"technical"
ACADEMIC"academic"
EXECUTIVE"executive"

Drives the localized persona line returned by persona_line().


Dataclasses

Document

A retrieved passage/chunk used as context.

Python · 8 lines
@dataclass
class Document:
    content:   str
    source:    str            = ""
    score:     float          = 1.0
    metadata:  Dict[str, Any] = field(default_factory=dict)
    chunk_id:  Optional[str]  = None
    language:  str            = "en"
FieldTypeDefaultNotes
contentstrrequiredThe passage text
sourcestr""Free-text source label, shown in citation headers
scorefloat1.0Relevance score; drives sort order and "(relevance: X.XX)" annotation in ContextManager._format when < 1.0
metadataDict[str, Any]{}Arbitrary passthrough metadata; ContextManager writes {"truncated": True} into a copy when a doc is partially included
chunk_idOptional[str]NoneNot read anywhere else in the provided source — reserved field
languagestr"en"Not read anywhere else in the provided source — reserved field

Example

Python · 3 lines
from zerda.prompt import Document

doc = Document(content="Paris is the capital of France.", source="wiki/paris", score=0.87)

Message

A single conversation turn.

Python · 4 lines
@dataclass
class Message:
    role:    Literal["system", "user", "assistant"]
    content: str

Used in PromptRequest.memory, BuiltPrompt.messages, and as the element type consumed by ContextManager.format_memory().

PromptRequest

Everything PromptEngine.build() needs, bundled for internal passing (also accepted directly via PromptEngine.build_from_request).

Python · 22 lines
@dataclass
class PromptRequest:
    query:      str
    documents:  List[Document]      = field(default_factory=list)
    memory:     List[Message]       = field(default_factory=list)
    prompt_type:    PromptType      = PromptType.QA
    strategy:       PromptStrategy  = PromptStrategy.SIMPLE
    output_format:  OutputFormat    = OutputFormat.TEXT
    complexity:     QueryComplexity = QueryComplexity.SIMPLE
    user_profile:   UserProfile     = UserProfile.GENERAL
    max_context_tokens: int         = 3000
    max_answer_tokens:  int         = 512
    output_schema:  Optional[Dict]  = None
    language:       str             = "en"
    enable_guardrails:   bool       = True
    enable_cot:          bool       = False
    enable_citations:    bool       = True
    enable_uncertainty:  bool       = True
    session_id:  str  = ""
    user_id:     str  = ""
    trace_id:    str  = ""
    extra:       Dict[str, Any] = field(default_factory=dict)

[!NOTE] enable_cot is unused enable_cot is declared with the comment "auto-set by strategy" but no code in the provided source reads or sets request.enable_cot. Chain-of-thought behavior is actually driven entirely by strategy/prompt_type, not this flag.

extra is read in one place: ReActStrategy._build_user looks up req.extra.get("tools") to render an "Available Tools" section (see strategies).

BuiltPrompt

The result of PromptEngine.build() / strategy.build().

Python · 17 lines
@dataclass
class BuiltPrompt:
    system_prompt: str
    user_prompt:   str
    messages:      List[Message] = field(default_factory=list)
    prompt_type:   PromptType    = PromptType.QA
    strategy:      PromptStrategy = PromptStrategy.SIMPLE
    output_format: OutputFormat  = OutputFormat.TEXT
    estimated_tokens:    int = 0
    context_tokens_used: int = 0
    documents_included:  int = 0
    documents_truncated: int = 0
    guardrails_applied:  List[str] = field(default_factory=list)
    tokens_saved:        int = 0
    optimization_notes:  List[str] = field(default_factory=list)
    session_id: str = ""
    trace_id:   str = ""

to_messages() -> List[Dict[str, str]]

Returns [{"role": m.role, "content": m.content} for m in self.messages] — directly usable as the messages= argument to OpenAI-compatible chat completion APIs.

to_anthropic() -> Dict[str, Any]

Python · 4 lines
{
    "system":   self.system_prompt,
    "messages": [{"role": m.role, "content": m.content} for m in self.messages if m.role != "system"],
}

Filters out the system message from messages (Anthropic's Messages API takes system as a separate top-level parameter). Splat this into client.messages.create(**payload, model=...).

full_text (property)

Python
f"[SYSTEM]\n{self.system_prompt}\n\n[USER]\n{self.user_prompt}"

Convenience string for debugging/logging; not used elsewhere internally.

Example: inspecting a built prompt

Python · 4 lines
prompt = engine.build(query="What is RAG?")
print(prompt.full_text)
print(prompt.to_messages())
print(prompt.to_anthropic())
  • prompt_engine — consumes and produces these types
  • guardrails — reads OutputFormat, PromptType, QueryComplexity
  • templates — reads UserProfile, PromptStrategy, OutputFormat

Related pages