Implementation:Langgenius Dify ModifyDocMetadata
| Knowledge Sources | Domains | Last Updated |
|---|---|---|
| Dify | RAG, Knowledge_Management, Frontend | 2026-02-12 00:00 GMT |
Overview
Description
modifyDocMetadata is a frontend service function that updates the document type and metadata for a specific document within a Dify knowledge base. It sends a PUT request to the metadata endpoint, allowing callers to classify documents and attach structured key-value metadata.
In addition to this metadata function, Dify provides a comprehensive set of segment management hooks in web/service/knowledge/use-segment.ts built on TanStack React Query. These hooks cover the full lifecycle of segments: listing, creating, updating, deleting, enabling, and disabling, as well as child chunk operations for hierarchical mode.
Usage
- Call
modifyDocMetadatato set or update a document's type (e.g.,book,web_page,paper) and its associated metadata fields. - Use the segment hooks (
useSegmentList,useUpdateSegment,useAddSegment,useDeleteSegment,useEnableSegment,useDisableSegment) for granular management of individual chunks within a document.
Code Reference
Source Location
Metadata function: web/service/datasets.ts, lines 186--188.
Segment hooks: web/service/knowledge/use-segment.ts.
Signature
modifyDocMetadata:
export const modifyDocMetadata = (
{ datasetId, documentId, body }: CommonDocReq & {
body: { doc_type: string, doc_metadata: Record<string, any> }
}
): Promise<CommonResponse> => {
return put<CommonResponse>(
`/datasets/${datasetId}/documents/${documentId}/metadata`, { body }
)
}
Segment hooks (selected):
export const useSegmentList = (payload: {
datasetId: string
documentId: string
params: { page: number, limit: number, keyword: string, enabled: boolean | 'all' | '' }
}, disable?: boolean) => useQuery<SegmentsResponse>({ ... })
export const useUpdateSegment = () => useMutation({
mutationFn: (payload: {
datasetId: string, documentId: string, segmentId: string, body: SegmentUpdater
}) => patch<{ data: SegmentDetailModel, doc_form: ChunkingMode }>(...)
})
export const useAddSegment = () => useMutation({
mutationFn: (payload: {
datasetId: string, documentId: string, body: SegmentUpdater
}) => post<{ data: SegmentDetailModel, doc_form: ChunkingMode }>(...)
})
export const useDeleteSegment = () => useMutation({
mutationFn: (payload: {
datasetId: string, documentId: string, segmentIds: string[]
}) => del<CommonResponse>(...)
})
export const useEnableSegment = () => useMutation({
mutationFn: (payload: {
datasetId: string, documentId: string, segmentIds: string[]
}) => patch<CommonResponse>(...)
})
export const useDisableSegment = () => useMutation({
mutationFn: (payload: {
datasetId: string, documentId: string, segmentIds: string[]
}) => patch<CommonResponse>(...)
})
Import
import { modifyDocMetadata } from '@/service/datasets'
import {
useSegmentList,
useUpdateSegment,
useAddSegment,
useDeleteSegment,
useEnableSegment,
useDisableSegment,
} from '@/service/knowledge/use-segment'
I/O Contract
Inputs (modifyDocMetadata)
| Parameter | Type | Required | Description |
|---|---|---|---|
datasetId |
string |
Yes | The ID of the dataset containing the document. |
documentId |
string |
Yes | The ID of the document to update. |
body.doc_type |
string |
Yes | Document type: book, web_page, paper, social_media_post, personal_document, business_document, im_chat_log, or a fixed type.
|
body.doc_metadata |
Record<string, any> |
Yes | Key-value metadata pairs (e.g., title, author, language, publisher).
|
Outputs (modifyDocMetadata)
Returns Promise<CommonResponse>:
| Field | Type | Description |
|---|---|---|
result |
string |
Operation result, typically 'success'.
|
SegmentUpdater (used by useUpdateSegment and useAddSegment)
| Field | Type | Required | Description |
|---|---|---|---|
content |
string |
Yes | The textual content of the segment. |
answer |
string |
No | Answer text (used in Q&A chunking mode). |
summary |
string |
No | Summary of the segment (used in hierarchical mode). |
keywords |
string[] |
No | Keywords associated with the segment for keyword-based retrieval. |
regenerate_child_chunks |
boolean |
No | Whether to regenerate child chunks after updating (hierarchical mode). |
attachment_ids |
string[] |
No | IDs of attached files associated with the segment. |
Usage Examples
Updating document metadata
import { modifyDocMetadata } from '@/service/datasets'
await modifyDocMetadata({
datasetId: 'ds-abc123',
documentId: 'doc-xyz789',
body: {
doc_type: 'book',
doc_metadata: {
title: 'Machine Learning Fundamentals',
author: 'Jane Smith',
language: 'English',
publisher: 'Tech Press',
publicationDate: '2025-01-15',
ISBN: '978-0-123456-78-9',
category: 'Computer Science',
},
},
})
Managing segments with React Query hooks
import { useSegmentList, useUpdateSegment, useDisableSegment } from '@/service/knowledge/use-segment'
// List segments
const { data: segments } = useSegmentList({
datasetId: 'ds-abc123',
documentId: 'doc-xyz789',
params: { page: 1, limit: 20, keyword: '', enabled: 'all' },
})
// Update a segment
const updateMutation = useUpdateSegment()
updateMutation.mutate({
datasetId: 'ds-abc123',
documentId: 'doc-xyz789',
segmentId: 'seg-001',
body: { content: 'Updated chunk content with corrected information.' },
})
// Disable low-quality segments
const disableMutation = useDisableSegment()
disableMutation.mutate({
datasetId: 'ds-abc123',
documentId: 'doc-xyz789',
segmentIds: ['seg-005', 'seg-012'],
})