Issue
The migration from Pydantic V1 to V2 introduced 112 validation errors (58.6% of total), primarily due to:
- Deprecated validator syntax:
@validatordecorators were replaced with@field_validatorand@model_validator(mode='after'). - Strict type coercion: Empty environment variables (e.g.,
CHROMA_PORT=) now triggerValidationErrorinstead of silently defaulting toNone. - Cross-field validation changes: The
valuesparameter in validators was removed, requiring updates to validation logic. - Configuration system overhaul: The
class Configpattern was replaced withmodel_config = ConfigDict(...)syntax.
Culprit
Validator Syntax Changes
Pydantic V2 introduced breaking changes to the validation system:
- Field validators: Required
@field_validatorwith@classmethoddecorator and explicit type hints. - Cross-field validation: Moved from
@validatorto@model_validator(mode='after')for post-validation checks. - Removed
valuesparameter: Cross-field validation logic needed restructuring to access other fields.
Environment Variable Handling
Pydantic V2 enforces stricter type coercion, rejecting empty strings in environment variables. For example:
- Empty strings (
CHROMA_PORT=) now causeValidationErrorinstead of defaulting toNone. - Fields expecting integers must explicitly handle
Noneor omit empty variables from configuration files.
Configuration System Overhaul
The class Config pattern was deprecated in favor of ConfigDict:
json_schema_extraand other configuration attributes must now be defined withinmodel_config.
Solution
Validator Migration Strategy
- Field validators: Replaced
@validatorwith@field_validatorand added@classmethodand type hints. - Cross-field validation: Used
@model_validator(mode='after')to access all fields after initialization. - Type safety: Added explicit type hints and validation logic for environment variables.
Environment Variable Fixes
- Best practice: Remove empty variables from
.envfiles entirely. - Optional fields: Use
Optional[int]with explicit default handling. - Documentation: Clearly document default values in configuration examples.
Configuration Migration
- Replaced
class Configwithmodel_config = ConfigDict(...)syntax.
Migration Workflow
- Incremental testing: Monitor test pass rates (target: >50%).
- Systematic checklist: Track validator replacements, configuration updates, and environment variable fixes.
- Dependency audit: Ensure required packages (e.g.,
tree-sitter) are installed.
Results
- Error reduction: 78% decrease in validation errors.
- Test pass rate: Improved from 25.7% to 60.2% (+135%).
- Migration time: ~1.5 hours per file (6 files total).
- New tests passing: 66 additional validation tests validated.
- Performance: Test runtime maintained at ~8 seconds per iteration.