> ## 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.

# Taxonomies & Tags

> Define what metadata to extract from your documents

**Tags** are the metadata attributes you want to extract or classify from your documents. **Taxonomies** organize tags into hierarchical structures that define parent-child relationships.

## What is a Tag?

A Tag defines a specific piece of information you want to capture from documents:

| Property | Description | Example |
| :- | :- | :- |
| **Name** | The tag identifier | `Contract Type` |
| **Description** | Instructions for the AI on what to extract | "Identify the type of legal agreement (NDA, MSA, SOW, etc.)" |
| **Output Type** | How values are returned | String, Binary, Number, Date |
| **Max Values** | How many values get returned | 1 to however many relevant values an AI can find |
| **Available Values** | Predefined options (for classification) | `["NDA", "MSA", "SOW", "Employment Agreement"]` |
| **Strategy** | Extraction method | LLM (AI), Regex (Pattern), Rule-based |

## Tag Types

| Tag Type | How It Works | When to Use | Example |
| :- | :- | :- | :- |
| **Classification Tags** | AI chooses from predefined list of values | When you have a known set of categories | Document Type: Contract, Invoice, Report |
| **Extraction Tags** | AI extracts open-ended values from text | When the value is unpredictable | Contract Value: \$1,500,000 |
| **Pattern Tags** | Regex plus context keywords that must appear nearby | For compliance, keyword-search, etc. | SSN: XXX-XX-XXXX, Email: [user@domain.com](mailto:user@domain.com) |

> [!TIP]
>
> **Classification** is best when you have a known set of categories. **Extraction** is best for unpredictable values like names, dates, or amounts. **Pattern** is best for structured data like phone numbers or SSNs.

## Three Ways to Define a Tag

Every tag resolves its value through one of three strategies:

| Strategy | How the value is decided | Cost | Example |
| :- | :- | :- | :- |
| **AI (LLM)** | The model reads the document and follows your description | LLM call per document | `summary`, `contract_type` |
| **Pattern (Regex + context)** | Deterministic matching, corroborated by context keywords | No LLM | `ssn_matches`, employee IDs |
| **Rule-based** | Deterministic conditions over *other tags'* values | No LLM | `AI Agent Access = Restricted` if `PII = true` |

