mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 15:48:29 +00:00
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:
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user