Fix issue #3764: Implement lazy loading for knowledge sources to prevent authentication errors #3765
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix issue #3764: Implement lazy loading for knowledge sources to prevent authentication errors
Summary
This PR fixes a bug where adding knowledge sources to agents caused authentication errors (401) during initialization, even when users only wanted to provide context without persisting to a vector database.
Root Cause: Knowledge sources were being eagerly loaded and saved to storage during agent/crew initialization (
agent.set_knowledge()andcrew.create_crew_knowledge()), which required valid embedding service credentials. The default embedder uses OpenAI's API, causing 401 errors for users without proper credentials.Solution: Implemented lazy loading pattern - knowledge sources are now loaded only when first queried, not during initialization. This allows agents to be created with knowledge sources without requiring immediate authentication.
Changes:
_sources_loadedprivate attribute toKnowledgeclass to track loading stateKnowledge.query()to check and load sources on first queryKnowledge.add_sources()to set the loaded flagadd_sources()calls fromagent.set_knowledge()andcrew.create_crew_knowledge()Review & Testing Checklist for Human
OPENAI_API_KEYto verify 401 error is prevented during agent/crew initialization (the core bug fix)Recommended Test Plan
TextFileKnowledgeSourcewithout settingOPENAI_API_KEYNotes
Link to Devin run: https://app.devin.ai/sessions/acca508411e24db9b564b7a427211553
Requested by: João ([email protected])