Pattern tags are not bare regex. Each pattern carries **context keywords** (`context_items`) and a `required_context_matches` threshold: a nine-digit number only counts as an SSN when words like "social security" or "SSN" appear near it. That corroboration is what keeps precision high on real documents, and the platform can suggest both the pattern and its context keywords from your data. See [Precision Patterns for Sensitive Data](https://docs.deasylabs.com/cookbooks/precision-patterns.md).

Rule-based tags evaluate after their input tags exist: if a rule matches, the value is resolved deterministically; if no rule matches, the tag falls back to its other configured strategy, pattern matching or LLM classification, depending on the tag definition. Because rules run at zero LLM cost, they are the right tool for policy verdicts derived from other signals (access gates, retention classes, readiness flags).

## Taxonomy Structure

A Taxonomy enables hierarchical organization and conditional extraction. Child tags only get generated when parent conditions are met:

```
Document Type (Classification: Contract | Invoice | Report)
├── Contract
│   ├── Contract Value (Extraction)
│   ├── Parties Involved (Extraction)
│   ├── Effective Date (Extraction)
│   └── Termination Date (Extraction)
├── Invoice
│   ├── Invoice Amount (Extraction)
│   ├── Due Date (Extraction)
│   └── Vendor Name (Extraction)
└── Report
    ├── Report Category (Classification: Financial | Operational | Compliance)
    └── Report Period (Extraction)
```

```mermaid theme={"dark"}
flowchart TD
    DT[Document Type] --> C[Contract]
    DT --> I[Invoice]
    DT --> R[Report]
    
    C --> CV[Contract Value]
    C --> PI[Parties Involved]
    C --> ED[Effective Date]
    
    I --> IA[Invoice Amount]
    I --> DD[Due Date]
    I --> VN[Vendor Name]
    
    R --> RC[Report Category]
    R --> RP[Report Period]
```

> [!NOTE]
>
> In this taxonomy, the AI first classifies the document type, then only extracts the relevant child tags. A Contract won't have "Invoice Amount" extracted, which saves time and cost.

## Creating Effective Tags

**1. Define Clear Descriptions**

Give the AI specific instructions about what to extract. The better your description, the more accurate the extraction.

**2. Choose the Right Type**

Use Classification for known categories, Extraction for open-ended values, and Pattern for structured formats.

**3. Set Available Values**

For Classification tags, provide a complete list of possible values to improve accuracy.

**4. Organize into Taxonomies**

Group related tags hierarchically to enable conditional extraction and reduce unnecessary processing.

***

## Python SDK

> [tab] Create Tags
>
> ```python theme={"dark"}
> from deasy import DeasyClient
>
> client = DeasyClient(
>     base_url="https://unstructured.your-company.com/rest/unstructured",
>     username="your-username",
>     password="your-password",
> )
>
> # Create or update tags, the metadata attributes you want to extract
> for tag in [
>     {
>         "name": "contract_type",
>         "description": "Type of contract (NDA, MSA, SLA, etc.)",
>         "output_type": "string",
>         "available_values": ["NDA", "MSA", "SLA", "Employment Agreement"],
>     },
>     {
>         "name": "parties",
>         "description": "Names of all parties involved",
>         "output_type": "string",
>         "max_values": 10,
>     },
>     {
>         "name": "effective_date",
>         "description": "When the contract becomes effective",
>         "output_type": "date",
>     },
>     {
>         "name": "total_value",
>         "description": "Total monetary value in USD",
>         "output_type": "number",
>     },
> ]:
>     result = client.tags.upsert(tag_data=tag)
> ```

> [tab] Create Taxonomy
>
> ```python theme={"dark"}
> # Organize tags into a taxonomy, a node/edge graph where child tags
> # are only extracted when the parent condition is met
> taxonomy = client.taxonomy.upsert(
>     taxonomy_name="contract-analysis",
>     taxonomy_description="Extract key data from legal contracts",
>     taxonomy_data={
>         "nodes": [
>             {"node_id": "1", "name": "contract_type",
>              "active_values": ["NDA", "MSA", "SLA"]},
>             {"node_id": "2", "name": "total_value"},
>         ],
>         "edges": [
>             # Only extract total_value for MSA contracts
>             {"target_node_ids": ["2"],
>              "conditions": [{"node_id": "1", "tag_value_name": "MSA"}]},
>         ],
>     },
> )
> ```

> [tab] Pattern & Rule Tags
>
> ```python theme={"dark"}
> # Pattern tag: deterministic regex matching, no LLM cost
> client.tags.upsert(tag_data={
>     "name": "ssn_matches",
>     "description": "US Social Security Numbers found in the document",
>     "output_type": "string",
>     "patterns": [{"pattern": r"\b\d{3}-\d{2}-\d{4}\b"}],
> })
>
> # Rule-based tag: a deterministic verdict derived from other tags.
> # If a rule matches, the value resolves with no LLM call; if none
> # match, the tag falls back to its other configured strategy
> # (pattern matching or LLM classification, per the tag definition).
> client.tags.upsert(tag_data={
>     "name": "AI Agent Access",
>     "description": "Whether AI agents may access this document",
>     "output_type": "string",
>     "available_values": ["Restricted", "Allowed"],
>     "visual_rules": [
>         {
>             "condition": {
>                 "condition": "OR",
>                 "children": [
>                     {"tag": {"name": "PII (Personally Identifiable Information)",
>                              "operator": "in", "values": ["true"]}},
>                     {"tag": {"name": "PHI (Protected Health Information)",
>                              "operator": "in", "values": ["true"]}},
>                 ],
>             },
>             "tag_value": "Restricted",
>         },
>     ],
> })
> ```

> [tab] AI Suggestions
>
> ```python theme={"dark"}
> # Get an AI-suggested taxonomy from a description of your data (nothing is saved)
> suggestion = client.taxonomy.generate_from_context(
>     user_context="Medical patient records with diagnoses and prescriptions",
> )
> print(suggestion.taxonomy_data)
>
> # Get an AI-suggested regex pattern for a Pattern tag
> pattern = client.tags.pattern.suggest_patterns(
>     pattern_description="US Social Security Number (XXX-XX-XXXX)",
>     tag_data={"name": "ssn", "description": "US Social Security Number"},
> )
> print(f"Suggested pattern: {pattern.regex} (confidence: {pattern.confidence_score})")
> ```

> [tab] List & Delete
>
> ```python theme={"dark"}
> # List all taxonomies
> taxonomies = client.taxonomy.list()
> for t in taxonomies.taxonomies:
>     print(t.taxonomy_name)
>
> # List all tags
> tags = client.tags.list()
> for tag in tags.tags:
>     print(f"{tag.name} ({tag.output_type})")
>
> # Delete a taxonomy
> client.taxonomy.delete(taxonomy_name="old-taxonomy")
>
> # Delete a tag
> client.tags.delete(tag_name="unused-tag")
> ```

***

## API Reference

> [card] [Upsert Tag](https://docs.deasylabs.com/api-reference/tags/upsert.md)
>
> Create or update a tag

> [card] [List Tags](https://docs.deasylabs.com/api-reference/tags/list.md)
>
> List all your tags

> [card] [Delete Tag](https://docs.deasylabs.com/api-reference/tags/delete.md)
>
> Remove a tag

> [card] [Suggest Patterns](https://docs.deasylabs.com/api-reference/tags/suggest-patterns.md)
>
> AI-powered pattern suggestions

> [card] [Upsert Taxonomy](https://docs.deasylabs.com/api-reference/taxonomies/upsert.md)
>
> Create or update a taxonomy

> [card] [List Taxonomies](https://docs.deasylabs.com/api-reference/taxonomies/list.md)
>
> List all your taxonomies

> [card] [Delete Taxonomy](https://docs.deasylabs.com/api-reference/taxonomies/delete.md)
>
> Remove a taxonomy

> [card] [Suggest Taxonomy](https://docs.deasylabs.com/api-reference/taxonomies/suggest-a-taxonomy-from-a-description.md)
>
> AI-powered taxonomy suggestions


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