General
PromptBeginner5 minmarkdown
<h1 align="center">
<a href="https://prompts.chat">
5
Guidelines for backend development in Langflow, focusing on Python components, FastAPI services, and backend testing.
Sign in to like and favorite skills
description: "Guidelines for backend development in Langflow, focusing on Python components, FastAPI services, and backend testing." globs:
Guidelines for backend development in Langflow, focusing on Python components, FastAPI services, and backend testing.
uv (>=0.4) for dependency managementmake for build coordinationmake backend # Start FastAPI backend on port 7860
src/backend/base/langflow/src/backend/base/langflow/components/ ├── agents/ # Agent components ├── data/ # Data processing components ├── embeddings/ # Embedding components ├── input_output/ # Input/output components ├── models/ # Language model components ├── processing/ # Text processing components ├── prompts/ # Prompt components ├── tools/ # Tool components └── vectorstores/ # Vector store components
src/backend/base/langflow/components/__init__.py with alphabetical imports:
from .my_component import MyComponent __all__ = [ "ExistingComponent", "MyComponent", # Add alphabetically ]
src/backend/tests/unit/components/ComponentTestBaseWithClient or ComponentTestBaseWithoutClientfile_names_mapping for backward compatibilitymake format_backend # Format Python code
Important: Run
make format_backend early and often (ideally before running linting or committing changes). It auto-corrects the majority of style issues, preventing lengthy manual fixes when lint errors surface later.
make lint # Run linting checks
make unit_tests # Run backend unit tests
make format_backend (FIRST - saves time on lint fixes)make lintmake unit_testssrc/backend/base/langflow/api/ ├── v1/ # API version 1 │ ├── chat.py # Chat endpoints │ ├── flows.py # Flow management │ ├── users.py # User management │ └── ... └── v2/ # API version 2 (future)
client fixture from conftest.pylogged_in_headers for authenticated endpointsasync def test_flows_endpoint(client, logged_in_headers): response = await client.post( "api/v1/flows/", json=flow_data, headers=logged_in_headers ) assert response.status_code == 201
src/backend/base/langflow/services/database/models/ ├── api_key/ # API key models ├── flow/ # Flow models ├── folder/ # Folder models ├── user/ # User models └── ...
uv run pytest src/backend/tests/unit/test_database.py
async def run(self) -> MessageType: """Main component execution method.""" # Use await for async operations result = await self.async_operation() return result async def message_response(self) -> Message: """Return a Message object for chat components.""" return Message( text=self.input_value, sender=self.sender, session_id=self.session_id, )
import asyncio async def process_in_background(self): """Process items without blocking.""" # Use asyncio.create_task for background work task = asyncio.create_task(self.heavy_operation()) # Ensure proper cleanup try: result = await task return result except asyncio.CancelledError: # Handle cancellation gracefully await self.cleanup() raise
async def queue_processing(self): """Non-blocking queue operations.""" queue = asyncio.Queue() # Non-blocking put queue.put_nowait(data) # Timeout-controlled get try: result = await asyncio.wait_for(queue.get(), timeout=5.0) return result except asyncio.TimeoutError: # Handle timeout appropriately raise ComponentError("Processing timeout")
from tests.unit.build_utils import create_flow, build_flow, get_build_events async def test_component_in_flow(client, json_flow, logged_in_headers): """Test component within a complete flow.""" flow_id = await create_flow(client, json_flow, logged_in_headers) build_response = await build_flow(client, flow_id, logged_in_headers) # Validate flow execution job_id = build_response["job_id"] events_response = await get_build_events(client, job_id, logged_in_headers) assert events_response.status_code == 200
@pytest.mark.api_key_required @pytest.mark.no_blockbuster async def test_with_real_api(self): """Test component with external service.""" api_key = os.getenv("OPENAI_API_KEY") component = MyComponent(api_key=api_key, model="gpt-4o") response = await component.run() assert response is not None
test_database.py may fail in batch runs but pass individually@pytest.mark.no_blockbuster to skip blockbuster plugin when neededasyncio.to_thread - test both patternslangflow run__init__.py updated with alphabetical importsmake format_backend (FIRST)make lintmake unit_tests