Skip to content

Commit 97fc44c

Browse files
fix: Change storage initialization to None for KnowledgeStorage (#1804)
* fix: Change storage initialization to None for KnowledgeStorage * refactor: Change storage field to optional and improve error handling when saving documents --------- Co-authored-by: João Moura <joaomdmoura@gmail.com>
1 parent 409892d commit 97fc44c

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/crewai/knowledge/knowledge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class Knowledge(BaseModel):
1414
Knowledge is a collection of sources and setup for the vector store to save and query relevant context.
1515
Args:
1616
sources: List[BaseKnowledgeSource] = Field(default_factory=list)
17-
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
17+
storage: Optional[KnowledgeStorage] = Field(default=None)
1818
embedder_config: Optional[Dict[str, Any]] = None
1919
"""
2020

2121
sources: List[BaseKnowledgeSource] = Field(default_factory=list)
2222
model_config = ConfigDict(arbitrary_types_allowed=True)
23-
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
23+
storage: Optional[KnowledgeStorage] = Field(default=None)
2424
embedder_config: Optional[Dict[str, Any]] = None
2525
collection_name: Optional[str] = None
2626

src/crewai/knowledge/source/base_file_knowledge_source.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
2222
default_factory=list, description="The path to the file"
2323
)
2424
content: Dict[Path, str] = Field(init=False, default_factory=dict)
25-
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
25+
storage: Optional[KnowledgeStorage] = Field(default=None)
2626
safe_file_paths: List[Path] = Field(default_factory=list)
2727

2828
@field_validator("file_path", "file_paths", mode="before")
@@ -62,7 +62,10 @@ def validate_content(self):
6262

6363
def _save_documents(self):
6464
"""Save the documents to the storage."""
65-
self.storage.save(self.chunks)
65+
if self.storage:
66+
self.storage.save(self.chunks)
67+
else:
68+
raise ValueError("No storage found to save documents.")
6669

6770
def convert_to_path(self, path: Union[Path, str]) -> Path:
6871
"""Convert a path to a Path object."""

src/crewai/knowledge/source/base_knowledge_source.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class BaseKnowledgeSource(BaseModel, ABC):
1616
chunk_embeddings: List[np.ndarray] = Field(default_factory=list)
1717

1818
model_config = ConfigDict(arbitrary_types_allowed=True)
19-
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
19+
storage: Optional[KnowledgeStorage] = Field(default=None)
2020
metadata: Dict[str, Any] = Field(default_factory=dict) # Currently unused
2121
collection_name: Optional[str] = Field(default=None)
2222

@@ -46,4 +46,7 @@ def _save_documents(self):
4646
Save the documents to the storage.
4747
This method should be called after the chunks and embeddings are generated.
4848
"""
49-
self.storage.save(self.chunks)
49+
if self.storage:
50+
self.storage.save(self.chunks)
51+
else:
52+
raise ValueError("No storage found to save documents.")

0 commit comments

Comments
 (0)