pydantic

pydantic is a cursor rule for coding agents from nedcodes-ok/cursorrules-collection. It costs 479 tokens per session, scanned A, original, MIT.

A set of guidelines for using Pydantic, a Python library that checks and converts data against defined models.

In plain words
What is it for?
Use it to define validated data models, constrain fields, check values across fields, manage settings, and turn models back into dictionaries or JSON.
Why use it?
It helps catch invalid input early and keeps validation, configuration, serialization, and defaults consistent.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/pydantic
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for pydantic

README.md
[![agentmods](https://agentmods.dev/badge/rules/nedcodes-ok/cursorrules-collection/pydantic.svg)](https://agentmods.dev/rules/nedcodes-ok/cursorrules-collection/pydantic)
Your own site
<a href="https://agentmods.dev/rules/nedcodes-ok/cursorrules-collection/pydantic"><img src="https://agentmods.dev/badge/rules/nedcodes-ok/cursorrules-collection/pydantic.svg" alt="Measured on agentmods" height="20"></a>
Per session 479 This file is loaded in full into every session.
When invoked 479 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00479 $0.00479
Opus 5 $0.00239 $0.00239
Sonnet 5 $0.00096 $0.00096
Haiku 4.5 $0.00048 $0.00048

Measured yesterday against content hash 306aa57191b0, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

pydantic scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules-mdc/tools/pydantic.mdc · 47 lines

How it starts

The opening of the file, as written. The whole thing — 47 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Pydantic Cursor Rules

You are an expert in Pydantic v2 data modeling. Follow these rules:

Models

  • Inherit from BaseModel for all data classes — never use plain dataclasses for validated data
  • Use model_config = ConfigDict(...) instead of inner class Config (v2 style)
  • Set model_config = ConfigDict(strict=True) when you need strict type coercion
  • Use frozen=True for immutable models — especially for value objects and configs

Fields

  • Use Field() for metadata: Field(description="...", examples=[...], ge=0, max_length=100)
  • Set Field(default=...) or Field(default_factory=list) — never use mutable defaults
  • Use Annotated[int, Field(gt=0)] style for reusable constrained types
  • Mark optional fields explicitly: name: str | None = None

Validators

  • Use @field_validator("field_name") for single-field validation
  • Use @model_validator(mode="before") for cross-field or raw-input transforms
  • Use @model_validator(mode="after") for cross-field validation on parsed models
  • Return the value from field validators — forgetting this silently sets None
  • Use mode="wrap" validators only when you need to control the entire parsing pipeline

Serialization

  • Use model_dump() and model_dump_json() — never dict() (deprecated)
  • Use model_dump(exclude_none=True) to omit unset optional fields
  • Use @field_serializer for custom output formatting (dates, enums, decimals)
  • Use model_validate() and model_validate_json() for deserialization

Settings

  • Use BaseSettings for environment/config with SettingsConfigDict(env_prefix="APP_")
  • Layer sources: .env file → environment variables → constructor args
  • Use @field_validator for settings that need post-load transformation
  • Separate settings by concern: DatabaseSettings, AuthSettings, AppSettings

Patterns

  • Use TypeAdapter for validating non-model types: lists, dicts, unions
  • Use discriminated unions with Literal + Discriminator for polymorphic models
  • Use computed_field for derived properties that should appear in serialization
  • Define reusable types: PositiveInt = Annotated[int, Field(gt=0)]

Read the full file on GitHub · 47 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 47 lines · 479 tokens per session scan A 306aa57191b0

Subscribe to this mod's changes

pydantic is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 479 tokens to every session, about $0.0024 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.