Pydantic 2.13
Last reviewed Sep 12, 2026
Prompt Details
Prompt Composition
2prompts
- Python 3155 lines · 3.8K tokens
- Pydantic 2.1356 lines · 1.3K tokens
Total211 lines · 5.1K tokens
CWE Coverage
- 20
- 23
- 36
- 59
- 78
- 89
- 95
- 117
- 201
- 208
- 209
- 214
- 250
- 295
- 319
- 328
- 338
- 347
- 367
- 379
- 409
- 416
- 426
- 427
- 470
- 502
- 532
- 611
- 674
- 732
- 770
- 772
- 776
- 807
- 829
- 915
- 916
- 918
- 1104
- 1284
- 1287
- 1333
- 1395
Prompt Preview
Apply the Python 3 baseline first. These rules cover Pydantic 2.13 strict validation, model boundaries, validators, unions, serialization, settings, secrets, and resource controls.
Model boundaries and strictness
- Use separate models for raw input, validated domain commands, persistence, and public output.
- Set
model_config = ConfigDict(extra='forbid')for security-sensitive input; the defaultextra='ignore'silently discards unexpected fields. - Set
ConfigDict(strict=True)orField(strict=True)where coercion could change identity, authorization, money, or routing semantics; strict mode is off by default. Check JSON-specific conversions, such as date strings, and do not weaken these boundaries with per-callstrict=False. - Never pass external data to
model_construct(): it performs no validation or nested model conversion, sostrict=True,extra='forbid', and validators do not make that construction path safe. - Do not pass unvalidated updates to
model_copy(update=...); evendeep=Truecopies values without validating the updates. - Apply explicit string, numeric, collection, and nesting constraints to every attacker-controlled field.
- Keep ownership, role, tenant, status, price, audit, and server-generated fields out of client-input models.
Validators and context
- Use field validators for local invariants and model validators for cross-field security invariants.
- Keep
SkipValidation, plain validators, and wrap validators that skip their handler out of fields that rely on Pydantic's type and constraint validation. - Keep validators