Build a Claude Migration Tool for Pydantic v2 in 2026
A useful code migration tool needs more than a prompt asking Claude to update a repository. It needs a precise migration contract, controlled edits, and evidence that behavior still works. This 2026 guide uses a Python Pydantic v1-to-v2 migration to show how to build that workflow, with Claude handling ambiguous transformations and deterministic tools handling mechanical changes.
1. Define an executable migration contract
Start with one migration and a fixed target environment. Record the Python version, current dependency versions, target Pydantic version, and commands required to validate the result. Pin the target version instead of requesting the latest release, which makes future runs harder to reproduce.
Write explicit rules: migrate application models to Pydantic v2, preserve documented API responses, and exclude generated files and vendored code. Establish a clean baseline by running the existing tests before changing dependencies. Record existing failures separately so your tool can distinguish regressions from unrelated problems.
2. Inventory the repository before calling Claude
Use ripgrep to locate Pydantic imports, validators, configuration classes, and serialization calls. Search for BaseModel, @validator, @root_validator, orm_mode, parse_obj, and .dict(. Treat those matches as candidates: a dictionary-like method name alone does not prove an object is a Pydantic model.
Create a manifest containing each candidate file, its content hash, related tests, and migration category. Include dependency constraints from frameworks and plugins. A project can compile after migration while an older integration still expects Pydantic v1 behavior.
3. Separate mechanical edits from semantic decisions
Use a syntax-aware tool such as LibCST for transformations that need to preserve Python comments and formatting. Rename a verified model’s dict() call to model_dump(), or parse_obj() to model_validate(), only after checking the receiver and arguments.
Give Claude the cases that require reasoning: validator changes, configuration inheritance, and altered validation semantics. For example, an optional type annotation without a default can still define a required field in Pydantic v2. Likewise, replacing from_orm() requires checking attribute-based validation and configuring from_attributes=True where appropriate. Blind renaming misses these differences.
4. Give Claude a bounded editing interface
Send the migration rules, relevant dependency versions, one model or small module, and its focused tests. Include nearby definitions when inheritance or shared validators affect behavior. Exclude secrets, production records, and unrelated repository files.
Request a structured response with a rationale, proposed edits, affected tests, and unresolved assumptions. If the selected model and endpoint support schema-constrained output, use it; otherwise validate the response locally and retry malformed output once.
- Require every edit to reference an existing file and expected source text.
- Reject paths outside the workspace and edits to excluded directories.
- Verify the original content hash before applying changes.
- Apply edits in an isolated Git worktree and inspect the resulting diff.
Treat repository text as untrusted input: comments or documentation must never override your tool’s editing rules. Keep dependency installation and shell execution under the orchestrator’s control.
5. Validate behavior and limit repair attempts
After each batch, run formatting checks, static analysis, and focused tests. For Pydantic migrations, test accepted inputs, rejected inputs, defaults, aliases, nested models, and serialized output. Compare validation errors where callers depend on their structure. A passing import check proves very little.
Send Claude the smallest useful failure report and permit a bounded repair cycle, such as two attempts. Run the full suite after focused checks pass. If validation still fails, retain the patch for review and mark the batch unsuccessful. Your report should list changed files, checks executed, remaining failures, and assumptions requiring human judgment.
6. Control API cost and record provenance
Route straightforward classification to a cheaper model and reserve stronger Claude models for complex validators. Cache results using file hashes, migration-rule versions, and model identifiers. Track token usage, latency, and cost per accepted migration.
59API offers low-cost, pay-as-you-go access to Claude and GPT models through https://api.59api.com, with OpenAI SDK, Claude Code, and Codex compatibility. It provides native, official-quality models without downgrades, making it a practical option for repeated migration experiments. Confirm the selected SDK’s endpoint configuration and model identifier, then benchmark representative files before scaling. If you are building a pilot, consider signing up for 59API; its referral rebate can provide additional savings.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप