Skip to main content

Part of STARSYSTEM

Pydantic Stack Core

Typed hierarchical composition for structured text generation through nested Pydantic models.

Overview

Pydantic Stack Core enables building complex documents through nested, typed Pydantic models that render to composite strings via recursive composition. The power isn't in simple string concatenation - it's in creating typed document trees where each node knows how to render itself and its children.

The Core Pattern: Nested Typed Composition

Unlike simple templating, this library enables you to:

  • Nest typed Pydantic models within other models arbitrarily deep
  • Have each model render itself AND its nested children
  • Get full type safety and validation at every level
  • Build complex documents from reusable, composable pieces

Installation

[Installation instructions pending PyPI publication]

Quick Start - Simple Example

from pydantic_stack_core import RenderablePiece, MetaStack, generate_output_from_metastack

# Simple atomic pieces
class Title(RenderablePiece):
    text: str
    level: int = 1
    
    def render(self) -> str:
        return f"{'#' * self.level} {self.text}"

class Paragraph(RenderablePiece):
    content: str
    
    def render(self) -> str:
        return self.content

# Basic usage
stack = MetaStack(pieces=[
    Title(text="Hello", level=1),
    Paragraph(content="World")
])
output = generate_output_from_metastack(stack)

The Real Power: Nested Composition

from pydantic_stack_core import RenderablePiece
from typing import List

# Level 1: Atomic piece
class Author(RenderablePiece):
    name: str
    bio: str
    
    def render(self) -> str:
        return f"**{self.name}** - {self.bio}"

# Level 2: Piece containing other pieces
class CodeExample(RenderablePiece):
    language: str
    code: str
    explanation: str
    
    def render(self) -> str:
        return f"```{self.language}\n{self.code}\n```\n{self.explanation}"

# Level 3: Nested composition with typed fields!
class Section(RenderablePiece):
    title: str
    content: str
    code_examples: List[CodeExample]  # NESTED TYPED MODELS!
    
    def render(self) -> str:
        output = f"## {self.title}\n\n{self.content}\n\n"
        for example in self.code_examples:
            output += example.render() + "\n\n"  # RECURSIVE RENDERING!
        return output

# Level 4: Deep hierarchical document
class BlogPost(RenderablePiece):
    title: str
    author: Author  # NESTED MODEL AS FIELD!
    sections: List[Section]  # LIST OF NESTED MODELS!
    
    def render(self) -> str:
        output = f"# {self.title}\n\n"
        output += self.author.render() + "\n\n"
        for section in self.sections:
            output += section.render()  # CASCADING RENDERS!
        return output

# Build complex document through typed composition
blog = BlogPost(
    title="Understanding Pydantic Stack",
    author=Author(name="Isaac", bio="Building compound intelligence"),
    sections=[
        Section(
            title="Getting Started",
            content="This library enables nested typed composition.",
            code_examples=[
                CodeExample(
                    language="python",
                    code="class MyModel(RenderablePiece):\n    pass",
                    explanation="Define your model by inheriting RenderablePiece"
                )
            ]
        )
    ]
)

# Single render call triggers entire tree!
output = blog.render()

This creates a fully typed document tree where:

  • Type safety: Can't put an Author where a Section belongs
  • Validation: Pydantic validates at every level
  • Composition: Sections contain CodeExamples, BlogPosts contain Sections
  • Recursive rendering: Each piece renders itself and its children

Advanced Usage

Custom Separators

# Control spacing between pieces
stack = MetaStack(
    pieces=[...],
    separator="\n\n"  # Double newline between pieces
)

Nested Stacks

Since MetaStack is also a RenderablePiece, you can nest them:

section1 = MetaStack(pieces=[
    Title(text="Section 1", level=2),
    Paragraph(content="Section content...")
])

section2 = MetaStack(pieces=[
    Title(text="Section 2", level=2), 
    Paragraph(content="More content...")
])

document = MetaStack(pieces=[
    Title(text="Main Document", level=1),
    section1,
    section2
])

Custom Rendering Logic

class NumberedList(RenderablePiece):
    items: List[str]
    
    def render(self) -> str:
        return "\n".join(f"{i+1}. {item}" for i, item in enumerate(self.items))

class BulletList(RenderablePiece):
    items: List[str]
    
    def render(self) -> str:
        return "\n".join(f"• {item}" for item in self.items)

Use Cases

  • Documentation Generation: Create structured docs from data
  • Code Generation: Build source code from templates
  • Report Creation: Compose complex reports from components
  • Template Systems: Build flexible, reusable content templates
  • Agent Outputs: Systematic text generation for AI agents

Integration with HEAVEN Ecosystem

Pydantic Stack Core integrates with:

  • Payload Discovery: For systematic content generation workflows
  • Powerset Agents: For structured agent outputs
  • MetaStack Powerset Agent: For learning and applying stacking patterns

Development

# Clone and install for development
git clone https://github.com/sancovp/pydantic-stack-core
cd pydantic-stack-core
pip install -e ".[dev]"

# Run tests
pytest

# Format code
black .
ruff check .

Why Stack-Based?

The stack metaphor makes composition intuitive:

  1. Sequential: Pieces render in order
  2. Nestable: Stacks can contain other stacks
  3. Separable: Control spacing between pieces
  4. Extensible: Easy to add new piece types

This approach scales from simple text formatting to complex document generation while keeping the API minimal and predictable.

License

MIT License - see LICENSE file for details.

Part of HEAVEN Ecosystem

This library is part of the HEAVEN (Hierarchical Event-based Agent-Versatile Environment Network) ecosystem for AI agent development.

Metadata

Release files for pydantic-stack-core 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-stack-core 0.1.4
File Size Uploaded Provenance
pydantic_stack_core-0.1.4.tar.gz 12.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-stack-core 0.1.4
File Interpreter ABI Platform
pydantic_stack_core-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 28.0 kB

Release files / pydantic_stack_core-0.1.4.tar.gz

Download URL pydantic_stack_core-0.1.4.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
15e66e496bd5d16fae621fc564779d118124ebb7775ed1e0038d1f4cc02e057e
BLAKE2b-256 checksum
How to use checksums
65db46e87f5b49dace84b14ec9f7779d6ebc9ce062bac1b8a01471f4669cf2dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pydantic_stack_core-0.1.4-py3-none-any.whl

Download URL pydantic_stack_core-0.1.4-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
036fdf1e2f4252f44e6d7096f5203ae1b64f5d907d1ebb028c9e9cb429ec3262
BLAKE2b-256 checksum
How to use checksums
d91e1c5146df528caa1ec485ba8de2ac2bf344912fb026781598f2543544ebb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for pydantic-stack-core 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page