DEV Community

technonotes-hacker
technonotes-hacker

Posted on

LLM - Data Validation with Pydantic

  • While typing a prompt to LLM and asking the LLM to Impersonating a USER --> what is the prompt called ? ROLE PROMPTING
  • ROLE PROMPTING is similar to which Prompting --> SYSTEM Prompting.
  • Main difference --> Boundaries are defined , System Prompting --> As a AI agent , ROLE Prompting --> As a Bank Manager.

Is the output is Structured ?

  • structured or Text Manner ?
  • By default it won't come , we need to define. How the output should be.

RAG System

  • What is Augment ? What will happen ? --> It happens between Retrieval and generation.

DATA VALIDATION

  • Python is Dynamic Typing.
  • Output of LLM , think like text.
  • Get the output in the desired format.
  • There is package called "Pydantic" , data validation.
  • Pydantic is a library in Python .
  • We need to learn --> Live with it --> Data Validation & Data Modelling.
  • Data that comes into your program, from a form, a website or an AI model, can be wrong: a missing field, text where a number should be, an age of -9. Validation means checking data before you use it.
  • Pydantic lets you describe the shape you expect once, as a class, and then checks everything for you.

List

  • List is a immutable element.

Agent

  • Predefined flow is not available in Agent.
  • How to pass the data ?

Questions

Validation --> Checking data before using it, at the gate rather than on the plane.

Pydantic BaseModel --> Describe the shape you expect as a class; Pydantic checks every value against it.

ValidationError --> Lists every problem at once: which field, which rule and which value.

gt / ge / lt / le --> Greater than / greater or equal / less than / less or equal rules for numbers.

min_length / max_length --> How long text (or a list) may be.

Optional field --> bio: str | None = None. May be missing.

default_factory=list --> Makes a fresh empty list every time instead of sharing one.

model_dump() / model_dump_json() --> Turn a model into a dict / into JSON text.

@field_validator --> Your own rule for a field. Raise ValueError to reject; always return the value.

Nested models --> A model inside a model (Order > Customer > Address), all checked with one call.

FastAPI + Pydantic --> Bad request = automatic 422. response_model filters what you send back (no password leaks).

with_structured_output(Model) --> Gets a validated Python object from an LLM instead of text you have to parse.

Notes

Top comments (0)