Area: API Issues
Sub-Area: Python SDK & GraphQL, Context Documents
Applies to: OSS + Cloud
Issue
You create Context Documents with the Python SDK or the GraphQL API, and you want images inside the document text. In the DataHub editor you can drag an image in or use the upload button. With the API you have to do this yourself, and it isn't clear where the image must live so that it renders in the DataHub UI.
You Might Be Asking
- How do I embed an image in a Context Document using the Python SDK?
- Where does the image need to be hosted for it to render?
- Can I upload an image into DataHub and reference it from a document through the API?
- Does the Python SDK have an
upload_image()method?
Solution
Document text is markdown. You embed an image with a standard markdown image tag, . You have two ways to get an image URL.
Option 1: Reference an image hosted somewhere else
- Put the image on any HTTPS host that your readers' browsers can load.
- Reference it in the
textyou pass toDocument.create_document(or toset_texton an existing document), then upsert the document:
from datahub.sdk import DataHubClient, Document
client = DataHubClient.from_env()
doc = Document.create_document(
id="my-document",
title="My Document",
text=(
"Here is an embedded image:\n\n"
"\n\n"
"More document content."
),
)
client.entities.upsert(doc)
DataHub does not copy the image. The reader's browser loads it from your host each time the document is viewed. An image behind a VPN or private network renders only for readers who are on that network.
See the Documents API tutorial for creating and updating documents with the SDK.
Option 2: Upload the image into DataHub through the GraphQL API
This is the same flow the editor's upload button uses. The image is stored in DataHub's file storage, and DataHub checks that each reader can view the document before it serves the image.
Before you start:
- The DataHub instance needs file storage set up. If it isn't, the upload query fails with
Object storage is not configured. Self-hosted deployments set this up on GMS as described in Attaching Files to Documentation. On DataHub Cloud, contact DataHub Support. - The token you use needs the Edit Description privilege on the document.
- The image is attached to one asset, so create the document first and use its URN.
The steps are:
- Create the document so it has a URN.
- Call the
getPresignedUploadUrlquery. It is a query, not a mutation. Passscenario: ASSET_DOCUMENTATIONand setassetUrnto the document URN; withoutassetUrnthe query fails withassetUrn is required for ASSET_DOCUMENTATION scenario. The response has two fields,url(where you upload) andfileId. - Send an HTTP
PUTwith the image bytes tourl. Use the sameContent-Typeyou passed ascontentTypein step 2. - Register the file with the
createDataHubFilemutation. Its inputs areid(the part offileIdbefore the first__),storageKey,originalFileName,mimeType,sizeInBytes,scenario, andreferencedByAsset(the document URN). Always setreferencedByAsset; DataHub uses it to decide who can view the image. - Build the image URL yourself. The mutation does not return one. The URL is
<your DataHub address>/openapi/v1/files/product_assets/<fileId, URL-encoded>, using the fullfileIdfrom step 2. - Put that URL in a markdown image tag in the document text and upsert the document.
This script runs all six steps with the Python SDK:
import os
import urllib.parse
import requests
from datahub.ingestion.graph.client import get_default_graph
from datahub.sdk import DataHubClient, Document
# The address you open DataHub at in a browser, with no trailing slash.
DATAHUB_URL = "https://your-datahub-host"
IMAGE_PATH = "diagram.png"
MIME_TYPE = "image/png"
graph = get_default_graph()
client = DataHubClient(graph=graph)
# Step 1: create the document so the image can be attached to its URN.
doc = Document.create_document(id="my-document", title="My Document", text="Draft")
client.entities.upsert(doc)
doc_urn = str(doc.urn)
file_name = os.path.basename(IMAGE_PATH)
with open(IMAGE_PATH, "rb") as f:
image_bytes = f.read()
# Step 2: get a presigned upload URL (a GraphQL query).
result = graph.execute_graphql(
"""
query getUploadUrl($input: GetPresignedUploadUrlInput!) {
getPresignedUploadUrl(input: $input) {
url
fileId
}
}
""",
variables={
"input": {
"fileName": file_name,
"contentType": MIME_TYPE,
"scenario": "ASSET_DOCUMENTATION",
"assetUrn": doc_urn,
}
},
)
upload_url = result["getPresignedUploadUrl"]["url"]
file_id = result["getPresignedUploadUrl"]["fileId"]
# Step 3: upload the image bytes.
response = requests.put(upload_url, data=image_bytes, headers={"Content-Type": MIME_TYPE})
response.raise_for_status()
# Step 4: register the file in DataHub.
graph.execute_graphql(
"""
mutation createFile($input: CreateDataHubFileInput!) {
createDataHubFile(input: $input) {
file {
urn
}
}
}
""",
variables={
"input": {
"id": file_id.split("__")[0],
"storageKey": f"product_assets/{file_id}",
"originalFileName": file_name,
"mimeType": MIME_TYPE,
"sizeInBytes": len(image_bytes),
"scenario": "ASSET_DOCUMENTATION",
"referencedByAsset": doc_urn,
}
},
)
# Step 5: build the image URL.
image_url = f"{DATAHUB_URL}/openapi/v1/files/product_assets/{urllib.parse.quote(file_id, safe='')}"
# Step 6: embed the image and save the document.
doc.set_text(f"Here is the diagram:\n\n\n\nMore document content.")
client.entities.upsert(doc)
The image URL does not expire. Each time someone opens it, DataHub checks that they can view the document and then redirects them to a short-lived link to the stored file. Readers without access to the document can't load the image, even with the direct URL.
If your self-hosted GMS sets DATAHUB_S3_ASSET_PATH_PREFIX, use that value in place of product_assets in storageKey and in the image URL.
Comparison
| Method | Where the image lives | Who can see it |
|---|---|---|
| Option 1, external URL | Your own host | Anyone whose browser can reach your host |
| Option 2, GraphQL upload | DataHub file storage | Users who can view the document |
Additional Notes
The Python SDK has no upload_image() method. To store an image in DataHub, use the GraphQL calls in Option 2. Once you have an image URL from either option, Document.create_document and set_text work as usual.
The document stores its content as markdown text. Images are always referenced by URL inside that text, never stored as binary data in the document itself.