Users of our Python client library can generate pydantic models to help with validating and consuming the API response in a type-safe way. Use the generate_models command to auto-generate pydantic models from your document type configuration.

Pydantic models

For example, this will generate pydantic models for your workspace:
# Assuming your workspace ID is rLERIsHk
python -m affinda generate_models --workspace-id=rLERIsHk
You will be prompted for your API key, unless you already have the AFFINDA_API_KEY environment variable set. The generated pydantic model(s) will be in ./affinda_models by default. For the list of options available, run:
python -m affinda generate_models --help

Example of using the generated pydantic models

from pathlib import Path
from affinda import AffindaAPI, TokenCredential
from affinda_models.resume_parser import ResumeParser

API_KEY = "YOUR_API_KEY"                      # replace with your actual key
WORKSPACE_ID = "YOUR_WORKSPACE_IDENTIFIER"    # e.g. "vBAdDBer"
FILE_PATH = Path("resume.pdf")                # path to the résumé you want to parse

# Set up the client
credential = TokenCredential(token=API_KEY)
client = AffindaAPI(credential=credential)

# This will raise `pydantic_core.ValidationError` if the API response
# does not validate against ResumeParser
with FILE_PATH.open("rb") as f:
    doc = client.create_document(
        file=f,
        workspace=WORKSPACE_ID,
        data_model=ResumeParser,
    )

# Access parsed data
print(doc.parsed)  # This is a ResumeParser instance
print(doc.data)  # This is the raw JSON response

# This will NOT raise `pydantic_core.ValidationError` if the API response 
# does not validate against ResumeParser.
# Instead `parsed` will be None if the API response is not compatible.
with FILE_PATH.open("rb") as f:
    doc = client.create_document(
        file=f,
        workspace=WORKSPACE_ID,
        data_model=ResumeParser,
        ignore_validation_errors=True,
    )

if doc.parsed:
    print("API response is valid.")
    print(doc.parsed)
else:
    print("API response is invalid.")
    print(doc.data)  # The raw JSON response is still available