Implement high-throughput Rust-backed Pydantic v2 data models, field validators, custom type coercions, and graceful Dead-Letter-Queue routing for malformed payloads.
[!IMPORTANT] Production Context: When ingesting millions of telemetry events or third-party webhooks per second, relying on loose Python dicts results in silent failures down the stream (e.g.
TypeError: '<' not supported between instances of 'NoneType' and 'int'). Pydantic v2 re-architected its core in Rust (pydantic-core), delivering a 5x-50x throughput gain, making it production-viable for inline stream validation.
Pydantic v2 performs validation directly in C-speed compiled Rust memory before instantiating Python objects.
| Feature | Pydantic v1 | Pydantic v2 | Production Impact |
|---|---|---|---|
| Engine | Pure Python loops | Rust (pydantic-core) | 5x to 50x faster validation throughput |
| Model Dump | .dict() | .model_dump() & .model_dump_json() | Native binary JSON serialization |
| Config | Inner class Config: | model_config = ConfigDict(...) | Type-safe, autocomplete friendly |
| Field Validators | @validator | @field_validator('col', mode='before') | Explicit pre/post validation pipeline |
Never let a single corrupt JSON event crash an entire ingestion batch. Use the DLQ pattern:
ValidationError is caught, serialize error details + original payload to an S3/GCS DLQ prefix or Kafka DLQ topic.pipeline_records_quarantined_total.Review the production code, company interview Q&As, incident traces, and comprehension quiz inside this lesson.
from pydantic import BaseModel, Field, field_validator, ConfigDict, ValidationError
from typing import Optional, List, Tuple
from datetime import datetime
import json
class RawEventSchema(BaseModel):
model_config = ConfigDict(frozen=True, str_strip_whitespace=True)
event_id: str = Field(min_length=8, max_length=64)
user_id: int = Field(gt=0)
event_timestamp: datetime
amount_usd: float = Field(ge=0.0)
ip_address: Optional[str] = None
@field_validator('amount_usd', mode='before')
@classmethod
def sanitize_currency(cls, v):
if isinstance(v, str):
# Clean '$1,250.50' string values from legacy endpoints
return float(v.replace('$', '').replace(',', '').strip())
return v
def ingest_batch(raw_json_records: List[str]) -> Tuple[List[RawEventSchema], List[dict]]:
valid_events = []
quarantine_dlq = []
for raw in raw_json_records:
try:
event = RawEventSchema.model_validate_json(raw)
valid_events.append(event)
except ValidationError as err:
quarantine_dlq.append({
"raw_payload": raw,
"error_details": err.errors(),
"quarantine_timestamp": datetime.utcnow().isoformat()
})
return valid_events, quarantine_dlqpydantic_core._pydantic_core.ValidationError: 1 validation error for EventSchema\namount\n Input should be a valid number