{"record":{"id":"586ce25f22a3c88d","repo":"binary-husky/gpt_academic","slug":"error-586ce2","errorCode":null,"errorMessage":"文件加载失败，请检查文件格式是否正确","messagePattern":"文件加载失败，请检查文件格式是否正确","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"crazy_functions/vector_fns/vector_database.py","lineNumber":203,"sourceCode":"                docs += load_file(file, SENTENCE_SIZE)\n                logger.info(f\"{file} 已成功加载\")\n                loaded_files.append(file)\n\n        if len(docs) > 0:\n            logger.info(\"文件加载完毕，正在生成向量库\")\n            if vs_path and os.path.isdir(vs_path):\n                try:\n                    self.vector_store = FAISS.load_local(vs_path, text2vec)\n                    self.vector_store.add_documents(docs)\n                except:\n                    self.vector_store = FAISS.from_documents(docs, text2vec)\n            else:\n                self.vector_store = FAISS.from_documents(docs, text2vec)  # docs 为Document列表\n\n            self.vector_store.save_local(vs_path)\n            return vs_path, loaded_files\n        else:\n            raise RuntimeError(\"文件加载失败，请检查文件格式是否正确\")\n\n    def get_loaded_file(self, vs_path):\n        ds = self.vector_store.docstore\n        return set([ds._dict[k].metadata['source'].split(vs_path)[-1] for k in ds._dict])\n\n\n    # query      查询内容\n    # vs_path    知识库路径\n    # chunk_content   是否启用上下文关联\n    # score_threshold    搜索匹配score阈值\n    # vector_search_top_k   搜索知识库内容条数，默认搜索5条结果\n    # chunk_sizes    匹配单段内容的连接上下文长度\n    def get_knowledge_based_content_test(self, query, vs_path, chunk_content,\n                                        score_threshold=VECTOR_SEARCH_SCORE_THRESHOLD,\n                                        vector_search_top_k=VECTOR_SEARCH_TOP_K, chunk_size=CHUNK_SIZE,\n                                        text2vec=None):\n        self.vector_store = FAISS.load_local(vs_path, text2vec)\n        self.vector_store.chunk_content = chunk_content","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/vector_fns/vector_database.py#L185-L221","documentation":"VectorDatabase.add_doc loads every input file via load_file; per-file exceptions are caught and logged, and only after the loop does the code check `if len(docs) > 0`. If zero documents were produced — every file failed to load or the loader returned nothing — it raises RuntimeError('文件加载失败，请检查文件格式是否正确'). The real per-file errors are in the log just above the raise.","triggerScenarios":"Uploading files whose type load_file cannot parse (images, .zip, proprietary formats, password-protected PDFs, corrupted docx), passing a file path that does not exist (load_file raises, is swallowed, file lands in failed_files), or a directory containing only unsupported files. Passing an empty list/filepath also yields docs == 0.","commonSituations":"Users drop scanned PDFs with no extractable text; .doc files (old Word binary) where only .docx is supported; files with extensions the loader maps to no parser; path/permission issues on mounted volumes making every load_file throw.","solutions":["Check the preceding logger.error output — it lists each failed file and the actual exception per file.","Convert documents to supported formats first (.doc → .docx, scans → OCR'd PDF, plain text).","Verify the paths exist and are readable by the process (mount permissions in Docker).","Call add_doc with a mix that includes at least one known-good file, or filter inputs to supported extensions before calling."],"exampleFix":"# before\nvd.add_doc(['upload.doc', 'photo.png'], vs_path)\n\n# after (pre-filter to extensions the loader supports)\nSUPPORTED = {'.pdf', '.docx', '.txt', '.md', '.html'}\nfiles = [f for f in files if os.path.splitext(f)[1].lower() in SUPPORTED]\nif not files:\n    raise ValueError('no supported files to index')\nvd.add_doc(files, vs_path)","handlingStrategy":"validation","validationCode":"import os\n\nSUPPORTED_EXTS = {'.pdf', '.docx', '.txt', '.md', '.html', '.json', '.csv'}\n\ndef filter_indexable(files: list[str]) -> list[str]:\n    return [f for f in files if os.path.splitext(f)[1].lower() in SUPPORTED_EXTS and os.path.exists(f)]\n\nfiles = filter_indexable(files)\nif not files:\n    raise ValueError('no indexable files: unsupported format or missing paths')","typeGuard":null,"tryCatchPattern":"try:\n    vs_path, loaded = vd.add_doc(files, vs_path)\nexcept RuntimeError as e:\n    if '文件加载失败' in str(e):\n        return report_to_user('unsupported file type — convert to pdf/docx/txt/md first')\n    raise","preventionTips":["Filter uploads to known-parsable extensions before calling add_doc.","Convert legacy formats (.doc) and OCR scans upstream.","Inspect logger.error output — it names the exact file and per-file exception that left docs empty."],"tags":["vector-database","file-parsing","faiss","validation","langchain"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}