---
title: "How do I gather a full Entity ontology in Python?"
section: "Libraries: Python"
route: /faqentityontologyinpython
account: {accountId}
bruce_api: https://{accountId}.api.nextspace.host
guardian_api: https://guardian.nextspace.host
---
# How do I gather a full Entity ontology in Python?

It is common to want to get all Entity records and associated concepts in Python for processing or analysis.

Here is a simple example that will perform a Data Lab action to export the results to JSON.

The export result will be a ZIP file containing JSON files (100k Entities per file) for a query and related concepts of interest. Attachments and LODs will include their download URLs and thumbnail URLs if available.

You will want to install our Python library to facilitate this process.

```bash
pip install bruce-models
```

**Python**

```python
import bruce_models as BM

ENTITY_TYPE_ID = "YOUR_ENTITY_TYPE_ID"
# Either a session token from a login or a long-lived access token.
TOKEN = "YOUR_TOKEN"

# Setup the API instance to communicate with your account.
bruce = BM.BruceApi({
    "account_id": "{accountId}",
    "session_id": TOKEN
})

# The selection to export. Nothing is requested until an action runs.
query = BM.DataLabBuilder(bruce).entity_type(ENTITY_TYPE_ID)

def start_job(query):
    """
    Starts a Data Lab job to export Entities and their related concepts.
    """
    response = query.run_action_export(
        "export-json",
        expand="EntityType,LOD,Source,Relation,Attachment"
    )
    return BM.DataLabBuilder.action_id(response)

def await_job(bruce, action_id):
    """
    Awaits for the Data Lab job to complete.
    """

    result = BM.PendingAction.on_completion(
        bruce,
        action_id,
        on_progress=lambda a: print(f"Job status: {a.get('Status')} {a.get('Progress')}%")
    )
    action = result.get("action")

    # A failed job returns rather than raising, so the messages are available.
    if action.get("Status") != BM.EStatus.COMPLETE:
        for message in result.get("messages"):
            print(message.get("Message"))
        raise Exception(f"Job failed with status: {action.get('Status')}")

    return action

job_id = start_job(query)
action = await_job(bruce, job_id)

# The export lands in the account's temporary store.
print("Download URL:", BM.DataLabBuilder.export_url(action))
print("File name:", BM.DataLabBuilder.export_file_name(action))
```

---

Urls on this page are resolved for account `{accountId}`.
Site index: https://docs.nextspace.host/llms.txt · whole site in one file: https://docs.nextspace.host/llms-full.txt
Human-readable version of this page: https://docs.nextspace.host/faqentityontologyinpython
