Skip to main content

A framework for creating extensible, reusable, and standardized prompts

Project description

Structured Prompt Framework

CI PyPI version Python Support License: MIT

A framework for creating extensible, reusable, and standardized prompts that can be easily modified by LLMs and adapted across organizations.

Project Goals

This framework addresses the challenge of maintaining consistent, high-quality prompts across teams and AI systems by providing:

  1. Extensibility - Add new prompt sections and modify existing ones without breaking the core structure and following the same semantic conventions, bullets indentation etc.
  2. Reusability - Share and compose prompt components across different use cases and teams
  3. Standardization - Enforce consistent prompt structure and formatting within your organization
  4. LLM-Friendly - Enable AI systems to understand and modify prompts programmatically
  5. Self-Introspection (TBD) - Support APIs to evaluate the rendered prompt for inconsistencies and ambiguities

Key Features

🏗️ Hierarchical Stage System

  • Define canonical prompt stages in YAML (e.g., Objective, Planning, Execution, Output)
  • Auto-generate type-safe, IDE-friendly stage classes
  • Support both predefined stages and ad-hoc sections

📝 Flexible Content Assembly

  • Mix plain text, structured sections, and nested components
  • Append or replace content with clear semantics
  • Support metadata like titles, subtitles, and bullet styles

🎯 Organization Templates

  • Define organization-specific prompt templates in YAML
  • Enforce consistent structure while allowing customization
  • Support fixed ordering for critical sections

🔧 Developer Experience

  • Full IDE autocompletion and static analysis
  • Type-safe stage references
  • Clear separation between structure and content

Quick Start

1. Define Your Prompt Structure

Create a YAML file defining your organization's prompt template:

# stages.yaml
stages:
  objective:
    display: "Objective"
    description: "Define the goal and scope"
    order: fixed
    order_index: 0
  
  planning:
    display: "Planning"
    description: "Outline the approach"
    children:
      steps:
        display: "Steps"
        description: "Detailed execution steps"
  
  execution:
    display: "Execution"
    description: "How to carry out the plan"
  
  output:
    display: "Output"
    description: "Format and structure results"

2. Generate Stage Classes

Using the command-line tool:

structured-prompt generate stages.yaml -o src/stages.py

Or programmatically:

from structured_prompt import PromptStructureGenerator

generator = PromptStructureGenerator("stages.yaml")
generator.generate("src/stages.py")

3. Build Your Prompt

The framework supports multiple ways to define a prompt. In most cases assigning an array would extend the existing prompt rather than override it:

from structured_prompt import StructuredPromptFactory, PromptSection
from stages import Stages

# Create a structured prompt
prompt = StructuredPromptFactory(stage_root=Stages)

# Add content to stages
prompt[Stages.Objective] = [
    "Analyze the provided incident and identify root causes",
    "Provide actionable recommendations for resolution"
]

# Add nested content
prompt[Stages.Planning.Steps] = [
    "Start with tracing tools",
    "Follow with metrics analysis",
    "End with infrastructure investigation"
]

# Add ad-hoc sections
prompt[Stages.Output]["Developer Notes"] = [
    "Include code snippets and configuration examples",
    "Provide step-by-step resolution instructions"
]

4. Render the Prompt

# Get the formatted prompt
formatted_prompt = prompt.render()

print(formatted_prompt)

Output:

1. Objective
  - Analyze the provided incident and identify root causes
  - Provide actionable recommendations for resolution

2. Planning
  - Investigation Plan
    * Review system logs and metrics
    * Analyze error patterns and correlations
    * Identify potential infrastructure issues
  - Steps
    * Start with tracing tools
    * Follow with metrics analysis
    * End with infrastructure investigation

3. Output
  - Developer Notes
    * Include code snippets and configuration examples
    * Provide step-by-step resolution instructions

Architecture

Stage Model (Auto-generated)

  • Input: YAML structure definition
  • Output: Static Python classes with metadata
  • Benefits: IDE autocompletion, type safety, predictable imports

Prompt Assembly Layer

  • Purpose: Compose prompts by addressing stages and adding content
  • Features:
    • Hierarchical addressing (Stages.Planning.Steps)
    • Append vs. replace semantics
    • Order guarantees for fixed sections
    • Idempotent operations

Rendering System

  • Purpose: Convert structured content to formatted text
  • Features:
    • Configurable bullet styles and indentation
    • Hanging alignment for multi-line content
    • Critical step highlighting
    • Customizable formatting preferences

Advanced Usage

Critical Steps

Highlight mandatory actions within your prompt:

prompt.add_critical_step("VERIFY ASSUMPTIONS", "Always validate data sources before proceeding")
prompt[Stages.Execution].add_critical_step("SAFETY CHECK", "Confirm no production impact before changes")

