Schema Versioning Guide
This document provides detailed information about the schema versioning system used in PromptDrifter. For basic contribution guidelines, please refer to the general contributing guide.
Schema Overview​
PromptDrifter uses a versioned schema system to validate configuration files. The schema is defined in JSON format and used to validate YAML configuration files. This ensures that configuration files are valid and consistent, while allowing for future evolution of the schema.
Schema Components​
The schema system consists of several components:
- JSON Schema Files: Formal schema definitions in JSON format
- Pydantic Models: Python classes that mirror the schema structure
- Version Management: Tools to manage schema versions
- Validation Logic: Code that validates configurations against schemas
Directory Structure​
src/promptdrifter/
├── schema/ # Schema-related code
│ ├── constants.py # Version definitions
│ ├── validator.py # Schema validation logic
│ ├── migration.py # Migration tools (for future)
│ ├── update_symlinks.py # Utility to update symlinks
│ ├── v0.1/ # Version-specific schemas
│ │ ├── schema.json # JSON Schema definition
│ │ └── sample.yaml # Example configuration
│ └── latest/ # Symlink to current version
└── models/ # Pydantic models
└── config.py # Configuration models
Version Management​
Schema versions are managed through the SchemaVersions
Pydantic model in constants.py
. This model defines the current version, supported versions, and provides a computed field for the latest version.
To add a new schema version:
- Update the
supported_versions
list inSchemaVersions
- Set
current_version
to the new version - Create a new version directory with schema files
- Run the
update_symlinks.py
script to update symlinks
Validation Process​
Configuration validation follows these steps:
- YAML file is loaded using
yaml.safe_load()
- The loaded data is validated against the JSON schema
- The data is then converted to a Pydantic model for runtime type checking
- Both validations must pass for the configuration to be considered valid
Making Schema Changes​
Minor Changes (Backward Compatible)​
For changes that don't break existing configurations:
- Update the JSON schema file with the new fields or modifications
- Update the corresponding Pydantic models in
models/config.py
- Add tests covering valid configurations, edge cases, and optional fields
- Document the changes
Examples: Adding optional fields, extending enums with new values, adding new options to existing fields.
Major Changes (Backward Incompatible)​
For changes that break existing configurations:
- Create a new version directory (e.g.,
schema/v0.2/
) - Copy and modify the schema files from the previous version
- Update the
SchemaVersions
model to include the new version - Implement migration functions to help users upgrade
- Update symlinks and Pydantic models
- Add comprehensive tests and documentation
Examples: Removing required fields, changing field types, renaming fields, restructuring the schema.
Migration Approach​
When creating a new schema version with breaking changes:
- Implement migration functions in
migration.py
to transform old configurations to the new format - Create CLI commands to help users migrate their configurations
- Document the migration process clearly with examples
Testing Strategy​
For schema changes, implement tests for:
- Validation: Ensure valid configurations pass validation
- Migration: Verify migrations correctly transform configurations
- Error Handling: Confirm invalid configurations fail with appropriate errors
Best Practices​
- Follow semantic versioning principles
- Maintain backward compatibility whenever possible
- Document all changes clearly
- Provide migration tools for major changes
- Test thoroughly before releasing
- Keep the schema as simple as possible while meeting requirements
- Include examples with each schema version