Environment:Langchain ai Langchain Unit Test Network Isolation
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Testing |
| Last Updated | 2026-02-11 14:00 GMT |
Overview
Network-isolated test environment using `pytest-socket` to enforce no network access during unit tests.
Description
All LangChain packages enforce network isolation during unit tests using the `pytest-socket` plugin. Tests run with `--disable-socket --allow-unix-socket` flags, ensuring no HTTP/HTTPS calls are made. Integration tests in separate directories are allowed network access. VCR cassette recording is used for deterministic replay of API responses.
Usage
This environment is mandatory for all unit test execution across the monorepo. It ensures tests are deterministic, fast, and do not depend on external services. Integration tests (in `tests/integration_tests/`) are exempt from this restriction.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| Python | >= 3.10.0 | Same as core |
| CI Runner | ubuntu-latest | GitHub Actions |
| Timeout | 20 minutes | Per test workflow run |
Dependencies
Test Packages
- `pytest` >= 8.0.0, < 10.0.0
- `pytest-socket` >= 0.7.0, < 1.0.0
- `pytest-asyncio` >= 0.21.1, < 2.0.0
- `pytest-mock` >= 3.10.0, < 4.0.0
- `pytest-xdist` >= 3.6.1, < 4.0.0
- `freezegun` >= 1.2.2, < 2.0.0
- `syrupy` >= 4.0.2, < 6.0.0
- `vcrpy` >= 8.0.0, < 9.0.0 (for integration tests)
- `blockbuster` >= 1.5.18, < 1.6.0
Credentials
No credentials required for unit tests. All tracing variables are explicitly unset:
- `LANGCHAIN_TRACING_V2=""`
- `LANGCHAIN_API_KEY=""`
- `LANGSMITH_API_KEY=""`
- `LANGSMITH_TRACING=""`
- `LANGCHAIN_PROJECT=""`
Quick Install
# Install test dependencies
uv sync --group test
# Run unit tests (network-isolated)
make test
Code Evidence
Network isolation from `libs/core/Makefile:19`:
uv run --group test pytest tests/unit_tests --disable-socket --allow-unix-socket
VCR configuration from `libs/standard-tests/langchain_tests/conftest.py:102-127`:
def base_vcr_config() -> dict[str, Any]:
return {
"record_mode": "once",
"filter_headers": _BASE_FILTER_HEADERS.copy(),
"match_on": ["method", "uri", "body"],
"cassette_library_dir": "tests/cassettes",
}
VCR header filtering from `libs/standard-tests/langchain_tests/conftest.py:95-99`:
_BASE_FILTER_HEADERS = ["authorization", "x-api-key", "api-key"]
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `SocketBlockedError: A]test tried to use socket` | Unit test making network call | Mock the external dependency or move test to integration_tests/ |
| `pytest.PytestUnraisableExceptionWarning` | Async socket cleanup issue | Ensure proper async fixture teardown |
| `VCR cassette not found` | Missing recorded API response | Run integration tests with `--vcr-record=once` to record cassettes |
Compatibility Notes
- Unix Sockets Allowed: `--allow-unix-socket` permits local IPC (needed for some database connections).
- GitHub Actions: CI runs on `ubuntu-latest` with Python 3.11 for the main check workflow.
- VCR Filtering: Authorization headers are automatically stripped from recorded cassettes to prevent credential leaks.