home
diamond Go Premium
Data Engineering Path  ·  Data & AI

Capstone 3: AI Data Quality & Anomaly Detector

+---------------------------------------------------------------------------------+
|                           PROJECT 3 ARCHITECTURE                                |
+---------------------------------------------------------------------------------+
|  Data Source (Pandas/Snowflake) -> Profile Stats (Min, Max, Nulls, Distinct)    |
|                                             |                                   |
|                                             v                                   |
|  LLM Quality Inspector          -> Detects semantic drift, formatting rot, & PII|
|                                             |                                   |
|                                             v                                   |
|  Automated RCA & Alert          -> Generates Slack markdown report & dbt tests  |
+---------------------------------------------------------------------------------+

Objective

Detect silent data failures that pass traditional schema and null checks, such as corrupted currency symbols, mock test strings, or prompt injection attempts in incoming data lakes.


Complete Implementation

import pandas as pd
from pydantic import BaseModel, Field
from openai import OpenAI
import instructor

client = instructor.from_openai(OpenAI())

class ColumnAnomaly(BaseModel):
    column_name: str
    anomaly_detected: bool
    risk_level: str = Field(description="LOW, MEDIUM, HIGH, CRITICAL")
    reason: str
    sample_bad_records: list[str]
    suggested_dbt_test: str

class DataQualityAudit(BaseModel):
    dataset_name: str
    passed_audit: bool
    findings: list[ColumnAnomaly]

def audit_dataset_semantics(df: pd.DataFrame, dataset_name: str) -> DataQualityAudit:
    column_summaries = {}
    for col in df.columns:
        column_summaries[col] = {
            "dtype": str(df[col].dtype),
            "sample_distinct": df[col].dropna().astype(str).unique()[:10].tolist()
        }

    prompt = f"""
You are a Principal Data Quality Engineer.
Analyze the following sample column profiles for semantic drift, injection attacks, mock test data (e.g. 'asdf', 'test@test.com'), or formatting inconsistencies.

DATASET: {dataset_name}
PROFILES:
{column_summaries}
"""
    return client.chat.completions.create(
        model="gpt-4o-mini",
        response_model=DataQualityAudit,
        messages=[{"role": "user", "content": prompt}],
        temperature=0.0
    )
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.