This section documents the agentic subsystem of Langflow, which provides AI-assisted component generation and flow building capabilities. It also covers the Knowledge Base API, a specialized infrastructure for managing document storage, chunking, and retrieval using Chroma-backed vector stores.
Langflow includes an "Agentic Experience" that allows the system to act as an assistant for users. This system is responsible for generating Python code for CustomComponent instances and providing troubleshooting feedback.
The assistant uses an agentic flow to generate Python code. This process involves iterative validation and retry logic to ensure the generated code is functional within the Langflow environment.
Component class code as a string.validate_code src/lfx/src/lfx/custom/validate.py32-34 This function parses the code into an AST to check for syntax errors src/lfx/src/lfx/custom/validate.py38-45 and attempts to resolve imports src/lfx/src/lfx/custom/validate.py52-58Data, Message, and Component src/lfx/src/lfx/custom/validate.py76-110validate_code returns errors in the function or imports keys src/lfx/src/lfx/custom/validate.py34 the assistant uses these error messages to refine the code. The frontend tracks these attempts, providing feedback on the generation progress.The AssistantRunner class orchestrates the execution of assistant-related tasks src/backend/base/langflow/agentic/utils/assistant_runner.py12-14 It interfaces with the FlowComponent to manage how generated components are integrated into a user's workspace src/backend/base/langflow/agentic/utils/flow_component.py10-15
The following diagram illustrates how natural language prompts are transformed into functional code entities and validated within the backend.
Sources: src/lfx/src/lfx/custom/validate.py32-133 src/backend/base/langflow/agentic/utils/assistant_runner.py12-25 src/backend/base/langflow/agentic/utils/flow_component.py10-40
The Knowledge Base (KB) API provides a structured way to manage document storage, indexing, and retrieval. It is primarily backed by ChromaDB for vector storage.
The KB system supports several key operations accessible via the /knowledge_bases router src/backend/base/langflow/api/v1/knowledge_bases.py78:
create_knowledge_base initializes a new KB directory and a Chroma collection src/backend/base/langflow/api/v1/knowledge_bases.py246-302 It verifies permissions via _guard_kb_action src/backend/base/langflow/api/v1/knowledge_bases.py95-165KBIngestionHelper processes data into vector stores src/backend/base/langflow/api/utils/kb_helpers.py440-441KnowledgeBaseComponent allows searching and retrieving data from a selected collection src/backend/base/langflow/api/v1/knowledge_bases.py33-48preview_chunks allows users to test RecursiveCharacterTextSplitter settings before final ingestion src/backend/base/langflow/api/v1/knowledge_bases.py1015-1049KBStorageHelper.delete_storage handles the teardown of ChromaDB SQLite file locks using exponential backoff and a .kb_deleted sentinel file src/backend/base/langflow/api/utils/kb_helpers.py166-224To prevent path traversal attacks, the KB API enforces strict containment checks.
validate_kb_path ensures the requested path is within the allowed knowledge bases directory src/backend/base/langflow/api/utils/kb_helpers.py20-30_guard_kb_action resolves the effective KB owner and ensures the actor has appropriate KnowledgeBaseAction permissions src/backend/base/langflow/api/v1/knowledge_bases.py95-165kb_allowed_folder_roots defined in system settings src/backend/base/langflow/api/v1/knowledge_bases.py18-24This diagram maps the API requests to the internal helper classes and storage mechanisms.
Sources: src/backend/base/langflow/api/v1/knowledge_bases.py78-302 src/backend/base/langflow/api/utils/kb_helpers.py103-224 src/backend/base/langflow/api/v1/knowledge_bases.py1015-1049
The ingestion process transforms raw files or dataframes into vectorized chunks.
chunk_text_for_ingestion uses RecursiveCharacterTextSplitter to divide text based on chunk_size and chunk_overlap src/backend/base/langflow/api/utils/kb_helpers.py74-100KBAnalysisHelper.get_metadata reads and updates embedding_metadata.json, tracking fields like embedding_provider, embedding_model, and avg_chunk_size src/backend/base/langflow/api/utils/kb_helpers.py270-320KBAnalysisHelper.update_text_metrics calculates the total word count, character count, and average chunk size across the entire collection src/backend/base/langflow/api/utils/kb_helpers.py340-365The system uses several constants to manage performance and reliability:
INGESTION_BATCH_SIZE (default 100) controls document batching during ingestion src/backend/base/langflow/api/utils/kb_helpers.py52MAX_RETRY_ATTEMPTS (default 3) and MAX_DELETE_RETRIES (default 5) manage transient failures and file lock issues src/backend/base/langflow/api/utils/kb_helpers.py53-55| Class / Function | Role | Source |
|---|---|---|
KBStorageHelper | Manages root paths, directory sizes, and Chroma clients | src/backend/base/langflow/api/utils/kb_helpers.py103-104 |
KBAnalysisHelper | Detects embedding models and calculates metrics | src/backend/base/langflow/api/utils/kb_helpers.py267-268 |
KBIngestionHelper | Orchestrates the ingestion of items into backends | src/backend/base/langflow/api/utils/kb_helpers.py440-441 |
_guard_kb_action | Validates permissions and resolves KB ownership | src/backend/base/langflow/api/v1/knowledge_bases.py95-100 |
Sources: src/backend/base/langflow/api/v1/knowledge_bases.py1-172 src/backend/base/langflow/api/utils/kb_helpers.py1-441
Langflow enforces security measures for ChromaDB collections to prevent the execution of unsafe server-side embedding functions.
The chroma_security.py module provides utility functions to ensure collections are created with disabled server-side embedding functions src/lfx/src/lfx/base/vectorstores/chroma_security.py1-15
chroma_langchain_collection_kwargs(): Returns a dictionary setting embedding_function to None within the collection_configuration src/lfx/src/lfx/base/vectorstores/chroma_security.py22-24chroma_client_create_collection_kwargs(): Similarly disables the embedding function for direct Chroma client calls src/lfx/src/lfx/base/vectorstores/chroma_security.py27-30To avoid readonly errors on concurrent access, KBStorageHelper.get_fresh_chroma_client clears the SharedSystemClient registry for the specific path before creating a new PersistentClient src/backend/base/langflow/api/utils/kb_helpers.py136-152
This diagram maps the component inputs to the underlying langchain_chroma.Chroma initialization.
Sources: src/lfx/src/lfx/base/vectorstores/chroma_security.py1-30 src/backend/base/langflow/api/utils/kb_helpers.py136-165
Langflow includes pre-built starter projects that demonstrate the integration of Agentic Assistants and Knowledge Bases.
The Vector Store RAG starter project illustrates a standard ingestion and retrieval pipeline src/backend/tests/unit/initial_setup/starter_projects/test_vector_store_rag.py73
FileComponent connected to a SplitTextComponent src/backend/tests/unit/initial_setup/starter_projects/test_vector_store_rag.py31-37ChatInput to a Prompt Template, which uses context from a ParserComponent (processing split text) to feed a LanguageModelComponent src/backend/tests/unit/initial_setup/starter_projects/test_vector_store_rag.py41-70ParserComponent stringifies ingested data for inclusion in prompts src/backend/tests/unit/initial_setup/starter_projects/test_vector_store_rag.py50-51{context} and {question} src/backend/tests/unit/initial_setup/starter_projects/test_vector_store_rag.py54-61Sources: src/backend/tests/unit/initial_setup/starter_projects/test_vector_store_rag.py1-173 src/backend/tests/unit/initial_setup/starter_projects/test_memory_chatbot.py1-50
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.