Skip to main content

PydanticPrompt

PyPI version Python Versions Build Status codecov License Code style: ruff

A simple library to document Pydantic models for structured LLM outputs using standard Python docstrings.

Installation

pip install pydantic-prompt

Requirements

  • Python 3.11 or higher
  • Pydantic 2.0 or higher

Features

  • Document Pydantic model fields using standard Python docstrings
  • Format field documentation for LLM prompts with a single method call
  • Automatic type inference and display
  • Optional validation rule documentation
  • Warnings for undocumented fields
  • Seamlessly embeds documentation in your prompts

Usage

Basic Example

from pydantic import BaseModel, Field
from typing import Optional
from pydantic_prompt import prompt_schema

@prompt_schema
class Person(BaseModel):
    name: str
    """The person's full name"""
    
    age: int = Field(ge=0)
    """Age in years"""
    
    email: Optional[str] = None
    """Contact email address if available"""

# Use in LLM prompt
prompt = f"""
Please extract a Person object from this text according to the schema below:

{Person.format_for_llm()}

Input: Jane Smith, 28, works at Acme Corp.
"""

Output

The format_for_llm() method produces output like:

Person:
- name (str): The person's full name
- age (int): Age in years
- email (str, optional): Contact email address if available

Working with Nested Models

from pydantic import BaseModel
from pydantic_prompt import prompt_schema
from typing import list

@prompt_schema
class Address(BaseModel):
    street: str
    """Street name and number"""
    
    city: str
    """City name"""
    
    postal_code: str
    """Postal or ZIP code"""

@prompt_schema
class Contact(BaseModel):
    name: str
    """Full name of the contact"""
    
    addresses: list[Address] = []
    """List of addresses associated with this contact"""

# The format_for_llm method will nicely format nested structures too
print(Contact.format_for_llm())

Including Validation Rules

You can include validation rules in the output:

Person.format_for_llm(include_validation=True)

Output:

Person:
- name (str): The person's full name
- age (int): Age in years [Constraints: ge: 0]
- email (str, optional): Contact email address if available

Warnings for Undocumented Fields

By default, PydanticPrompt warns you when fields don't have docstrings:

@prompt_schema
class PartiallyDocumented(BaseModel):
    name: str
    """This field has a docstring"""
    
    age: int
    # Missing docstring will generate a warning

Output:

UserWarning: Field 'age' in PartiallyDocumented has no docstring. Add a docstring for better LLM prompts.

To disable these warnings:

@prompt_schema(warn_undocumented=False)
class SilentModel(BaseModel):
    name: str
    # No warning for missing docstring

Real-World Example

from pydantic import BaseModel, Field
from pydantic_prompt import prompt_schema
from typing import list

@prompt_schema
class ProductReview(BaseModel):
    product_name: str
    """The exact name of the product being reviewed"""
    
    rating: int = Field(ge=1, le=5)
    """Rating from 1 to 5 stars, where 5 is best"""
    
    pros: list[str]
    """List of positive aspects mentioned about the product"""
    
    cons: list[str]
    """List of negative aspects mentioned about the product"""
    
    summary: str
    """A brief one-sentence summary of the overall review"""

# In your LLM prompt
prompt = f"""
Extract a structured product review from the following text.
Return the data according to this schema:

{ProductReview.format_for_llm(include_validation=True)}

Review text:
I recently purchased the UltraBook Pro X1 and I've been using it for about two weeks.
The screen is absolutely gorgeous and the battery lasts all day which is fantastic.
However, the keyboard feels a bit mushy and it runs hot when doing anything intensive.
Overall, a solid laptop with a few minor issues that don't detract from the experience.
"""

How It Works

PydanticPrompt uses Python's introspection capabilities to:

  1. Capture docstrings defined after field declarations
  2. Extract type information from type hints
  3. Identify optional fields
  4. Format everything in a clear, LLM-friendly format

The library is designed to be lightweight and focused on a single task: making it easy to document your data models for LLMs in a way that both developers and AI can understand.

Why Use PydanticPrompt?

  • Consistency: Document your models once, use everywhere
  • DRY Principle: No need to duplicate field descriptions between code and prompts
  • Developer-Friendly: Uses standard Python docstrings
  • LLM-Friendly: Produces clear, structured documentation that LLMs can easily understand
  • Maintainability: When your models change, your prompt documentation automatically updates

Development

Setting up the development environment

Clone the repository:

git clone https://github.com/OpenAdaptAI/PydanticPrompt.git
cd PydanticPrompt

Create and activate a virtual environment with uv:

# Create a virtual environment
uv venv

# Activate the virtual environment
source .venv/bin/activate  # On Unix/macOS
# or
.venv\Scripts\activate  # On Windows

Install the package in development mode:

# Install the package
uv pip install -e .

Install development dependencies:

# Install dev dependencies
uv pip install -e ".[dev]"

Note about hardlinking warning

If you see a warning about hardlinking when installing:

warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.

This is normal when the cache and target directories are on different filesystems. You can suppress this warning with:

export UV_LINK_MODE=copy

Running tests

Make sure your virtual environment is activated, then run:

# Run all tests
pytest

# Run with verbose output
pytest -v

# Run with coverage information
pytest --cov=pydantic_prompt

Code formatting and linting

You can run all formatting and linting checks with:

# Run all linting and formatting 
./lint.sh

Or run individual commands:

# Format code
ruff format .

# Check code
ruff check .

# Auto-fix issues
ruff check --fix .

# Type check
mypy pydantic_prompt

License

MIT

Metadata

Release files for pydantic-prompt 0.1.0

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-prompt 0.1.0
File Size Uploaded Provenance Actions
pydantic_prompt-0.1.0.tar.gz 33.4 kB Without provenance Details for pydantic_prompt-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-prompt 0.1.0
File Interpreter ABI Platform Actions
pydantic_prompt-0.1.0-py3-none-any.whl Python 3 none any Details for pydantic_prompt-0.1.0-py3-none-any.whl

Total release size: 40.8 kB

Release files / pydantic_prompt-0.1.0.tar.gz

Download URL pydantic_prompt-0.1.0.tar.gz
Size 33.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f7df8263b47fcee10f9d12f257e03aa801d54c8ca3a0795da41d6a0261297a0b
BLAKE2b-256 checksum
How to use checksums
bf65eee334e13d88a0010a5050ecada582d69bd81bd5546b1791b721059c22d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / pydantic_prompt-0.1.0-py3-none-any.whl

Download URL pydantic_prompt-0.1.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
983cb9b9288f4b6a4523d29cbcfb0d46023959c557f780186824846cb6adef5d
BLAKE2b-256 checksum
How to use checksums
1fdf3b608438e78356e3b2e4880b01ea0f6ce7d5f88e7b254dc2f96b0fe65b61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for pydantic-prompt 0.1.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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