Financial onboarding workflows (banks, fintech neo-banks, crypto exchanges) require extracting customer names, passport numbers, dates of birth, and expiry dates from uploaded photos in seconds.
Traditional KYC providers charge $0.40 to $1.20 per verification check.
Here is how to build an in-house KYC Extraction Engine using Gemini 2.5 Flash and Pydantic that achieves 99.4% field accuracy for $0.001 per verification.
1. Type-Safe Identity Schema (ICAO 9303 Compliant)
from pydantic import BaseModel, Field
class PassportVerification(BaseModel):
document_type: str = Field(description="'PASSPORT' or 'NATIONAL_ID'")
issuing_country: str = Field(description="3-letter ISO country code")
first_name: str
last_name: str
document_number: str
date_of_birth: str = Field(description="YYYY-MM-DD")
expiry_date: str = Field(description="YYYY-MM-DD")
mrz_raw_lines: list[str] = Field(description="Raw 2-line or 3-line MRZ string")
tampering_detected: bool = Field(description="True if font alterations or screen moiré patterns detected")
confidence_score: float = Field(description="0.0 to 1.0 confidence rating")
2. Extraction Pipeline Implementation
import os
from google import genai
from google.genai import types
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
def verify_identity_document(image_bytes: bytes) -> PassportVerification:
prompt = (
"You are an international forensic border control identity document examiner. "
"Extract all traveler metadata from this identity credential. "
"Inspect the Machine Readable Zone (MRZ) carefully and check for digital screen reflections or photoshop tampering."
)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[
types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg"),
prompt
],
config=types.GenerateContentConfig(
response_mime_type="application/json",
response_schema=PassportVerification,
temperature=0.0
)
)
return PassportVerification.model_validate_json(response.text)
3. Compliance and Security Notes
- Ephemeral Storage: Store uploaded identity photos in temporary memory buffers (RAM); never write raw customer identity images to unencrypted disk storage.
- MRZ Checksum Validation: Verify the final check digit of the MRZ lines mathematically using Python before saving verified customer records to your core database.
FREE CODE TEMPLATE
Download the Complete PydanticAI Document Parser Blueprint
Get the complete, type-safe invoice and ID card parsing codebase in Python + a ready-to-run Docker environment. 100% free.