Data Extraction Patterns
Extract structured information from documents, emails, and other unstructured text.
Contact Information Extraction
class ContactInfo {
name string?
email string?
phone string?
company string?
}
function ExtractContact(text: string) -> ContactInfo {
client GPT4
prompt #"
Extract contact information from this text:
{{ text }}
Return any contact details found.
"#
}
Invoice Data Extraction
class LineItem {
description string
quantity int
unit_price float
total float
}
class Invoice {
invoice_number string
date string
vendor string
items LineItem[]
subtotal float
tax float
total float
}
function ExtractInvoice(document: string) -> Invoice {
client GPT4
prompt #"
Extract all invoice data from this document:
{{ document }}
"#
}
Handling Missing Data
Use optional fields (?) for data that may not be present:
class Person {
name string // Required
email string? // Optional
age int? // Optional
address Address? // Optional nested object
}
Best Practices
- Make fields optional when data might be missing
- Use descriptive names that match how data appears in text
- Add
@descriptionfor ambiguous fields
Related
- Classification - Categorize before extracting
- RAG Pipeline Tutorial - Extraction in context
- Type System - Optional types explained