Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page: - [CLAUDE.md](https://github.com/langchain-ai/langchain/blob/HEAD/CLAUDE.md) - [libs/partners/xai/tests/integration_tests/test_chat_models_standard.py](https://github.com/langchain-ai/langchain/blob/HEAD/libs/partners/xai/tests/integration_tests/test_chat_models_standard.py) - [libs/langchain/langchain/evaluation/qa/eval_prompt.py](https://github.com/langchain-ai/langchain/blob/HEAD/libs/langchain/langchain/evaluation/qa/eval_prompt.py) - [libs/langchain/langchain/evaluation/agents/trajectory_eval_prompt.py](https://github.com/langchain-ai/langchain/blob/HEAD/libs/langchain/langchain/evaluation/agents/trajectory_eval_prompt.py) - [libs/langchain/langchain/chains/constitutional_ai/principles.py](https://github.com/langchain-ai/langchain/blob/HEAD/libs/langchain/langchain/chains/constitutional_ai/principles.py) - [libs/langchain/langchain/chains/constitutional_ai/prompts.py](https://github.com/langchain-ai/langchain/blob/HEAD/libs/langchain/langchain/chains/constitutional_ai/prompts.py) - [SECURITY.md](https://github.com/langchain-ai/langchain/blob/HEAD/SECURITY.md) - [libs/langchain/tests/README.md](https://github.com/langchain-ai/langchain/blob/HEAD/libs/langchain/tests/README.md)The LangChain testing strategy is a multi-faceted approach designed to ensure code quality, feature correctness, and model safety. It combines traditional software testing practices with novel, LLM-based evaluation techniques. The strategy mandates that every new feature or bug fix must be accompanied by tests, which are organized into distinct categories based on their purpose and dependencies.
The core framework for testing is pytest. A suite of make commands is provided to streamline development, including running tests, linting, and formatting code. A significant and unique aspect of LangChain's testing is "Evaluation," where language models are used to assess the performance, correctness, and safety of other generative models and agents. This includes structured QA evaluation, agent trajectory analysis, and applying "Constitutional AI" principles to refine model outputs.
Sources: CLAUDE.md:39-41, libs/langchain/langchain/evaluation/qa/eval_prompt.py, libs/langchain/langchain/evaluation/agents/trajectory_eval_prompt.py, libs/langchain/langchain/chains/constitutional_ai/principles.py, libs/langchain/tests/README.md
Test Organization and Execution
Tests are structurally divided based on whether they require network access. This separation ensures that core logic can be verified quickly and deterministically without external dependencies.
Sources: CLAUDE.md:44-46
Directory Structure
- Unit Tests: Located in
tests/unit_tests/, these tests are forbidden from making network calls. They focus on testing individual components in isolation. - Integration Tests: Located in
tests/integration_tests/, these tests are permitted to make network calls to external APIs and services. They verify the interaction between different components and with third-party integrations.
Sources: CLAUDE.md:44-46
Test Execution
A set of make commands simplifies the process of running tests and maintaining code quality.
| Command | Description |
|---|---|
make test | Runs all unit tests (no network access required). |
make lint | Lints the codebase to check for style issues. |
make format | Automatically formats the code to conform to project standards. |
uv run --group test pytest ... | Runs a specific test file using pytest. |
Sources: CLAUDE.md:163-169
Types of Tests
LangChain employs several layers of testing, from standard unit and integration tests to specialized LLM-based evaluations.
Unit and Integration Tests
Standard tests are written using pytest. A key quality checklist for these tests includes:
- Ensuring tests fail when the logic is broken.
- Covering the "happy path" of execution.
- Testing edge cases and error conditions.
- Using fixtures and mocks for external dependencies in unit tests.
- Maintaining deterministic behavior to avoid flaky tests.
Sources: CLAUDE.md:48-55
For partner integrations, a standardized testing interface, ChatModelIntegrationTests, is often used to ensure that different chat model implementations adhere to a common set of behaviors.
This diagram shows how a specific partner integration test class inherits from the standard ChatModelIntegrationTests.
LLM-based Evaluation
A unique aspect of LangChain is using LLMs to evaluate the output of other models. This is crucial because generative models are notoriously difficult to evaluate with traditional metrics.
Sources: libs/langchain/README.md:66-69
QA Evaluation
This method grades a model's answer to a question by comparing it against a "true" answer or a given context. The evaluator LLM is prompted to score the student's answer as CORRECT or INCORRECT.
This diagram illustrates the flow of QA evaluation.
Several prompt templates are available for this purpose:
| Prompt Template | Input Variables | Description |
|---|---|---|
PROMPT | query, result, answer | Grades a student answer based on a true answer. |
CONTEXT_PROMPT | query, context, result | Grades a student answer based on provided context. |
COT_PROMPT | query, context, result | A Chain-of-Thought prompt that requires the LLM to explain its reasoning before grading. |
SQL_PROMPT | query, answer, result | Specifically designed to compare a submitted SQL query against an expert answer for semantic equivalence. |
Sources: libs/langchain/langchain/evaluation/qa/eval_prompt.py
Agent Trajectory Evaluation
This form of evaluation assesses the entire process an agent takes to arrive at an answer, not just the final result. The evaluator LLM is given the user's question, the tools available to the agent, the sequence of tool calls made by the agent (the "trajectory"), and the final answer. It then provides a score from 1 to 5 based on several criteria.
The evaluation criteria are:
- Is the final answer helpful?
- Is the sequence of tool usage logical?
- Are the tools used in a helpful way?
- Does the agent use too many steps?
- Are the appropriate tools used?
This flow shows the inputs for evaluating an agent's trajectory.
Constitutional AI
Constitutional AI is a process for refining model outputs to be safer and more aligned with a set of principles. It's a two-step process: Critique and Revision. An LLM is first asked to critique a model's response based on a specific "constitutional principle" and then asked to revise the response according to the critique.
This process can be considered a form of automated testing and self-correction.
This diagram shows the two-step critique and revision process of Constitutional AI.
A ConstitutionalPrinciple is a data structure containing a critique_request and a revision_request.
| Principle Name | Critique Request |
|---|---|
harmful1 | Identify specific ways in which the assistant's last response is harmful, unethical, racist, sexist, toxic, dangerous, or illegal. |
illegal | Discuss whether the assistant provided any advice that may encourage illegal or dangerous activity... |
uo-reasoning-2 | Discuss whether the AI model's response is plausible, logically valid, sound, consistent and coherent. |
uo-ethics-3 | Discuss whether the AI model's response contains any protected information that should not be shared... |
Sources: libs/langchain/langchain/chains/constitutional_ai/principles.py:7-320
Security in Testing
The security policy emphasizes that developers are responsible for the security implications of their code, especially for Tools that interact with the real world. While building applications, developers should follow best practices like limiting permissions and anticipating potential misuse.
Testing should account for these risks. For example, when testing an agent with file system access, tests should verify that it cannot delete files or read sensitive information outside its designated scope. Tools in langchain and langchain-community are explicitly out of scope for bug bounties, reinforcing that their security is the developer's responsibility.
Sources: SECURITY.md:5-26, 48-54