> ## Documentation Index
> Fetch the complete documentation index at: https://docs.affinda.com/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.affinda.com/feedback

```json
{
  "path": "/reference/pydantic-models",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Structured Outputs

> Generate structured outputs using Pydantic models from your document types and ensure type-safe validation of Affinda API responses.

Users of our [Python client library](https://pypi.org/project/affinda/) 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:

```shell theme={null}
# 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:

```shell theme={null}
python -m affinda generate_models --help
```

### Example of using the generated pydantic models

```python theme={null}
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
```
