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

# Bootstrap a Taxonomy with AI

> Use AI to automatically generate domain-specific taxonomies for your documents

This cookbook shows you how to use the platform's AI to automatically generate custom taxonomies. Instead of manually defining tags one by one, you can describe your use case in natural language, and the AI will propose a complete taxonomy for you.

## AI-Generated Taxonomies

`taxonomy.generate_from_context` bootstraps a taxonomy from a free-form description of your data. Nothing is saved, so you can review the result first:

```python theme={"dark"}
from deasy import DeasyClient

client = DeasyClient(
    base_url="https://unstructured.your-company.com/rest/unstructured",
    username="your-username",
    password="your-password",
)

suggestion = client.taxonomy.generate_from_context(
    user_context="""
    I need to analyze legal contracts. Extract key terms, financial obligations,
    risk indicators, important dates, and anything relevant for compliance.
    Focus on NDAs, MSAs, and employment agreements.
    """,
)

# Review the suggestion before saving anything
print(suggestion.taxonomy_description)
print(suggestion.taxonomy_data)  # the taxonomy graph: nodes (tags) and edges (conditions)
print(suggestion.suggested_tags)
```

Optional parameters steer the result: `industry`, `data_type`, `use_case`, `taxonomy_type` (default `hierarchical`) and `max_depth` (default 3).

## Validate Against Your Documents

For higher accuracy, pass the data source's id and set `validate_tags=True`. The platform samples that data source's files and scores the suggested tags against them; `tag_not_found_rates` reports how often each tag was not found in the sample.

```python theme={"dark"}
page = client.get("/v2/datasources", options={"params": {"name": "my-documents"}}, cast_to=object)
rows = page["results"]
# Prefer the connection you own if a shared one has the same name
datasource_id = next((r["datasource_id"] for r in rows if r["is_owner"]), rows[0]["datasource_id"])

suggestion = client.taxonomy.generate_from_context(
    user_context="Extract key data from these vendor invoices",
    datasource_id=datasource_id,
    validate_tags=True,
)

for tag, rate in (suggestion.tag_not_found_rates or {}).items():
    if rate >= 0:
        print(f"{tag}: not found in {rate:.0%} of sampled files")
```

A rate of `-1` means there is no score for that tag. Validation was not requested, no sample files were found, or validation failed. Without a data source and `validate_tags=True`, every tag reports `-1`.

> [!NOTE]
>
> `generate_from_context` takes the data source by id only. The lookup above is the same name-to-id lookup described in the SDK README.

## Create the Tags

Once you are happy with the suggestion, create its tags with `tags.upsert`. Each entry in `suggested_tags` is keyed by tag name:

```python theme={"dark"}
for name, tag in suggestion.suggested_tags.items():
    client.tags.upsert(tag_data={
        "name": name,
        "description": tag["description"],
        "output_type": tag["output_type"],
    })
```

Add tags the AI missed the same way:

```python theme={"dark"}
client.tags.upsert(tag_data={
    "name": "reviewed_by_legal",
    "description": "Whether this contract has been reviewed",
    "output_type": "binary",
})
```

> [!NOTE]
>
> The suggested graph names its nodes by tag name, while a saved taxonomy refers to tags by id, so don't pass `suggestion.taxonomy_data` to `taxonomy.upsert` as returned. Use the suggestion as a blueprint for the tags above.

## Common Use Cases

> [tab] Financial Reports
>
> ```python theme={"dark"}
> client.taxonomy.generate_from_context(
>     user_context="""
>     Analyze quarterly financial reports. Extract revenue, earnings,
>     growth metrics, risk factors, and forward guidance.
>     """,
>     industry="finance",
> )
> ```

> [tab] Resumes
>
> ```python theme={"dark"}
> client.taxonomy.generate_from_context(
>     user_context="""
>     Process resumes for technical recruiting. Extract skills,
>     experience level, education, and certifications.
>     """,
>     use_case="recruiting",
> )
> ```

> [tab] Insurance Claims
>
> ```python theme={"dark"}
> client.taxonomy.generate_from_context(
>     user_context="""
>     Analyze insurance claims. Extract claim details, incident info,
>     injury status, and potential fraud indicators.
>     """,
>     industry="insurance",
> )
> ```

## Next Steps

> [card] [Organize a SharePoint Library](https://docs.deasylabs.com/cookbooks/sharepoint-to-sharepoint.md)
>
> Enrich libraries at the source with your taxonomy.

> [card] [Clean Up a RAG Index](https://docs.deasylabs.com/cookbooks/qdrant-to-qdrant.md)
>
> Curate a serving collection with your taxonomy.

> [card] [Protect Sensitive Data](https://docs.deasylabs.com/cookbooks/pii-detection.md)
>
> Add sensitive data detection.

> [card] [Taxonomies Concept](https://docs.deasylabs.com/concepts/taxonomies-tags.md)
>
> Learn the fundamentals of taxonomies.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.