Pydantic 2.13

Last reviewed Sep 12, 2026

Export as PDF

Prompt Details

Prompt Composition

2prompts
  1. Python 3155 lines · 3.8K tokens
  2. 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 default extra='ignore' silently discards unexpected fields.
  • Set ConfigDict(strict=True) or Field(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-call strict=False.
  • Never pass external data to model_construct(): it performs no validation or nested model conversion, so strict=True, extra='forbid', and validators do not make that construction path safe.
  • Do not pass unvalidated updates to model_copy(update=...); even deep=True copies 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