{"record":{"id":"593412b8820569bd","repo":"langgenius/dify","slug":"invalid-doc-type","errorCode":null,"errorMessage":"Invalid doc_type.","messagePattern":"Invalid doc_type\\.","errorType":"validation","errorClass":"ValueError","httpStatus":500,"severity":"error","filePath":"api/controllers/console/datasets/datasets_document.py","lineNumber":1312,"sourceCode":"        dataset_id: UUID,\n        document_id: UUID,\n    ):\n        dataset_id_str = str(dataset_id)\n        document_id_str = str(document_id)\n        document = self.get_document(session, dataset_id_str, document_id_str, current_user, current_tenant_id)\n\n        doc_type = req_data.doc_type\n        doc_metadata = req_data.doc_metadata\n\n        # The role of the current user in the ta table must be admin, owner, dataset_operator, or editor\n        if not current_user.is_dataset_editor:\n            raise Forbidden()\n\n        if doc_type is None or doc_metadata is None:\n            raise ValueError(\"Both doc_type and doc_metadata must be provided.\")\n\n        if doc_type not in DocumentService.DOCUMENT_METADATA_SCHEMA:\n            raise ValueError(\"Invalid doc_type.\")\n\n        if not isinstance(doc_metadata, dict):\n            raise ValueError(\"doc_metadata must be a dictionary.\")\n        metadata_schema: dict[str, Any] = cast(dict[str, Any], DocumentService.DOCUMENT_METADATA_SCHEMA[doc_type])\n\n        document.doc_metadata = {}\n        if doc_type == \"others\":\n            document.doc_metadata = doc_metadata\n        else:\n            for key, value_type in metadata_schema.items():\n                value = doc_metadata.get(key)\n                if value is not None and isinstance(value, value_type):\n                    document.doc_metadata[key] = value\n\n        document.doc_type = doc_type\n        document.updated_at = naive_utc_now()\n\n        return SimpleResultMessageResponse(result=\"success\", message=\"Document metadata updated.\").model_dump(","sourceCodeStart":1294,"sourceCodeEnd":1330,"githubUrl":"https://github.com/langgenius/dify/blob/ef8544b173fd6cd7a8e71df2cab576e52bebbfbc/api/controllers/console/datasets/datasets_document.py#L1294-L1330","documentation":"Raised in the metadata PUT handler when doc_type is not a key in DocumentService.DOCUMENT_METADATA_SCHEMA (datasets_document.py:1311). Allowed keys are the metadata document types (book, web_page, paper, social_media_post, ... and others). Same controller defect as 529: ValueError surfaces as HTTP 500 instead of a 400.","triggerScenarios":"PUT .../metadata with doc_type set to an unknown value (e.g. \"unknown\", \"article\") or a type from a newer/older backend version whose schema differs.","commonSituations":"Client hardcoded a doc_type not in this server's schema; version skew between frontend and backend; typo in the doc_type string.","solutions":["Use a doc_type present in DocumentService.DOCUMENT_METADATA_SCHEMA (book, web_page, paper, social_media_post, or others).","Fetch the allowed set from the backend if exposed, or pin client and server versions together.","Server-side fix: raise InvalidMetadataError to return 400 instead of ValueError -> 500."],"exampleFix":"# before\nif doc_type not in DocumentService.DOCUMENT_METADATA_SCHEMA:\n    raise ValueError(\"Invalid doc_type.\")\n# after\nif doc_type not in DocumentService.DOCUMENT_METADATA_SCHEMA:\n    raise InvalidMetadataError(f\"Invalid doc_type: {doc_type}\")","handlingStrategy":"validation","validationCode":"ALLOWED_DOC_TYPES = {\"book\", \"web_page\", \"paper\", \"social_media_post\", \"others\"}  # keep in sync with DOCUMENT_METADATA_SCHEMA\nif doc_type not in ALLOWED_DOC_TYPES:\n    raise ValueError(f\"Invalid doc_type {doc_type!r}; must be one of {ALLOWED_DOC_TYPES}\")","typeGuard":"def is_known_doc_type(doc_type: str, schema_keys) -> bool:\n    return doc_type in schema_keys","tryCatchPattern":"try:\n    console.update_document_metadata(dataset_id, document_id, payload)\nexcept HTTPError as e:\n    if e.response.status_code >= 500 and \"Invalid doc_type\" in e.response.json().get(\"message\", \"\"):\n        # doc_type not in schema; correct and retry\n        payload[\"doc_type\"] = \"others\"\n        console.update_document_metadata(dataset_id, document_id, payload)\n    else:\n        raise","preventionTips":["Source the allowed doc_type set from the backend schema to avoid version skew.","Fall back to doc_type=\"others\" when the type is unknown."],"tags":["metadata","validation","bug","knowledge"],"backgroundTag":null,"analyzedSha":"ef8544b173fd6cd7a8e71df2cab576e52bebbfbc","analyzedAt":"2026-08-12T05:15:17.394Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}