You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LangChain遇FileType.UNK不支持错误,求解决方案

LangChain加载PDF时触发FileType.UNK不支持错误

问题详情

确认目标文件夹仅包含PDF文档,但使用UnstructuredFileLoader递归加载时出现以下错误:

ValueError: Invalid file .... The FileType.UNK file type is not supported in partition.

运行代码如下:

from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Milvus
from langchain.chains import ConversationalRetrievalChain
from langchain.document_loaders import UnstructuredFileLoader

# Recursive loader to get files  
loader = UnstructuredFileLoader(mydocs, recursive=True)
docs = loader.load()

解决方案

  • 过滤非PDF文件:递归加载时会扫描到系统隐藏文件(如Mac的.DS_Store、临时缓存文件),这些文件被识别为未知类型。通过glob参数限定只加载PDF:

    loader = UnstructuredFileLoader(mydocs, recursive=True, glob="*.pdf")
    docs = loader.load()
    
  • 排查异常PDF文件:部分PDF可能损坏、加密或后缀名错误,导致无法识别。可逐个验证文件完整性,或添加异常捕获跳过问题文件:

    from langchain.document_loaders import UnstructuredFileLoader
    from pathlib import Path
    
    docs = []
    for pdf_path in Path(mydocs).rglob("*.pdf"):
        try:
            loader = UnstructuredFileLoader(str(pdf_path))
            docs.extend(loader.load())
        except Exception as e:
            print(f"跳过异常文件 {pdf_path}: {str(e)}")
    
  • 强制指定PDF解析类型:若部分PDF被误识别,可强制指定content_type让unstructured按PDF格式解析:

    loader = UnstructuredFileLoader(
        mydocs, 
        recursive=True, 
        glob="*.pdf",
        unstructured_kwargs={"content_type": "application/pdf"}
    )
    docs = loader.load()
    

内容的提问来源于stack exchange,提问作者biogeek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 18:52:15