binary-husky/gpt_academic · error · RuntimeError

文件加载失败,请检查文件格式是否正确

Error message

文件加载失败,请检查文件格式是否正确

What it means

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.

Source

Thrown at crazy_functions/vector_fns/vector_database.py:203

                docs += load_file(file, SENTENCE_SIZE)
                logger.info(f"{file} 已成功加载")
                loaded_files.append(file)

        if len(docs) > 0:
            logger.info("文件加载完毕,正在生成向量库")
            if vs_path and os.path.isdir(vs_path):
                try:
                    self.vector_store = FAISS.load_local(vs_path, text2vec)
                    self.vector_store.add_documents(docs)
                except:
                    self.vector_store = FAISS.from_documents(docs, text2vec)
            else:
                self.vector_store = FAISS.from_documents(docs, text2vec)  # docs 为Document列表

            self.vector_store.save_local(vs_path)
            return vs_path, loaded_files
        else:
            raise RuntimeError("文件加载失败,请检查文件格式是否正确")

    def get_loaded_file(self, vs_path):
        ds = self.vector_store.docstore
        return set([ds._dict[k].metadata['source'].split(vs_path)[-1] for k in ds._dict])


    # query      查询内容
    # vs_path    知识库路径
    # chunk_content   是否启用上下文关联
    # score_threshold    搜索匹配score阈值
    # vector_search_top_k   搜索知识库内容条数,默认搜索5条结果
    # chunk_sizes    匹配单段内容的连接上下文长度
    def get_knowledge_based_content_test(self, query, vs_path, chunk_content,
                                        score_threshold=VECTOR_SEARCH_SCORE_THRESHOLD,
                                        vector_search_top_k=VECTOR_SEARCH_TOP_K, chunk_size=CHUNK_SIZE,
                                        text2vec=None):
        self.vector_store = FAISS.load_local(vs_path, text2vec)
        self.vector_store.chunk_content = chunk_content

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Check the preceding logger.error output — it lists each failed file and the actual exception per file.
  2. Convert documents to supported formats first (.doc → .docx, scans → OCR'd PDF, plain text).
  3. Verify the paths exist and are readable by the process (mount permissions in Docker).
  4. Call add_doc with a mix that includes at least one known-good file, or filter inputs to supported extensions before calling.

Example fix

# before
vd.add_doc(['upload.doc', 'photo.png'], vs_path)

# after (pre-filter to extensions the loader supports)
SUPPORTED = {'.pdf', '.docx', '.txt', '.md', '.html'}
files = [f for f in files if os.path.splitext(f)[1].lower() in SUPPORTED]
if not files:
    raise ValueError('no supported files to index')
vd.add_doc(files, vs_path)
Defensive patterns

Strategy: validation

Validate before calling

import os

SUPPORTED_EXTS = {'.pdf', '.docx', '.txt', '.md', '.html', '.json', '.csv'}

def filter_indexable(files: list[str]) -> list[str]:
    return [f for f in files if os.path.splitext(f)[1].lower() in SUPPORTED_EXTS and os.path.exists(f)]

files = filter_indexable(files)
if not files:
    raise ValueError('no indexable files: unsupported format or missing paths')

Try / catch

try:
    vs_path, loaded = vd.add_doc(files, vs_path)
except RuntimeError as e:
    if '文件加载失败' in str(e):
        return report_to_user('unsupported file type — convert to pdf/docx/txt/md first')
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/586ce25f22a3c88d. Report an issue: GitHub.