Custom Bullet Styles

Control formatting at the section level:

prompt[Stages.ToolReference] = PromptSection(
    bullet_style=None,  # No bullets for children
    subtitle="Format: [tool|purpose|inputs|notes]",
    items=[
        "[tracing|service analysis|scope|run first]",
        "[metrics|correlation|scope|run after tracing]"
    ]
)

Fixed Ordering

Ensure critical sections appear in specific positions:

# In your YAML:
tool_reference:
  order: fixed
  order_index: 3  # Always appears 4th in the prompt

Organization Integration

Template Standardization

  1. Define Core Template: Create a YAML file with your organization's standard prompt structure
  2. Generate Stage Classes: Run the generator to create type-safe stage classes
  3. Distribute: Share the generated classes across teams
  4. Enforce: Use the framework to ensure all prompts follow the same structure

LLM Modifications

The framework enables AI systems to:

  • Understand prompt structure through the stage hierarchy
  • Add or modify content while maintaining consistency
  • Validate changes against the organization's template
  • Generate new prompt variations programmatically

Installation

pip install structured-prompt

For development:

git clone https://github.com/model-labs/structured-prompt
cd structured-prompt
pip install -e ".[dev]"

CLI Reference

The structured-prompt command-line tool provides utilities for working with the framework.

Getting Help

# Show all available commands
structured-prompt --help

# Show help for a specific command
structured-prompt generate --help

Generate Command

Generate Python stage classes from a YAML definition:

# Basic usage
structured-prompt generate stages.yaml -o src/stages.py

# With custom paths
structured-prompt generate config/prompt_structure.yaml -o myapp/prompts/stages.py

Arguments:

  • input - Path to the input YAML file containing stage definitions
  • -o, --output - Path where the generated Python file should be written

Development

Running Tests

pytest tests/

Linting and Type Checking

# Run ruff linter
ruff check src/

# Run type checker
mypy src/structured_prompt

Generating Stages

Using the CLI tool:

structured-prompt generate stages.yaml -o src/stages.py

Or programmatically:

from structured_prompt import PromptStructureGenerator

generator = PromptStructureGenerator("stages.yaml")
generator.generate("src/stages.py")

Contributing

Contributions are welcome! Please follow these guidelines:

  1. Fork the repository and create a feature branch
  2. Follow the existing code structure and patterns
  3. Add tests for new features (we aim for high test coverage)
  4. Run the test suite and ensure all tests pass
  5. Run linters (ruff check src/) to ensure code quality
  6. Update documentation for API changes
  7. Submit a pull request with a clear description of changes

CI/CD

All pull requests automatically run:

  • Tests on Python 3.13
  • Linting with ruff
  • Type checking with mypy
  • Package build validation

The CI must pass before merging.

Releasing

To create a new release:

  1. Create a git tag with the version (e.g., v0.3.0):

    git tag v0.3.0
    git push origin v0.3.0
    
  2. Create a GitHub release from the tag

  3. The workflow will automatically:

    • Extract the version from the tag
    • Update pyproject.toml and __init__.py
    • Build the package
    • Publish to PyPI and TestPyPI

Note: The version in the code doesn't need to be manually updated - it's extracted from the git tag during release.

License

MIT


This framework transforms prompt management from an ad-hoc process into a systematic, maintainable system that scales with your organization's needs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

structured_prompt-0.2.6.tar.gz (36.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

structured_prompt-0.2.6-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file structured_prompt-0.2.6.tar.gz.

File metadata

  • Download URL: structured_prompt-0.2.6.tar.gz
  • Upload date:
  • Size: 36.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for structured_prompt-0.2.6.tar.gz
Algorithm Hash digest
SHA256 1186f09403cb2395189b3e849cb0b011250a48d9af111bb8912ee0fd9da513ba
MD5 314e2847176a2cf219b0c86f1270d70e
BLAKE2b-256 7f0b6a49c38ff03c0d844b49b6c16970e5869d52165d895fa5bb2fdedae73208

See more details on using hashes here.

Provenance

The following attestation bundles were made for structured_prompt-0.2.6.tar.gz:

Publisher: publish-to-pypi.yml on model-labs/structured-prompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file structured_prompt-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for structured_prompt-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2234991b3e3a63204db079fa8ce56b95bc62a55b96aa41193a841d90e78fe608
MD5 ce46db2c3054f115c0153183435831e9
BLAKE2b-256 cfa14fcc929300712cacddd130fa3984e6289e732b4270e08452e66e3a2bf760

See more details on using hashes here.

Provenance

The following attestation bundles were made for structured_prompt-0.2.6-py3-none-any.whl:

Publisher: publish-to-pypi.yml on model-labs/structured-prompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page