Skip to content

skillData and AIpython

Data Validation Pydantic

Validates API payloads, files and config in Python with Pydantic v2 models, strict types and error reports a user can understand.

npx workspacetemplates add data-validation-pydantic --tool claude-code

Writes 1 file to .claude/skills/data-validation-pydantic/

.claude/skills/data-validation-pydantic/SKILL.md29 lines · 1.7 KBFile
---
name: data-validation-pydantic
description: Validates API payloads, files and config in Python with Pydantic v2 models, strict types and error reports a user can understand.
---

# Data validation with Pydantic

Validate once at the edge, then pass typed models inward.

## Steps

1. Find boundaries: request bodies, queue messages, files, environment variables, third-party API responses. List what each should contain.
2. Write one `BaseModel` per shape. Use precise types (`EmailStr`, `HttpUrl`, `conint(ge=0)`, `Literal[...]`, `datetime`) instead of bare `str` and `int`.
3. Decide strictness per model. Use `model_config = ConfigDict(strict=True, extra="forbid")` for internal contracts so typos fail loudly. Keep coercion only where the source is text, such as CSV.
4. Express rules that span fields with `@model_validator(mode="after")` and single-field rules with `@field_validator`. Raise `ValueError` with a human message.
5. Parse with `Model.model_validate(data)` or `model_validate_json`. Never catch a bare exception; catch `ValidationError`.
6. Convert errors: loop over `e.errors()` and build `{"field": ".".join(map(str, err["loc"])), "problem": err["msg"]}`. Return all errors at once, not the first.
7. Use `SecretStr` for tokens and exclude them from output with `model_dump(exclude=...)`.
8. For settings, use `pydantic-settings` so missing variables fail at startup.

## Rules

- No `dict[str, Any]` crossing a function boundary when a model fits.
- Do not use v1 names (`parse_obj`, `validator`, `Config`).
- Add a test with one valid and three invalid payloads per model.

## Report

List models added, fields tightened, and any input that used to pass and now fails.

What it does

Data from outside your program is untrusted until a model says otherwise. This skill has the agent put a Pydantic model at every boundary, so the rest of the code works with typed objects instead of loose dicts.

It also covers the part people skip: turning a validation failure into a message that names the field and the problem, for logs and for end users.

Example

you › Validate the signup webhook payload before we create the account

agent › Added SignupEvent with EmailStr, a Literal plan field and extra="forbid". The handler now returns 422 with a list of {field, problem} entries. Tests cover a valid payload, a bad email, an unknown plan and a missing timestamp.

Questions

Should I use strict mode everywhere?
Use it for data you control. For text sources like CSV or query strings, lax mode is needed because everything arrives as a string.
Does validation slow my service down?
Pydantic v2 validates in compiled Rust and is rarely the bottleneck. Measure before skipping it on a hot path.
  • Data Pipeline Design

    SkillData and AI

    Free
  • dbt Modeling

    SkillData and AI

    Free
  • LLM Cost and Latency

    SkillData and AI

    Free