Area: Best Practices
Sub-Area: AI-Powered Features / Context Documents
Applies to: OSS + Cloud
Issue
Teams that write Context Documents for AI agents (Ask DataHub on DataHub Cloud, and agents connected through the DataHub MCP server) ask how to structure them. They want to know when to use domain overview documents and when to use per-metric documents. They also want to know how to attach owners, a domain, tags and linked assets.
A common mistake is to put YAML frontmatter at the top of the document text and push it through the Python SDK or the GraphQL API. DataHub stores that text as the document body. It doesn't read the frontmatter. The imported documents then have no owners, no domain, no tags and no related assets.
You Might Be Asking
- Should I create separate context documents for domain overviews and individual metrics, or combine them?
- Is per-metric documentation too granular?
- Why do my imported context documents have no linked assets, domain, owners or tags?
- How do I pass frontmatter values so DataHub stores them as metadata?
- How do I link a context document to a dbt model or a metric?
Cause
When you create a document with Document.create_document(text=...) in the Python SDK, or with the createDocument GraphQL mutation, DataHub saves the text unchanged as the document's content (documentInfo.contents.text). A YAML block at the top of that text stays part of the body. Owners, domain, tags, related assets and related documents are separate fields. You must set them as separate fields.
Solution
1. Use two tiers of documents
- Domain overview documents cover what applies to all related metrics and datasets: entity disambiguation, date window rules, which tables to query first, and shared gotchas. Set the document's domain to your DataHub Domain. You can also add the domain as a related asset.
- Per-metric or per-dataset documents cover one metric or one table. Don't repeat the domain content. Add the metric or dataset as a related asset.
Link the two tiers in both directions with related documents: the overview points to each metric document, and each metric document points back to the overview. DataHub doesn't check that a related document exists, so the order of import doesn't matter.
Related documents travel with the asset. When an agent looks up a table, it gets the documents related to that table along with the schema. See Context Documents.
2. Per-metric documents are a good level of detail
Per-metric documents work well. Give each one these sections:
- Disambiguation: the questions an agent must resolve before it answers, such as which queue type, overall or first-time, which slice, and which time window.
- Metric-specific gotchas: edge cases that don't belong in the domain overview.
Don't copy column descriptions into the document. If you ingest dbt, those descriptions already come in with the dbt models.
If you include snapshot numbers (for example, a metric's baseline value), record the date in the document and review it on a schedule. A stale number makes an agent give a confident wrong answer.
3. Pass frontmatter values as fields, not as text
Parse the frontmatter yourself, then pass each value to Document.create_document. Put only the body in text. The Documents API tutorial shows each field.
Example file context-docs/support-first-response-time.md:
---
id: support-first-response-time
title: First Response Time
type: Metric Doc
owners:
- urn:li:corpuser:jdoe
domain: urn:li:domain:support
tags:
- urn:li:tag:support-metrics
related_assets:
- urn:li:dataset:(urn:li:dataPlatform:dbt,analytics.support.fct_tickets,PROD)
related_documents:
- urn:li:document:support-domain-overview
last_verified: 2026-11-01
verified_by: jdoe
---
# First Response Time
## Disambiguation
...
Import script (needs the acryl-datahub package and a connection set up with datahub init):
from pathlib import Path
import yaml
from datahub.sdk import DataHubClient, Document
client = DataHubClient.from_env()
for path in sorted(Path("context-docs").glob("*.md")):
_, frontmatter, body = path.read_text().split("---", 2)
meta = yaml.safe_load(frontmatter)
doc = Document.create_document(
id=meta["id"],
title=meta["title"],
text=body.strip(),
subtype=meta.get("type"),
owners=meta.get("owners", []),
domain=meta.get("domain"),
tags=meta.get("tags", []),
related_assets=meta.get("related_assets", []),
related_documents=meta.get("related_documents", []),
custom_properties={
key: str(meta[key])
for key in ("last_verified", "verified_by", "snapshot_as_of")
if key in meta
},
)
client.entities.upsert(doc)
print(f"Imported {doc.urn}")
Things to know:
- The document URN is
urn:li:document:<id>, for exampleurn:li:document:support-first-response-time. Use the same ids inrelated_documents. - Owners passed as plain URNs get the Technical Owner type. To pick another type, pass a tuple, for example
("urn:li:corpuser:jdoe", "BUSINESS_OWNER"). - Keys with no matching field (
last_verified,verified_by,snapshot_as_of) go into custom properties as text. -
client.entities.upsertwrites the title, text, related links, owners, tags and domain from your file. If you run the script again, it replaces those values in DataHub with the ones in your files, including edits that people made in the UI. - To add a link to a document that already exists without rewriting it, load it with
client.entities.get, calldoc.add_related_asset(...)ordoc.add_related_document(...), then upsert it. The GraphQLupdateDocumentRelatedEntitiesmutation does the same. - The GraphQL
createDocumentmutation acceptsowners,relatedAssetsandrelatedDocuments, but it has no domain or tags input. Use the Python SDK when you want to set everything in one step.
4. Agree on one frontmatter schema before you scale
Before your team writes many documents, fix the set of keys and map each one to a field:
| Frontmatter key |
create_document argument |
|---|---|
id |
id |
title |
title |
type |
subtype |
owners |
owners |
domain |
domain |
tags |
tags |
related_assets |
related_assets |
related_documents |
related_documents |
| anything else | custom_properties |
Check the frontmatter in CI so a malformed file fails before import.
5. Build dbt URNs that match the ingested entities
A related asset only helps if its URN matches an entity that exists in DataHub. The dbt source names each dbt dataset <database>.<schema>.<alias or name>, and lowercases it unless your dbt recipe sets convert_urns_to_lowercase: false. A schema.name URN with no database doesn't match.
If you build URNs from manifest.json:
import json
from datahub.emitter.mce_builder import make_dataset_urn
with open("target/manifest.json") as f:
manifest = json.load(f)
for node in manifest["nodes"].values():
if node["resource_type"] not in ("model", "seed", "snapshot"):
continue
name = node.get("alias") or node["name"]
parts = [node.get("database"), node["schema"], name]
fqn = ".".join(p for p in parts if p).replace('"', "").lower()
print(node["unique_id"], make_dataset_urn(platform="dbt", name=fqn, env="PROD"))
Drop .lower() if your recipe sets convert_urns_to_lowercase: false. If your recipe sets platform_instance or a different env, the URN changes too. Look up a few of the URNs in DataHub before a bulk import.
Additional Notes
Metrics and Semantic Models need DataHub Cloud 2.1.0 or later and are on by default from 2.3.0. On DataHub Cloud with Metrics on, the dbt source creates metric entities from your dbt semantic layer. The emit_semantic_model_entities recipe option controls this. Top-level metrics: definitions come in from dbt Core only. Use the latest acryl-datahub release for your dbt ingestion. Once your dbt metrics are in DataHub, find each metric, copy its URN, and add it as a related asset of the per-metric document. Otherwise, link the per-metric document to the dataset behind the metric. See Metrics and Semantic Models.
Related Documentation
Tags: context-documents, best-practices, yaml-frontmatter, asset-linking, ai-features, ask-datahub, dbt, metrics, python-sdk