부분 데이터와 누락 데이터 처리
Optional 필드와 신뢰도 점수를 포함한 스키마를 설계하고, 모호한 문서를 위한 대체 추출 전략을 구현하며, 사람의 검토가 필요한 낮은 신뢰도 추출을 기록합니다.
부분 데이터와 누락 데이터 처리은(는) CoddyKit의 무료 AI Engineering Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 AI Engineering Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. AI Engineering Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
The Reality of Incomplete Documents
Real-world documents rarely contain every field your schema expects. An invoice might be missing a PO number, a resume might omit dates, and a news article might not mention a location. Designing your extraction schema to handle partial and missing data gracefully is as important as extracting what is present.
Optional Fields in Pydantic
Mark fields that might not appear in every document as Optional[type] and give them a None default. Pydantic v2 treats these fields as nullable, and the model is instructed not to hallucinate values when information is absent. Always prefer None over an empty string for missing data — it is easier to filter downstream.
from pydantic import BaseModel, Field
from typing import Optional
class JobPosting(BaseModel):
title: str
company: str
salary_min: Optional[float] = Field(None, description='Minimum salary if stated')
salary_max: Optional[float] = Field(None, description='Maximum salary if stated')
remote: Optional[bool] = Field(None, description='True if remote, False if on-site, None if unspecified')Adding Confidence Scores
Ask the model to rate its own confidence in each extracted field by adding a confidence score alongside the value. Wrap both in a generic FieldExtract helper. Low-confidence extractions can be routed to human reviewers rather than passed directly to downstream systems, reducing the risk of silently bad data.
from pydantic import BaseModel
from typing import Optional
class Confident(BaseModel):
value: Optional[str]
confidence: float # 0.0 to 1.0
class ContractExtract(BaseModel):
party_a: Confident
party_b: Confident
effective_date: Confident
termination_clause: ConfidentSentinel Values vs. None
Sometimes the absence of data is itself meaningful. Use Python's Optional plus a Literal enum to distinguish between not mentioned, explicitly stated as none, and unknown. This three-way distinction prevents downstream code from treating an explicitly stated absence the same as a missing mention, which can cause subtle business logic bugs.
from pydantic import BaseModel
from typing import Optional, Literal
class Discount(BaseModel):
# 'none' = explicitly no discount; None = not mentioned
discount_type: Optional[Literal['percentage', 'fixed', 'none']] = None
discount_value: Optional[float] = NoneFallback Extraction Strategies
When a primary extraction call returns too many None fields, try a targeted follow-up prompt that focuses specifically on the missing information. Send just the relevant paragraph alongside the partially extracted model and ask the model to fill only the empty fields. This two-pass approach significantly improves recall on ambiguous documents.
def fill_missing(partial: JobPosting, raw_text: str) -> JobPosting:
missing = [k for k, v in partial.model_dump().items() if v is None]
if not missing:
return partial
prompt = f'From this text, extract ONLY these fields: {missing}.\n\n{raw_text}'
supplement = client.chat.completions.create(
model='gpt-4o-mini',
response_model=JobPosting,
messages=[{'role': 'user', 'content': prompt}]
)
merged = partial.model_dump()
for field in missing:
if getattr(supplement, field) is not None:
merged[field] = getattr(supplement, field)
return JobPosting(**merged)Using Default Values and Factories
For fields that have a sensible default when missing, use Pydantic's default or default_factory. For example, a list of tags should default to an empty list rather than None so downstream code can always iterate over it. Reserve None for fields where the absence must be flagged and handled explicitly.
from pydantic import BaseModel, Field
from typing import List, Optional
class Article(BaseModel):
title: str
author: Optional[str] = None
tags: List[str] = Field(default_factory=list)
word_count: Optional[int] = None
published_date: Optional[str] = NoneRouting Low-Confidence Extractions
Build a review queue for extractions that have confidence below a threshold. Store low-confidence results in a separate database table with a needs_review flag, expose them in an internal review UI, and allow human annotators to correct them. Feed the corrections back as few-shot examples to improve future extractions.
CONFIDENCE_THRESHOLD = 0.75
def process_extraction(result: ContractExtract, doc_id: str):
needs_review = any(
field.confidence < CONFIDENCE_THRESHOLD
for field in [result.party_a, result.party_b, result.effective_date]
)
if needs_review:
queue_for_human_review(doc_id, result)
else:
store_in_production_table(doc_id, result)Handling Ambiguous Spans of Text
Some fields can be extracted in multiple valid ways from the same text. For example, a date written as 'next Monday' is ambiguous without a reference date. Use a raw_span field to capture the exact text the model used, alongside the normalized value. This preserves the original evidence and makes debugging extractions much easier.
from pydantic import BaseModel
from typing import Optional
class DateField(BaseModel):
raw_span: Optional[str] = None # exact text from document
iso_date: Optional[str] = None # normalized YYYY-MM-DD
confidence: float = 1.0
class Contract(BaseModel):
effective_date: DateField
expiration_date: DateFieldLogging Missing Field Patterns
Track which fields are most frequently None across your document corpus. High missing rates on a required field suggest either the field is genuinely absent in most documents, or your schema description is confusing the model. Logging missing patterns per document type helps you triage schema improvements that will have the biggest impact on data quality.
from collections import Counter
missing_counter = Counter()
def log_missing(result):
for field, value in result.model_dump().items():
if value is None:
missing_counter[field] += 1
# After processing 1000 documents:
for field, count in missing_counter.most_common(5):
print(f'{field}: {count} missing ({100*count//1000}%)')Combining Extraction Across Multiple Passes
For complex documents like annual reports or lengthy contracts, a multi-pass extraction strategy works best. In the first pass, extract high-confidence fields that are always present. In subsequent passes, focus on specific sections or paragraphs to extract the harder fields. Merge all passes into a single final record, letting later passes override earlier None values.
def multi_pass_extract(pages: list) -> Invoice:
# Pass 1: header info from first page
header = extract_header(pages[0])
# Pass 2: line items from middle pages
items = []
for page in pages[1:-1]:
items.extend(extract_line_items(page))
# Pass 3: totals from last page
totals = extract_totals(pages[-1])
return Invoice(
vendor=header.vendor,
invoice_number=header.invoice_number,
line_items=items,
total_amount=totals.total_amount
)Schema Design Best Practices
Well-designed schemas reduce missing data naturally. Use narrow, specific field descriptions so the model knows exactly what to look for. Avoid combining two concepts in one field. Add examples in the Field description to guide extraction. A schema that makes the model's job easy will have far fewer missing fields than one that relies on vague labels.
from pydantic import BaseModel, Field
class Address(BaseModel):
street: str = Field(description='Street number and name, e.g. 123 Main St')
city: str = Field(description='City name only, no state')
state: str = Field(description='Two-letter US state code, e.g. CA')
zip_code: str = Field(description='5-digit ZIP code, e.g. 94105')
country: str = Field(default='US', description='ISO 3166-1 alpha-2 country code')Quick Check
Test your understanding of handling partial and missing data in extraction pipelines.
Lesson Recap
In this lesson you learned: Optional fields with None defaults prevent hallucination of missing data, confidence scores and review queues create a safety net for uncertain extractions, and multi-pass extraction improves recall on complex documents by focusing each pass on specific sections. Next up we scale extraction with async processing and queues.
자주 묻는 질문
“부분 데이터와 누락 데이터 처리” 강의는 무료인가요?
네 — “부분 데이터와 누락 데이터 처리” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 AI Engineering Academy 강의 전체를 잠금 해제할 수 있습니다. AI Engineering Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“부분 데이터와 누락 데이터 처리”에서 뭘 배우나요?
Optional 필드와 신뢰도 점수를 포함한 스키마를 설계하고, 모호한 문서를 위한 대체 추출 전략을 구현하며, 사람의 검토가 필요한 낮은 신뢰도 추출을 기록합니다. 브라우저에서 직접 실행하는 실습 코드로 AI Engineering Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
AI Engineering Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 AI Engineering Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“부분 데이터와 누락 데이터 처리” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 AI Engineering Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 AI Engineering Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.