使用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
相关产品推荐
相关产品推荐

