Chaos testing middleware for LangChain agents
Project description
LangChain Chaos Middleware
A middleware for LangChain agents that intentionally injects failures (exceptions) into tool and model calls to test agent resilience.
Features
- Failure Injection: Randomly raises exceptions based on a configurable failure rate.
- Targeted Chaos: Include or exclude specific tools from chaos.
- Safety Mechanism: Requires an environment variable (
ENABLE_CHAOS=true) to be active, preventing accidental production issues. - Customizable Exceptions: Choose which exceptions to raise (e.g., network errors, rate limits).
Installation
pip install langchain-chaos-middleware
Usage
Basic Example
import os
from langchain.agents import create_agent
from chaos_middleware import ChaosMiddleware, NETWORK_ERRORS
# 1. Enable Chaos (Safety Check)
os.environ["ENABLE_CHAOS"] = "true"
# 2. Configure Middleware
chaos_config = {
"failure_rate": 0.2, # 20% chance of failure
"exception_types": NETWORK_ERRORS,
"exclude_tools": ["save_memory"], # Don't break critical tools
}
chaos = ChaosMiddleware(chaos_config)
# 3. Inject into Agent
agent = create_agent(
model="gpt-4o",
tools=[...],
middleware=[chaos]
)
# 4. Run Agent
# The agent will now experience random network errors!
Configuration
The ChaosMiddleware is initialized with a ChaosConfig dictionary:
| Key | Type | Default | Description |
|---|---|---|---|
failure_rate |
float |
0.1 |
Probability of failure (0.0 to 1.0). |
exception_types |
List[Exception] |
[] |
List of exceptions to randomly choose from. |
include_tools |
List[str] |
None |
If set, only these tools will be targeted. |
exclude_tools |
List[str] |
[] |
These tools will NEVER fail. |
seed |
int |
None |
Random seed for reproducible chaos. |
safety_key |
str |
"ENABLE_CHAOS" |
Env var name required to enable the middleware. |
Default Exception Profiles
The library provides pre-built lists of exceptions for common scenarios:
NETWORK_ERRORS:[TimeoutError, ConnectionError]LLM_ERRORS:[RateLimitError, ServiceUnavailableError]CRITICAL_ERRORS:[ValueError, KeyError]
Custom Exception Profiles
You can define your own lists of exceptions to simulate specific scenarios (e.g., database failures, custom API errors). Just create a list of exception classes and pass it to the exception_types configuration:
class DatabaseError(Exception):
pass
MY_DB_ERRORS = [DatabaseError]
config = {
"exception_types": MY_DB_ERRORS,
# ...
}
Safety
To prevent accidental chaos in production, the middleware checks for an environment variable (default: ENABLE_CHAOS). If this variable is not set to "true", the middleware acts as a pass-through and does nothing.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_chaos_middleware-0.1.0.tar.gz.
File metadata
- Download URL: langchain_chaos_middleware-0.1.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a1c2fab80f97241f56178e7294be4fb451e0b6e0582437b6716c14aaa99a001
|
|
| MD5 |
181be66ad720f25987edbdf8e0ae9ca8
|
|
| BLAKE2b-256 |
01f3e8c0d5ebdbbac168c8c48e4b8586d29d57fa77487a2a956b729b9f6b402d
|
File details
Details for the file langchain_chaos_middleware-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_chaos_middleware-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f262a28b95803c98694b78af4fb0a63701676afe12ab70fc6b8d2e4fc8f4fbe0
|
|
| MD5 |
45f61cc67fa25a9b3689c9bebd2be8ec
|
|
| BLAKE2b-256 |
f4e6be417425e9f2f2d765315ce0cca60696db16102d683d16678f3e23bce8a4
|