Source code for automated_document_parser.loaders.pdf_load.unstructured_loader

"""
Unstructured API loader implementation.

Reference: https://docs.langchain.com/oss/python/integrations/document_loaders/unstructured_file
"""

import logging
import os

from langchain_core.documents import Document

from .base import BasePDFLoader

logger = logging.getLogger(__name__)


[docs] class UnstructuredPDFLoader(BasePDFLoader): """ PDF loader using Unstructured API backend. Advanced parsing with support for complex document layouts. Requires UNSTRUCTURED_API_KEY environment variable or api_key in kwargs. """
[docs] def load(self) -> list[Document]: """Load a PDF through the Unstructured hosted API.""" try: from langchain_unstructured import UnstructuredLoader except ImportError as exc: logger.error("Unstructured dependencies not installed: %s", exc) raise ImportError( f"Required package not installed for unstructured. " f"Install with: {self.get_install_command()}" ) from exc api_key = self.kwargs.get("api_key") or os.environ.get("UNSTRUCTURED_API_KEY") if not api_key: raise ValueError( "UNSTRUCTURED_API_KEY not found. Set it as an environment variable " "or pass it as api_key." ) file_paths = ( [str(self.file_path)] if not isinstance(self.file_path, list) else self.file_path ) try: loader = UnstructuredLoader( file_paths, api_key=api_key, partition_via_api=True, ) docs = loader.load() logger.info("Loaded %s documents with Unstructured API", len(docs)) return docs except Exception as exc: logger.error("Error loading PDF with Unstructured API: %s", exc) raise RuntimeError( f"Failed to load PDF with Unstructured API: {exc}" ) from exc
[docs] def get_install_command(self) -> str: """Get pip install command for Unstructured dependencies.""" return "pip install automated-document-parser[pdf-unstructured]"