Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+275 -261
View File
@@ -1,310 +1,324 @@
---
description: This rule provides best practices for developing LangChain applications, covering code organization, performance, security, testing, and common pitfalls. It aims to improve code quality, maintainability, and overall project success.
globs: *.py
description: Definitive guidelines for writing maintainable, performant, and robust LangChain applications using modern best practices (LCEL, LangGraph, Pydantic, `create_agent`).
globs: **/*.py
---
# LangChain Development Best Practices
# LangChain Best Practices
This document outlines the best practices for developing LangChain applications to ensure code quality, maintainability, performance, security, and overall project success. These guidelines cover various aspects of development, from code organization to testing and deployment.
This guide outlines the definitive best practices for developing with LangChain. Adhere to these rules to ensure your LLM applications are modular, scalable, and production-ready.
## 1. Code Organization and Structure
### 1.1 Directory Structure Best Practices
Always structure your LangChain projects around core components, separating concerns into distinct modules. This enhances readability, testability, and maintainability.
**✅ GOOD: Modular Structure**
Organize by component type (models, prompts, tools, agents, memory).
```python
# my_project/
# ├── agents/
# │ └── flight_booking_agent.py
# ├── models/
# │ └── llm_config.py
# ├── prompts/
# │ └── flight_prompts.py
# ├── tools/
# │ └── flight_tools.py
# ├── memory/
# │ └── chat_memory.py
# └── main.py
```
**❌ BAD: Monolithic Files**
Avoid dumping all logic into a single file.
```python
# main.py (containing everything)
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
# ... many more imports and definitions
# ... LLM, prompt, tools, agent definition all in one file
```
## 2. Leverage LangChain Expression Language (LCEL)
LCEL is the modern, recommended way to compose chains. It offers first-class streaming, async support, and clear debugging. **Never use deprecated `LLMChain` or older chain patterns.**
**✅ GOOD: LCEL for Chains**
Use the `|` operator for clear, composable pipelines.
```python
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
# Define components
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("user", "{question}")
])
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
output_parser = StrOutputParser()
# Compose chain with LCEL
chain = prompt | llm | output_parser
# Invoke
response = chain.invoke({"question": "What is the capital of France?"})
print(response)
```
**❌ BAD: Deprecated `LLMChain`**
This pattern is outdated and lacks modern features.
```python
from langchain.chains import LLMChain # DEPRECATED
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
prompt = PromptTemplate.from_template("What is the capital of {country}?")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# Avoid this
chain = LLMChain(llm=llm, prompt=prompt)
response = chain.invoke({"country": "Germany"})
print(response)
```
## 3. Agents: Use `create_agent` with LangGraph
For stateful, multi-step reasoning, use `create_agent` which is built on LangGraph. This provides durable execution, checkpointing, and human-in-the-loop support.
**✅ GOOD: `create_agent` for Robust Agents**
Leverage the proven ReAct pattern out-of-the-box.
```python
from langchain.agents import create_agent, tool
from langchain_openai import ChatOpenAI
from typing import Literal
@tool
def get_current_weather(location: str, unit: Literal["celsius", "fahrenheit"] = "fahrenheit") -> str:
"""Get the current weather in a given location and unit."""
if "tokyo" in location.lower():
return "It's 25 degrees Celsius and sunny in Tokyo."
elif "san francisco" in location.lower():
return "It's 60 degrees Fahrenheit and foggy in San Francisco."
else:
return f"Weather data for {location} not available."
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
tools = [get_current_weather]
system_prompt = "You are a helpful AI assistant that can answer questions about the weather."
agent_executor = create_agent(llm, tools, system_prompt=system_prompt)
# Run the agent
result = agent_executor.invoke({"messages": [{"role": "user", "content": "What's the weather like in Tokyo?"}]})
print(result["messages"][-1].content)
```
**❌ BAD: Manually Implementing Agent Logic**
Re-inventing the wheel leads to brittle, less robust agents.
```python
# Avoid complex, manual agent loops unless absolutely necessary with LangGraph directly.
# This often lacks built-in persistence, error handling, and standard patterns.
# ... (complex custom loop with if/else for tool calling and response generation)
```
Adopt a clear and consistent directory structure to improve code discoverability and maintainability. A recommended structure is:
## 4. Enforce Structured Output with Pydantic
Always define structured output schemas for LLM responses using Pydantic. This drastically reduces hallucination risk and simplifies downstream processing.
project_root/
├── data/ # Raw data, processed data, and datasets
├── src/ # Source code directory
│ ├── components/ # Reusable LangChain components (e.g., custom chains, tools)
│ ├── chains/ # Definitions of LangChain chains
│ ├── agents/ # Agent implementations
│ ├── memory/ # Memory implementations
│ ├── utils/ # Utility functions and modules
│ ├── models/ # Custom model definitions or wrappers
│ ├── callbacks/ # Custom callback handlers
│ ├── vectorstores/ # Vectorstore configurations and connections
│ ├── document_loaders/ # Custom document loaders
│ ├── prompts/ # Prompt templates and management
│ ├── config/ # Configuration files
│ └── main.py # Entry point of the application
├── tests/ # Unit and integration tests
├── notebooks/ # Jupyter notebooks for experimentation and documentation
├── docs/ # Project documentation
├── requirements.txt # Project dependencies
├── pyproject.toml # Project metadata and build configuration
└── README.md # Project README file
**✅ GOOD: Pydantic for Reliable Output**
```python
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel, Field
class Joke(BaseModel):
"""Joke to tell user."""
setup: str = Field(description="the setup of the joke")
punchline: str = Field(description="the punch line of the joke")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
structured_llm = llm.with_structured_output(Joke)
prompt = ChatPromptTemplate.from_messages([
("human", "Tell me a joke about {topic}")
])
chain = prompt | structured_llm
joke_obj = chain.invoke({"topic": "bears"})
print(f"Setup: {joke_obj.setup}\nPunchline: {joke_obj.punchline}")
```
**❌ BAD: Relying on Free-Form Text Output**
Parsing unstructured text is error-prone and fragile.
```python
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
### 1.2 File Naming Conventions
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("human", "Tell me a joke about {topic}. Format it as 'Setup: ... Punchline: ...'")
])
chain = prompt | llm | StrOutputParser()
raw_joke = chain.invoke({"topic": "dogs"})
# This will require manual, brittle parsing logic
print(raw_joke)
```
Use descriptive and consistent file names:
## 5. Asynchronous Operations and Streaming
- `module_name.py`: For general modules.
- `component_name.py`: For LangChain components (e.g., `custom_chain.py`).
- `test_module_name.py`: For test files.
- Use lowercase and underscores for file names (snake_case).
For responsive user experiences and efficient resource utilization, always use LangChain's asynchronous APIs and streaming capabilities.
**✅ GOOD: Async and Streaming**
### 1.3 Module Organization
```python
import asyncio
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
Organize code into logical modules based on functionality. Each module should have a clear purpose and minimal dependencies.
async def stream_response():
prompt = ChatPromptTemplate.from_template("Write a long poem about {topic}.")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
chain = prompt | llm | StrOutputParser()
print("Streaming response:")
async for chunk in chain.stream({"topic": "the ocean"}):
print(chunk, end="", flush=True)
print("\n--- End Stream ---")
- **Cohesion**: Modules should have high cohesion, meaning their elements are closely related.
- **Coupling**: Modules should have low coupling, meaning they are independent of each other as much as possible.
if __name__ == "__main__":
asyncio.run(stream_response())
```
### 1.4 Component Architecture
**❌ BAD: Blocking Calls for Long Operations**
Synchronous calls block the event loop, leading to poor UX.
Design LangChain applications using a component-based architecture. Components should be reusable, testable, and well-defined.
```python
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
- **Chains**: Define chains as reusable components that encapsulate specific workflows.
- **Agents**: Implement agents as modular entities that interact with the environment using tools.
- **Memory**: Manage conversation history and state using memory components.
- **Tools**: Create tools as independent units that perform specific actions.
- **Callbacks**: Utilize callbacks for logging, monitoring, and custom event handling.
def get_blocking_response():
prompt = ChatPromptTemplate.from_template("Write a long poem about {topic}.")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
chain = prompt | llm | StrOutputParser()
### 1.5 Code Splitting Strategies
print("Getting blocking response...")
response = chain.invoke({"topic": "the ocean"}) # Blocks until complete
print(response)
Split large files into smaller, manageable chunks to improve readability and maintainability.
if __name__ == "__main__":
get_blocking_response()
```
- **Function-level splitting**: Break down large functions into smaller, single-purpose functions.
- **Class-level splitting**: Divide large classes into smaller, more focused classes.
- **Module-level splitting**: Separate modules based on functionality to reduce complexity.
## 6. Type Hints
## 2. Common Patterns and Anti-patterns
Strictly use type hints for all functions, variables, and LangChain components. This improves code clarity, enables static analysis, and reduces runtime errors.
### 2.1 Design Patterns Specific to LangChain
**✅ GOOD: Comprehensive Type Hinting**
- **Chain of Responsibility**: Implement chains of operations where each component handles a specific task, passing the result to the next component.
- **Strategy Pattern**: Use strategy patterns to encapsulate different algorithms or behaviors within interchangeable strategy objects.
- **Template Method**: Define the skeleton of an algorithm in a base class, allowing subclasses to override specific steps without changing the algorithm's structure.
- **Factory Pattern**: Use factory patterns to create instances of LangChain components dynamically, based on configuration or runtime conditions.
```python
from typing import List, Dict, Any
from langchain_core.runnables import Runnable
from langchain_core.messages import BaseMessage
### 2.2 Recommended Approaches for Common Tasks
def process_chat_history(
messages: List[BaseMessage],
agent_chain: Runnable[Dict[str, Any], Dict[str, Any]]
) -> List[BaseMessage]:
"""Processes a list of chat messages using an agent chain."""
# ... logic
return messages
- **Prompt Engineering**: Use prompt templates to manage and reuse prompts. Optimize prompts for clarity, context, and desired output.
- **Data Loading**: Implement custom data loaders to handle various data sources and formats. Use text splitters to chunk large documents into smaller pieces for retrieval.
- **Vector Storage**: Use vector stores to store and retrieve embeddings efficiently. Choose the appropriate vector store based on performance, scalability, and cost.
- **Agent Design**: Design agents with clear objectives, tools, and decision-making logic. Use observation logs to track agent actions and outcomes.
- **Memory Management**: Implement memory components to maintain conversation history and context. Use sliding window or summarization techniques to manage long conversations.
# Example usage with proper types
# agent_chain = ... (a Runnable object)
# history: List[BaseMessage] = []
# updated_history = process_chat_history(history, agent_chain)
```
### 2.3 Anti-patterns and Code Smells to Avoid
**❌ BAD: Untyped Code**
Makes code harder to understand and refactor.
- **God Classes**: Avoid creating large classes that handle too many responsibilities.
- **Long Methods**: Avoid creating long methods that are difficult to understand and maintain.
- **Duplicated Code**: Avoid duplicating code across multiple modules. Extract common code into reusable functions or components.
- **Magic Numbers**: Avoid using magic numbers or hardcoded values. Define constants or configuration variables instead.
- **Tight Coupling**: Avoid creating tight coupling between modules. Use interfaces and dependency injection to promote loose coupling.
```python
def process_chat_history(messages, agent_chain): # No type hints
# ... logic
return messages
```
### 2.4 State Management Best Practices
## 7. Secure API Key Management
- **Stateless Components**: Design components to be stateless whenever possible. This improves testability and scalability.
- **Centralized State**: Manage application state in a centralized location (e.g., a state management class or library).
- **Immutable State**: Use immutable data structures to prevent unintended side effects and improve predictability.
- **Explicit State Transitions**: Define explicit state transitions to make state changes clear and traceable.
Never hardcode API keys. Always use environment variables, preferably loaded via `python-dotenv`.
### 2.5 Error Handling Patterns
**✅ GOOD: Environment Variables with `python-dotenv`**
- **Try-Except Blocks**: Use try-except blocks to handle exceptions and prevent application crashes.
- **Logging**: Log errors and exceptions to facilitate debugging and monitoring.
- **Custom Exceptions**: Define custom exceptions to represent specific error conditions.
- **Retry Logic**: Implement retry logic for transient errors (e.g., network timeouts).
- **Fallback Strategies**: Implement fallback strategies for critical operations to ensure application resilience.
```python
# .env file (NOT committed to Git)
# OPENAI_API_KEY="sk-..."
# ANTHROPIC_API_KEY="sk-..."
## 3. Performance Considerations
# my_app.py
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
### 3.1 Optimization Techniques
load_dotenv() # Load environment variables from .env
- **Caching**: Implement caching mechanisms to store frequently accessed data and results.
- **Batch Processing**: Process data in batches to reduce overhead and improve throughput.
- **Asynchronous Operations**: Use asynchronous operations to perform non-blocking I/O and improve responsiveness.
- **Connection Pooling**: Use connection pooling to reuse database connections and reduce latency.
- **Data Compression**: Compress data to reduce storage space and network bandwidth.
- **Vectorstore Optimization**: Use efficient vectorstore implementations (e.g., FAISS, Annoy) and optimize indexing parameters for fast retrieval.
llm = ChatOpenAI(
openai_api_key=os.getenv("OPENAI_API_KEY"),
model="gpt-4o-mini"
)
```
### 3.2 Memory Management
**❌ BAD: Hardcoded API Keys**
A major security vulnerability.
- **Object Pooling**: Use object pooling to reuse objects and reduce memory allocation overhead.
- **Garbage Collection**: Monitor garbage collection performance and tune parameters to minimize pauses.
- **Memory Profiling**: Use memory profiling tools to identify memory leaks and optimize memory usage.
- **Lazy Loading**: Load data on demand to reduce initial memory footprint.
- **Chunking Large Documents**: Process large documents in smaller chunks to avoid memory overflow.
```python
# my_app.py
from langchain_openai import ChatOpenAI
### 3.3 Rendering Optimization (if applicable for UI components)
# NEVER DO THIS
llm = ChatOpenAI(openai_api_key="sk-YOUR_HARDCODED_KEY_HERE", model="gpt-4o-mini")
```
- **Virtualization**: Use virtualization techniques to render large lists efficiently.
- **Debouncing and Throttling**: Use debouncing and throttling to reduce the frequency of UI updates.
- **Memoization**: Use memoization to cache expensive rendering calculations.
## 8. Testing Approaches
### 3.4 Bundle Size Optimization (if applicable for web apps)
Implement a robust testing strategy including unit tests for components and integration tests for chains/agents. Leverage LangSmith for tracing and evaluation.
- **Code Splitting**: Split code into smaller chunks to reduce initial load time.
- **Tree Shaking**: Use tree shaking to remove unused code from bundles.
- **Minification and Compression**: Minify and compress code to reduce bundle size.
- **Lazy Loading**: Load components and modules on demand.
**✅ GOOD: Unit & Integration Tests + LangSmith**
### 3.5 Lazy Loading Strategies
```python
# tests/test_tools.py
import pytest
from tools.flight_tools import get_flight_status # Assuming a tool module
- **On-Demand Loading**: Load data or components only when they are needed.
- **Intersection Observer**: Use the Intersection Observer API to load components when they become visible in the viewport.
- **Dynamic Imports**: Use dynamic imports to load modules asynchronously.
def test_get_flight_status_valid():
status = get_flight_status("AA123")
assert "on time" in status.lower()
## 4. Security Best Practices
# tests/test_agent.py
from langchain_core.messages import HumanMessage
from agents.flight_booking_agent import agent_executor # Assuming an agent module
### 4.1 Common Vulnerabilities and How to Prevent Them
@pytest.mark.asyncio
async def test_flight_booking_agent_query():
# Set LANGCHAIN_TRACING_V2="true" and LANGCHAIN_API_KEY for LangSmith
response = await agent_executor.ainvoke({"messages": [HumanMessage(content="What is the status of flight AA123?")]})
assert "on time" in response["messages"][-1].content.lower()
- **Prompt Injection**: Prevent prompt injection by validating and sanitizing user inputs. Use prompt templates and parameterized queries to avoid direct injection of malicious code.
- **Data Exfiltration**: Prevent data exfiltration by restricting access to sensitive data and implementing data masking techniques.
- **Code Execution**: Prevent arbitrary code execution by avoiding the use of `eval()` or similar functions. Use safe alternatives for dynamic code generation.
- **Denial of Service (DoS)**: Prevent DoS attacks by implementing rate limiting, input validation, and resource quotas.
# For prompt engineering, use golden tests (input/expected output pairs)
```
### 4.2 Input Validation
**❌ BAD: No Tests or Manual Testing Only**
Leads to regressions and unreliable LLM applications.
- **Whitelisting**: Validate inputs against a whitelist of allowed values or patterns.
- **Sanitization**: Sanitize inputs to remove or escape potentially harmful characters or code.
- **Type Checking**: Enforce type checking to ensure that inputs conform to expected data types.
- **Length Limits**: Enforce length limits to prevent buffer overflows or excessive memory usage.
### 4.3 Authentication and Authorization Patterns
- **Authentication**: Use strong authentication mechanisms (e.g., multi-factor authentication) to verify user identities.
- **Authorization**: Implement role-based access control (RBAC) to restrict access to resources based on user roles.
- **Least Privilege**: Grant users the minimum necessary privileges to perform their tasks.
- **Secure Storage**: Store sensitive credentials (e.g., API keys) securely using encryption or secret management tools.
### 4.4 Data Protection Strategies
- **Encryption**: Encrypt sensitive data at rest and in transit.
- **Data Masking**: Mask sensitive data to protect it from unauthorized access.
- **Data Anonymization**: Anonymize data to remove personally identifiable information (PII).
- **Access Logging**: Log all data access events to track and monitor usage.
- **Data Retention**: Define and enforce data retention policies to minimize the risk of data breaches.
### 4.5 Secure API Communication
- **HTTPS**: Use HTTPS to encrypt communication between clients and servers.
- **API Keys**: Protect API keys and other sensitive credentials.
- **Rate Limiting**: Implement rate limiting to prevent abuse and DoS attacks.
- **Input Validation**: Validate all API inputs to prevent injection attacks.
- **Output Encoding**: Encode API outputs to prevent cross-site scripting (XSS) attacks.
## 5. Testing Approaches
### 5.1 Unit Testing Strategies
- **Test-Driven Development (TDD)**: Write unit tests before writing the code to be tested.
- **Mocking**: Use mocking to isolate components and test them independently.
- **Assertion**: Use assertions to verify that the code behaves as expected.
- **Coverage**: Aim for high code coverage to ensure that all code paths are tested.
- **Parameterized Tests**: Use parameterized tests to test multiple scenarios with different inputs.
### 5.2 Integration Testing
- **Component Integration**: Test the integration between components to ensure that they work together correctly.
- **API Integration**: Test the integration with external APIs to ensure that data is exchanged correctly.
- **Database Integration**: Test the integration with databases to ensure that data is stored and retrieved correctly.
- **End-to-End Flows**: Test end-to-end flows to ensure that the application works as a whole.
### 5.3 End-to-End Testing
- **UI Testing**: Test the user interface to ensure that it is functional and user-friendly.
- **Functional Testing**: Test the functional requirements of the application to ensure that it meets the specifications.
- **Performance Testing**: Test the performance of the application to ensure that it is responsive and scalable.
- **Security Testing**: Test the security of the application to identify and mitigate vulnerabilities.
- **Accessibility Testing**: Test the accessibility of the application to ensure that it is usable by people with disabilities.
### 5.4 Test Organization
- **Test Suites**: Organize tests into test suites based on functionality or component.
- **Test Naming**: Use descriptive test names to make it clear what each test is testing.
- **Test Data**: Use realistic test data to simulate real-world scenarios.
- **Test Environment**: Set up a dedicated test environment to isolate tests from production data.
### 5.5 Mocking and Stubbing
- **Mocking**: Use mocking to replace external dependencies with controlled substitutes.
- **Stubbing**: Use stubbing to provide predefined responses to external dependencies.
- **Dependency Injection**: Use dependency injection to make it easier to mock and stub dependencies.
- **Mocking Frameworks**: Use mocking frameworks (e.g., `unittest.mock`) to simplify the mocking process.
## 6. Common Pitfalls and Gotchas
### 6.1 Frequent Mistakes Developers Make
- **Hardcoding API Keys**: Storing API keys directly in the code instead of using environment variables.
- **Ignoring Rate Limits**: Failing to handle API rate limits, leading to errors and service disruptions.
- **Lack of Input Validation**: Not validating user inputs, making the application vulnerable to prompt injection attacks.
- **Insufficient Error Handling**: Not handling errors properly, leading to application crashes and data loss.
- **Over-Reliance on Default Settings**: Using default settings without considering their impact on performance and security.
### 6.2 Edge Cases to Be Aware Of
- **Empty Inputs**: Handling empty inputs gracefully to prevent errors.
- **Long Inputs**: Handling long inputs efficiently to avoid performance issues.
- **Special Characters**: Handling special characters correctly to prevent injection attacks.
- **Unicode Support**: Ensuring proper Unicode support to handle different languages and character sets.
- **Network Errors**: Handling network errors gracefully to ensure application resilience.
### 6.3 Version-Specific Issues
- **API Changes**: Being aware of API changes in different LangChain versions and updating code accordingly.
- **Compatibility**: Ensuring compatibility between different LangChain components and versions.
- **Deprecated Features**: Avoiding the use of deprecated features and migrating to their replacements.
### 6.4 Compatibility Concerns
- **Python Versions**: Ensuring compatibility with different Python versions.
- **Operating Systems**: Ensuring compatibility with different operating systems (e.g., Windows, macOS, Linux).
- **Dependency Conflicts**: Resolving dependency conflicts between different libraries.
### 6.5 Debugging Strategies
- **Logging**: Use logging to track the execution flow and identify errors.
- **Debugging Tools**: Use debugging tools (e.g., `pdb`) to step through code and inspect variables.
- **Print Statements**: Use print statements strategically to output debugging information.
- **Error Messages**: Pay attention to error messages and stack traces to understand the root cause of errors.
- **Remote Debugging**: Use remote debugging to debug applications running on remote servers.
## 7. Tooling and Environment
### 7.1 Recommended Development Tools
- **IDE**: Use a powerful IDE (e.g., VS Code, PyCharm) with support for Python and LangChain.
- **Linters**: Use linters (e.g., `flake8`, `pylint`) to enforce code style and identify potential errors.
- **Formatters**: Use formatters (e.g., `black`, `autopep8`) to automatically format code according to PEP 8 standards.
- **Debuggers**: Use debuggers (e.g., `pdb`, `ipdb`) to step through code and inspect variables.
- **Version Control**: Use Git for version control and collaboration.
### 7.2 Build Configuration
- **`pyproject.toml`**: Use `pyproject.toml` file to manage project metadata, dependencies, and build configuration.
- **`requirements.txt`**: Generate and update the `requirements.txt` file to specify project dependencies.
- **Virtual Environments**: Use virtual environments (`venv`, `conda`) to isolate project dependencies.
### 7.3 Linting and Formatting
- **Linting**: Configure linters to enforce code style and identify potential errors automatically.
- **Formatting**: Configure formatters to automatically format code according to PEP 8 standards.
- **Pre-commit Hooks**: Use pre-commit hooks to run linters and formatters before committing code.
### 7.4 Deployment Best Practices
- **Containerization**: Use containerization (e.g., Docker) to package the application and its dependencies.
- **Orchestration**: Use orchestration tools (e.g., Kubernetes) to manage and scale the application.
- **Infrastructure as Code (IaC)**: Use IaC tools (e.g., Terraform, CloudFormation) to provision and manage infrastructure.
- **Monitoring**: Implement monitoring and logging to track application performance and identify issues.
- **Continuous Deployment**: Implement continuous deployment to automate the deployment process.
### 7.5 CI/CD Integration
- **Continuous Integration (CI)**: Use CI tools (e.g., GitHub Actions, GitLab CI, Jenkins) to automatically build, test, and analyze code.
- **Continuous Delivery (CD)**: Use CD tools to automatically deploy code to staging or production environments.
- **Automated Testing**: Integrate automated testing into the CI/CD pipeline to ensure code quality.
- **Rollback Strategies**: Implement rollback strategies to quickly revert to previous versions in case of deployment failures.
By following these best practices, developers can build robust, scalable, and maintainable LangChain applications that meet the needs of their users and stakeholders.
This comprehensive guide is designed to help developers create high-quality LangChain applications by adhering to industry-standard coding practices and principles.
@file best_practices_python.mdc
@file best_practices_langchain_specific.mdc
```python
# No test files, relying on manual `python main.py` to check functionality.
```