Home Knowledge Base Data Extraction with LLMs

Data Extraction with LLMs

Unstructured to Structured Extraction LLMs excel at extracting structured data from unstructured text, emails, documents, and web pages.

Basic Extraction

def extract_data(text: str, fields: list) -> dict:
    return llm.generate(f"""
Extract the following information from the text as JSON:
Fields: {fields}

Text:
{text}

JSON output:
    """)

Structured Extraction with Pydantic

from pydantic import BaseModel
import instructor

class Invoice(BaseModel):
    vendor_name: str
    invoice_number: str
    date: str
    line_items: list[dict]
    total: float
    currency: str

client = instructor.from_openai(OpenAI())

invoice = client.chat.completions.create(
    model="gpt-4o",
    response_model=Invoice,
    messages=[{"role": "user", "content": f"Extract invoice: {text}"}]
)

Document Types

DocumentExtraction Fields
InvoiceVendor, items, totals, dates
ContractParties, terms, dates, values
ResumeName, experience, skills, education
ReceiptMerchant, items, amount, date
EmailSender, subject, action items, dates

Multi-Document Extraction

def batch_extract(documents: list, schema: dict) -> list:
    results = []
    for doc in documents:
        result = extract_with_schema(doc, schema)
        results.append(result)
    return results

Web Scraping with LLM

def extract_from_html(html: str, target: str) -> dict:
    return llm.generate(f"""
From this HTML, extract: {target}

HTML (cleaned):
{clean_html(html)}

Extracted data (JSON):
    """)

Validation and Post-Processing

def extract_with_validation(text: str, schema: BaseModel) -> BaseModel:
    extracted = llm_extract(text)

    try:
        validated = schema.model_validate(extracted)
    except ValidationError as e:
        # Self-correction
        corrected = llm.generate(f"""
Fix this extraction to match schema:
Extracted: {extracted}
Errors: {e}
Schema: {schema.model_json_schema()}
        """)
        validated = schema.model_validate(corrected)

    return validated

Best Practices

data extractionparsingscraping

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.