{"record":{"id":"ce903069a6750d3c","repo":"langgenius/dify","slug":"invalid-file-type-only-csv-files-are-allowed-ce9030","errorCode":null,"errorMessage":"Invalid file type. Only CSV files are allowed","messagePattern":"Invalid file type\\. Only CSV files are allowed","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"api/controllers/console/datasets/datasets_segments.py","lineNumber":651,"sourceCode":"        dataset_id_str = str(dataset_id)\n        dataset = DatasetService.get_dataset(dataset_id_str, session)\n        if not dataset:\n            raise NotFound(\"Dataset not found.\")\n        # check document\n        document_id_str = str(document_id)\n        document = DocumentService.get_document(dataset_id_str, document_id_str, session=session)\n        if not document:\n            raise NotFound(\"Document not found.\")\n\n        upload_file_id = req_data.upload_file_id\n\n        upload_file = session.scalar(select(UploadFile).where(UploadFile.id == upload_file_id).limit(1))\n        if not upload_file:\n            raise NotFound(\"UploadFile not found.\")\n\n        # check file type\n        if not upload_file.name or not upload_file.name.lower().endswith(\".csv\"):\n            raise ValueError(\"Invalid file type. Only CSV files are allowed\")\n\n        try:\n            # async job\n            job_id = str(uuid.uuid4())\n            indexing_cache_key = f\"segment_batch_import_{job_id}\"\n            # send batch add segments task\n            redis_client.setnx(indexing_cache_key, \"waiting\")\n            batch_create_segment_to_index_task.delay(\n                job_id,\n                upload_file_id,\n                dataset_id_str,\n                document_id_str,\n                current_tenant_id,\n                current_user.id,\n            )\n        except Exception as e:\n            return {\"error\": str(e)}, 500\n        return dump_response(SegmentBatchImportStatusResponse, {\"job_id\": job_id, \"job_status\": \"waiting\"}), 200","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/langgenius/dify/blob/ef8544b173fd6cd7a8e71df2cab576e52bebbfbc/api/controllers/console/datasets/datasets_segments.py#L633-L669","documentation":"Raised after the UploadFile row is confirmed but its stored name is empty or does not end in .csv (case-insensitive). The batch segment importer is CSV-only, so any other extension or a file with no name is rejected with ValueError. This typically surfaces as an HTTP 400/500 depending on the framework's ValueError handler rather than a clean NotFound.","triggerScenarios":"POST .../segments/batch_import with an upload_file_id whose stored name is e.g. data.xlsx, segments.json, or empty. Also triggered when an older file record has a null/None name.","commonSituations":"User drops a non-CSV file into the batch import dialog; a programmatic caller reuses a generic document upload for the import; migration/import left an UploadFile row with a blank name.","solutions":["Re-upload the data as a .csv file (with the .csv extension in the filename) and pass the new upload_file_id.","Verify the extension before submitting: assert the stored UploadFile.name ends with .csv.","If you must import non-CSV data, convert it to CSV first; this endpoint does not accept other formats."],"exampleFix":"// before\nupload:  data.xlsx   -> {\"upload_file_id\": \"<id>\"}\n// after\nconvert data.xlsx -> data.csv\nupload data.csv   -> {\"upload_file_id\": \"<id>\"}\nPOST .../segments/batch_import {\"upload_file_id\": \"<id>\"}","handlingStrategy":"validation","validationCode":"function isCsvFileName(name) {\n  return typeof name === 'string' && name.toLowerCase().endsWith('.csv');\n}\n// client-side: block the submit unless isCsvFileName(file.name)","typeGuard":"const isCsvFile = (file) => file instanceof File && /\\.csv$/i.test(file.name);","tryCatchPattern":"if (!isCsvFileName(uploadName)) {\n  // prompt user to provide a .csv file, do not call the API\n} else {\n  await batchImport(uploadFileId, ids);\n}","preventionTips":["Restrict the file picker to .csv in the UI (accept='.csv').","Convert Excel/JSON data to CSV before invoking batch import.","Validate the extension on the client to avoid a wasted round trip."],"tags":["datasets","batch-import","file-type","validation","csv"],"backgroundTag":null,"analyzedSha":"ef8544b173fd6cd7a8e71df2cab576e52bebbfbc","analyzedAt":"2026-08-12T05:15:17.394Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}