Skip to content

Conversation

@abrichr
Copy link
Member

@abrichr abrichr commented Mar 23, 2025

Initial Implementation of PydanticPrompt Library

This PR introduces PydanticPrompt, a lightweight Python library for documenting Pydantic models for LLM interactions using standard Python docstrings.

Overview

PydanticPrompt allows developers to document their Pydantic models with standard Python docstrings and then easily format that documentation for inclusion in LLM prompts, providing a clean, consistent way to describe expected outputs for large language models.

Key Features

  • Simple decorator interface (@prompt_schema)
  • Uses standard Python docstrings for field documentation
  • Automatic type inference including generics and nested models
  • Optional validation rule display
  • Warnings for undocumented fields
  • Seamless integration with existing Pydantic models

Implementation Details

  • Core functionality built around field inspection and docstring extraction
  • Uses Pydantic's JSON schema for validation rule discovery
  • Modern type annotations with mypy validation
  • Automated CI setup with GitHub Actions
  • Comprehensive linting with Ruff

Example Usage

from pydantic import BaseModel, Field
from pydantic_prompt import prompt_schema

@prompt_schema
class UserProfile(BaseModel):
    name: str
    """The full name of the user"""
    
    age: int = Field(ge=0)
    """User's age in years - must be a positive integer"""
    
    email: str = Field(pattern=r"[^@]+@[^@]+\.[^@]+")
    """Valid email address that will be used for communication"""

# In an LLM prompt
prompt = f"""
Extract a UserProfile from this text according to this schema:

{UserProfile.format_for_llm(include_validation=True)}

Text: John Doe is 32 years old, contact him at [email protected]
"""

Test Results

All tests are passing:

====================================================================== test session starts ======================================================================
platform linux -- Python 3.12.1, pytest-8.3.5, pluggy-1.5.0 -- /workspaces/PydanticPrompt/.venv/bin/python
cachedir: .pytest_cache
rootdir: /workspaces/PydanticPrompt
configfile: pyproject.toml
testpaths: tests
collected 4 items                                                                                                                                               

tests/test_pydantic_prompt.py::test_basic_docstring_extraction PASSED                                                                                     [ 25%]
tests/test_pydantic_prompt.py::test_optional_fields PASSED                                                                                                [ 50%]
tests/test_pydantic_prompt.py::test_validation_rules PASSED                                                                                               [ 75%]
tests/test_pydantic_prompt.py::test_nested_models PASSED                                                                                                  [100%]

======================================================================= 4 passed in 0.13s =======================================================================

Changes Made

  • Created core implementation with docstring extraction
  • Added comprehensive type hints with mypy validation
  • Set up CI with GitHub Actions for automated testing
  • Added linting script for consistent code style
  • Implemented warnings for undocumented fields
  • Created detailed documentation and examples

abrichr added 13 commits March 23, 2025 16:06
- Add core functionality with prompt_schema decorator
- Implement docstring extraction from Pydantic models
- Support type annotation formatting for LLM prompts
- Include validation rule documentation
- Add comprehensive tests
- Create project configuration and documentation
- Add warning for undocumented fields
- Rename decorator to prompt_schema for better clarity
- Fix type annotations to follow modern Python practices
- Add linting script for consistent code style enforcement
- Add warnings for undocumented fields
- Update CI workflow to use the linting script
- Fix Ruff configuration in pyproject.toml
- Address all mypy typing issues
- Update test for nested models to be more flexible
- Simplify type representation for better cross-version compatibility
- Fix CI failure with different Python versions
- Enhance type formatting to properly show nested types (like list[Address])
- Rename decorator from llm_documented to prompt_schema for better clarity
- Add warning system for undocumented fields
- Make tests more robust across different Python versions
- Fix type extraction from ForwardRef and complex generic types
- Change import from llm_documented to prompt_schema
- Update __all__ list to include the new name
@codecov
Copy link

codecov bot commented Mar 23, 2025

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment

Thanks for integrating Codecov - We've got you covered ☂️

@abrichr abrichr merged commit da94cd0 into main Mar 23, 2025
3 checks passed
@abrichr abrichr deleted the abrichr-patch-1 branch March 23, 2025 21:29
@abrichr abrichr changed the title Update README.md Initial Implementation of PydanticPrompt Library Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant