fix: raise ImportError during PDFKnowledgeSource instantiation when pdfplumber is missing

- Add __init__ method to PDFKnowledgeSource that checks PDFPLUMBER_AVAILABLE
- Update test_optional_dependencies.py to expect ImportError during instantiation
- Fixes test_optional_pdf_import_error in test_lite_installation.py
- Ensures consistent behavior across all optional dependency implementations

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-06-18 10:40:59 +00:00
parent 8e97e6e5f3
commit c1984e4406
2 changed files with 10 additions and 3 deletions

View File

@@ -14,6 +14,15 @@ from crewai.knowledge.source.base_file_knowledge_source import BaseFileKnowledge
class PDFKnowledgeSource(BaseFileKnowledgeSource):
"""A knowledge source that stores and queries PDF file content using embeddings."""
def __init__(self, *args, **kwargs):
"""Initialize PDFKnowledgeSource and check for pdfplumber availability."""
if not PDFPLUMBER_AVAILABLE:
raise ImportError(
"pdfplumber is required for PDF knowledge sources. "
"Please install it with: pip install 'crewai[knowledge]'"
)
super().__init__(*args, **kwargs)
def load_content(self) -> Dict[Path, str]:
"""Load and preprocess PDF file content."""
if not PDFPLUMBER_AVAILABLE:

View File

@@ -44,10 +44,8 @@ class TestOptionalDependencies:
test_file.touch()
try:
pdf_source = PDFKnowledgeSource(file_paths=["test.pdf"])
with pytest.raises(ImportError) as exc_info:
pdf_source._import_pdfplumber()
PDFKnowledgeSource(file_paths=["test.pdf"])
assert "pdfplumber is required" in str(exc_info.value)
assert "crewai[knowledge]" in str(exc_info.value)