pydantic-v2-patterns
Skill0 starsChanged 7 months agoApache-2.0View on GitHub
Guardrail for Pydantic v2 runtime models, config loaders, contract schemas, validators, and persisted state serialization in Python code.
In this repository57 other files it gives its agents.
- ai-app-integrationSkill
- anti-llm-frontend-designSkill
- aspnet-core-web-developmentSkill
- async-python-patternsSkill
- browser-game-foundationsSkill
- browser-local-qaSkill
- build-mcp-server-sdk-v2Skill
- canva-design-opsSkill
- check-completionSkill
- cli-tool-creatorSkill
- cloudflare-agents-mcpSkill
- cloudflare-durable-objectsSkill
- cloudflare-workers-platformSkill
- deployment-preview-workflowsSkill
- docx-document-artifactsSkill
- durable-workflow-orchestrationSkill
- dynamic-og-imagesSkill
- eval-auditSkill
- evaluate-ragSkill
- figma-design-to-code-rulesSkill
- fine-tuningSkill
- frontend-product-builderSkill
- game-asset-pipeline-expansionSkill
- game-ui-playtestSkill
- generate-synthetic-dataSkill
- github-pr-ci-opsSkill
- gmail-inbox-opsSkill
- golden-datasetSkill
- huggingface-llm-trainerSkill
- javascript-monorepo-buildsSkill
- linear-issue-managementSkill
- media-generation-transcriptionSkill
- millrace-rs-contract-paritySkill
- minecraft-mod-qa-automationSkill
- notion-workspace-flowsSkill
- openai-docsSkill
- optimize-agentic-cliSkill
- pdf-artifact-workflowSkill
- phaser-2d-game-developmentSkill
- postgres-query-performanceSkill
- product-ui-polish-reviewSkill
- python-repo-refactoring-hygieneSkill
- rag-corpus-hygieneSkill
- react-next-performance-reviewSkill
- rust-best-practicesSkill
- shadcn-ui-compositionSkill
- skill-evaluation-harnessSkill
- tauri-desktopSkill
- tauri-mcp-cliSkill
- tauri-ptySkill
- tile-pixel-art-transitionsSkill
- train-sentence-transformersSkill
- typer-cli-patternsSkill
- using-dbt-for-analytics-engineeringSkill
- validate-evaluatorSkill
- visual-asset-pipelineSkill
- watchdog-file-watcher-patternsSkill
The file
We split this skill into the rules it gives an agent
12 rules. About 0 are common, 8 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 41
Use v2-native APIs: model_config, ConfigDict, model_validate, model_dump, field_validator, and model_validator.
- Very specific: line 42
Default runtime config and contracts to closed-world behavior such as extra="forbid" unless unknown keys are part of the contract.
- Distinctive: line 43
Keep validators pure: no filesystem reads, environment reads, subprocesses, network calls, or repo mutation.
- Very specific: line 48
Do not add new v1 idioms such as class Config, @validator, @root_validator, parse_obj(), dict(), or json().
- Very specific: line 49
Use explicit defaults and Field(default_factory=...) for mutable containers.
- Distinctive: line 50
Prefer nested models over deeply nested untyped dictionaries.
- Distinctive: line 51
Keep enums, literals, and discriminated unions aligned with persisted state names.
- Distinctive: line 52
Do not hide I/O, mutation, or expensive computation inside validators.
- Distinctive: line 76
Add or update focused tests for valid input, invalid input, defaults, and serialization.
- Distinctive: line 77
Run the repo's targeted validation command and inspect serialized output.
- Distinctive: line 88
Start with the model and boundary being edited.
- Distinctive: line 95
Run a representative valid/invalid load test and a load-to-dump round trip.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Pydantic v2 Patterns
Purpose
Help agents build or refactor Pydantic v2 models for typed runtime configuration, shared contracts, stage/result schemas, and persisted state snapshots. This skill is for validation and serialization boundaries, not arbitrary business logic.
Use When
- adding or changing
BaseModelclasses, validators, config loaders, contract models, or state snapshots - replacing untyped
dict[str, Any]blobs with typed nested models - wiring file, environment, API, or object ingestion through a validation boundary
- removing Pydantic v1 idioms from edited code
Do Not Use When
- a dataclass,
TypedDict, enum, or literal type is enough and no runtime validation boundary exists - the task is broad environment-settings design across many deployment sources
- the change is business logic rather than model shape, validation, normalization, or serialization
Quick Start
- Identify the boundary: load, validate, normalize, mutate, dump, or persist.
- Use v2-native APIs:
model_config,ConfigDict,model_validate,model_dump,field_validator, andmodel_validator. - Default runtime config and contracts to closed-world behavior such as
extra="forbid"unless unknown keys are part of the contract. - Keep validators pure: no filesystem reads, environment reads, subprocesses, network calls, or repo mutation.
- Choose mutable working models or immutable snapshots deliberately.
- Validate one representative load-to-dump round trip before claiming the boundary is correct.
Operating Constraints
- Do not add new v1 idioms such as
class Config,@validator,@root_validator,parse_obj(),dict(), orjson(). - Use explicit defaults and
Field(default_factory=...)for mutable containers. - Prefer nested models over deeply nested untyped dictionaries.
- Keep enums, literals, and discriminated unions aligned with persisted state names.
- Do not hide I/O, mutation, or expensive computation inside validators.
- Preserve serialized field names unless an explicit migration changes the persisted contract.
- Report any compatibility constraint if the repo still supports Pydantic v1.
Inputs This Skill Expects
- Target Python files that define or consume models.
- Boundary call sites that load, validate, mutate, or dump those models.
- Representative raw payloads, config samples, fixtures, or persisted state examples.
- Existing tests for config, contracts, state, or control-plane behavior when available.
- Any serialized output compatibility constraints.
Output Contract
- Provide v2-native model and boundary edits scoped to the touched area.
- Name the input boundary and output serialization shape.
- State extra-field, default, path, enum, mutation, and serialization policy when relevant.
- Include a representative round-trip test, fixture check, or smoke snippet.
- State any intentionally preserved v1 compatibility separately from new v2-native code.
Procedure
- Locate model definitions and loader/dumper call sites.
- Identify the raw input shape and persisted output shape.
- Choose closed-world or open-world extra-field behavior intentionally.
- Implement v2-native fields, validators, computed fields, and serializers only where needed.
- Replace nearby edited v1 idioms with v2-native calls.
- Add or update focused tests for valid input, invalid input, defaults, and serialization.
- Run the repo's targeted validation command and inspect serialized output.
Pitfalls And Gotchas
- Putting file reads or environment lookups inside validators.
- Using
Anybecause the model shape was not investigated. - Forgetting
default_factoryfor lists, dicts, and sets. - Allowing unknown config keys silently in runtime contracts.
- Serializing with defaults or aliases accidentally changed from prior persisted state.
- Migrating a broad model graph when the task only touches one boundary.
Progressive Disclosure
Start with the model and boundary being edited. Expand into migration, aliases, discriminated unions, custom serializers, or settings management only when the current payload requires them. Keep examples small and tied to fixtures or persisted artifacts.
Verification Pattern
- Confirm new or edited code uses Pydantic v2 APIs.
- Confirm untrusted input enters through
model_validateormodel_validate_json. - Confirm serialization uses
model_dumpormodel_dump_jsonwith deliberate flags. - Confirm validators are pure and do not perform hidden I/O.
- Run a representative valid/invalid load test and a load-to-dump round trip.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

