Modify documents metadata through API #26688
Self Checks
1. Is this request related to a challenge you're experiencing? Tell me about your story.I created a custom "uuid" metadata field in my knowledge base, to have personalized filtering based on who is using the chatbot linked to this knowledge base. 2. Additional context or commentsNo response |
Replies: 2 comments 2 replies
|
You can update document metadata (including custom fields like "uuid") in Dify using the API endpoint: Your request should include a JSON payload with two fields: Example Python snippet using import requests
url = "https://your-dify-instance/api/datasets/{dataset_id}/documents/{document_id}/metadata"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"doc_type": "others", # or your specific type
"doc_metadata": {
"uuid": "your-uuid-value",
# ...other metadata fields
}
}
response = requests.put(url, json=payload, headers=headers)
print(response.json())If you need to update many documents, fetch each document’s current metadata, merge your changes, and send the full metadata in each update request. More details on the endpoint and its behavior are in the API implementation and community discussions. If you’ve defined your custom metadata fields at the dataset level, this approach will work for your "uuid" field. If you haven’t, make sure to do so first—otherwise, only built-in fields will be updated or shown (discussion). To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
|
Apparently in the new v1.9.1 documentation, the endpoint to update metadata is not mentioned. import requests
dataset_id = "yourdatasetid"
document_id = "yourdocumentid"
uuid_metadata_id = "yourmetadataid"
dns = "yourdns"
url = f"https://{dns}/v1/datasets/{dataset_id}/documents/metadata"
payload = {
"operation_data": [
{
"document_id": document_id,
"metadata_list": [
{
"id": uuid_metadata_id,
"value": "mynewbeatuifulvalue",
"name": "uuid"
}
]
}
]
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
print("Metadata updated successfully:", response.json())
except requests.exceptions.RequestException as e:
print("Error updating metadata:", e) |
Apparently in the new v1.9.1 documentation, the endpoint to update metadata is not mentioned.
Still, I was able to use the endpoint mentioned in previous versions API docs, even using dify v1.9.1.
Below the python code, hope it helps: