理念
旨在为所有代码贡献遵循这些核心原则:向后兼容性
维护稳定的公共接口并避免破坏性更改
测试优先
每个更改都必须包含全面的测试以验证正确性并防止回归
代码质量
遵循一致的风格、文档和架构模式
安全为重
优先考虑安全编码实践和漏洞预防
入门
快速修复:提交错误修复
对于简单的错误修复,您可以立即开始:克隆和设置
Make your changes
Fix the bug while following our code quality standards
Add tests
Include unit tests that fail without your fix. This allows us to verify the bug is resolved and prevents regressions
Submit a pull request
Follow the PR template provided. If applicable, reference the issue you’re fixing using a closing keyword (e.g.
Fixes #123).Full development setup
For ongoing development or larger contributions:Development environment
Set up your environment following our setup guide below
Repository structure
Understand the repository structure and package organization
Development workflow
Learn our development workflow including testing and linting
Contribution guidelines
Review our contribution guidelines for features, bugfixes, and integrations
Development environment
Set up a development environment for the package(s) you’re working on.- LangChain
- LangGraph
Core abstractions
Core abstractions
For changes to
langchain-core:Main package
Main package
For changes to
langchain:Partner packages
Partner packages
For changes to partner integrations:
Community packages
Community packages
For changes to community integrations (located in a separate repo):
Repository structure
- LangChain
- LangGraph
LangChain is organized as a monorepo with multiple packages:
Core packages
Core packages
langchain(located inlibs/langchain/): Main package with chains, agents, and retrieval logiclangchain-core(located inlibs/core/): Base interfaces and core abstractions
Partner packages
Partner packages
Located in
libs/partners/, these are independently versioned packages for specific integrations. For example:langchain-openai: OpenAI integrationslangchain-anthropic: Anthropic integrationslangchain-google-genai: Google Generative AI integrations
Supporting packages
Supporting packages
langchain-text-splitters: Text splitting utilitieslangchain-standard-tests: Standard test suites for integrationslangchain-cli: Command line interfacelangchain-community: Community maintained integrations (located in a separate repo)
Development workflow
Testing requirements
Directories are relative to the package you’re working in.
Unit tests
Location:
tests/unit_tests/Requirements:- No network calls allowed
- Test all code paths including edge cases
- Use mocks for external dependencies
Integration tests
Integration tests require access to external services/ provider APIs (which can cost money) and therefore are not run by default.Not every code change will require an integration test, but keep in mind that we’ll require/ run integration tests separately as apart of our review process.Location:
tests/integration_tests/Requirements:- Test real integrations with external services
- Use environment variables for API keys
- Skip gracefully if credentials unavailable
Code quality standards
Quality requirements:- Type hints
- Documentation
- Code style
Required: Complete type annotations for all functions
Manual formatting and linting
Code formatting and linting are enforced via CI/CD. Run these commands before committing to ensure your code passes checks.
Contribution guidelines
Backwards compatibility
Maintain compatibility:Stable interfaces
Stable interfaces
Always preserve:
- Function signatures and parameter names
- Class interfaces and method names
- Return value structure and types
- Import paths for public APIs
Safe changes
Safe changes
Acceptable modifications:
- Adding new optional parameters
- Adding new methods to classes
- Improving performance without changing behavior
- Adding new modules or functions
Before making changes
Before making changes
- Would this break existing user code?
- Check if your target is public
-
If needed, is it exported in
__init__.py? - Are there existing usage patterns in tests?
Bugfixes
For bugfix contributions:Reproduce the issue
Create a minimal test case that demonstrates the bug. Maintainers and other contributors should be able to run this test and see the failure without additional setup or modification
New features
We aim to keep the bar high for new features. We generally don’t accept new core abstractions, changes to infra, changes to dependencies, or new agents/chains from outside contributors without an existing issue that demonstrates an acute need for them. In general, feature contribution requirements include:Design discussion
Open an issue describing:
- The problem you’re solving
- Proposed API design
- Expected usage patterns
Implementation
- Follow existing code patterns
- Include comprehensive tests and documentation
- Consider security implications
Security guidelines
Security checklist:Input validation
Input validation
- Validate and sanitize all user inputs
- Properly escape data in templates and queries
- Never use
eval(),exec(), orpickleon user data, as this can lead to arbitrary code execution vulnerabilities
Error handling
Error handling
- Use specific exception types
- Don’t expose sensitive information in error messages
- Implement proper resource cleanup
Dependencies
Dependencies
- Avoid adding hard dependencies
- Keep optional dependencies minimal
- Review third-party packages for security issues
测试与验证
本地运行测试
在提交 PR 之前,请确保完成以下步骤。请注意,LangChain 与 LangGraph 的要求略有差异。- LangChain
- LangGraph
PR submission
Push your branch and open a pull request. Follow the provided form template. Note related issues using a closing keyword. After submitting, wait, and check to ensure the CI checks pass. If any checks fail, address the issues promptly - maintainers may close PRs that do not pass CI within a reasonable timeframe.
测试编写指南
为了编写有效的测试,需要遵循一些良好的实践:- Use natural language to describe the test in docstrings
- Use descriptive variable names
- Be exhaustive with assertions
- Unit tests
- Integration tests
- Mock usage
Getting help
Our goal is to have the most accessible developer setup possible. Should you experience any difficulty getting setup, please ask in the community slack or open a forum post.You’re now ready to contribute high-quality code to LangChain!