LangChain wrapper with automatic fallback and alert notifications when API keys fail
Project description
fika-langwatch
LangChain wrapper with automatic fallback and alert notifications when API keys fail.
Copyright (c) 2026 FIKA Private Limited. All Rights Reserved.
Features
- Automatic Fallback: When a model fails, automatically try the next one
- Alert Notifications: Get notified via Email, Slack, or Webhooks when keys fail
- Rate Limiting: Built-in cooldown to prevent alert spam
- Tool Binding: Call
.bind_tools()once, applies to ALL underlying models - Sync & Async: Supports both
.invoke()and.ainvoke()
Installation
pip install fika-langwatch
# With optional dependencies
pip install fika-langwatch[email] # Email alerts
pip install fika-langwatch[slack] # Slack alerts
pip install fika-langwatch[webhook] # Webhook alerts
pip install fika-langwatch[all] # All alert channels
# Provider-specific
pip install fika-langwatch[google] # Google Gemini
pip install fika-langwatch[openai] # OpenAI
pip install fika-langwatch[anthropic] # Anthropic Claude
pip install fika-langwatch[providers] # All providers
Quick Start
Option 1: Config-based (Recommended)
from langwatch import ChatWithFallback
from langwatch.alerts import EmailAlert, SlackAlert
from langchain_core.messages import HumanMessage
# Create with config - models are created automatically
chat = ChatWithFallback.from_config(
models=[
{
"name": "gemini-1",
"provider": "google",
"model": "gemini-2.5-flash",
"api_key": "AIza...",
},
{
"name": "gemini-2",
"provider": "google",
"model": "gemini-2.5-flash",
"api_key": "AIza...",
},
{
"name": "fallback",
"provider": "openrouter",
"model": "x-ai/grok-4.1-fast",
"api_key": "sk-...",
"is_fallback": True,
},
],
alerts=[
EmailAlert(
smtp_server="smtp.gmail.com",
smtp_port=587,
username="alerts@company.com",
password="app-password",
to=["ops@company.com"],
),
SlackAlert(webhook_url="https://hooks.slack.com/services/..."),
],
cooldown_seconds=3600, # 1 alert per hour
)
# Bind tools - applies to ALL models
chat_with_tools = chat.bind_tools([your_tool_1, your_tool_2])
# Use like any LangChain model
response = await chat_with_tools.ainvoke([HumanMessage(content="Hello!")])
Option 2: Manual Models (Full Flexibility)
from langwatch import ChatWithFallback
from langwatch.alerts import EmailAlert
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_openai import ChatOpenAI
# Create your own models
models = [
ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key="..."),
ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key="..."),
ChatOpenAI(model="grok-4.1", base_url="https://openrouter.ai/api/v1", api_key="..."),
]
chat = ChatWithFallback(
models=models,
model_names=["gemini-1", "gemini-2", "fallback"],
alerts=[EmailAlert(...)],
)
# Bind tools and use
chat_with_tools = chat.bind_tools(tools)
response = await chat_with_tools.ainvoke(messages)
Supported Providers
When using from_config(), these providers are auto-created:
| Provider | Value | LangChain Class |
|---|---|---|
| Google Gemini | "google" |
ChatGoogleGenerativeAI |
| OpenAI | "openai" |
ChatOpenAI |
| Anthropic Claude | "anthropic" |
ChatAnthropic |
| OpenRouter | "openrouter" |
ChatOpenAI with OpenRouter base_url |
For other providers, create the model manually and pass it directly.
Alert Channels
from langwatch.alerts import EmailAlert
alert = EmailAlert(
smtp_server="smtp.gmail.com",
smtp_port=587,
username="alerts@company.com",
password="your-app-password", # Use Gmail App Password
to=["ops@company.com"],
cc=["team@company.com"], # Optional
from_name="LangWatch Alerts", # Optional
)
Slack
from langwatch.alerts import SlackAlert
alert = SlackAlert(
webhook_url="https://hooks.slack.com/services/T.../B.../xxx",
channel="#alerts", # Optional override
username="LangWatch Bot", # Optional
)
Webhook
from langwatch.alerts import WebhookAlert
alert = WebhookAlert(
url="https://your-api.com/alerts",
headers={"Authorization": "Bearer token"},
method="POST",
)
Callbacks
def on_key_failure(key_name: str, error: str):
print(f"Key {key_name} failed: {error}")
def on_fallback_activated(fallback_key: str):
print(f"Now using fallback: {fallback_key}")
chat = ChatWithFallback.from_config(
models=[...],
alerts=[...],
on_key_failure=on_key_failure,
on_fallback_activated=on_fallback_activated,
)
Check Status
# Get status of all models
status = chat.get_status()
print(status)
# {
# "total_keys": 3,
# "healthy_keys": 2,
# "failed_keys": 1,
# "all_primary_failed": False,
# "keys": [...]
# }
How It Works
- Request comes in → Try first model
- Model fails → Mark as unhealthy, try next model
- All primary models fail → Activate fallback, send alerts
- Alerts are rate-limited → Only 1 alert per hour (configurable)
- Model recovers → Automatically marked as healthy on next success
License
Copyright (c) 2026 FIKA Private Limited. All Rights Reserved.
This is proprietary software. Unauthorized copying, modification, or distribution is prohibited.
For licensing inquiries, contact: rahul@pupiltree.ai
Project details
Release history Release notifications | RSS feed
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 fika_langwatch-0.1.0.tar.gz.
File metadata
- Download URL: fika_langwatch-0.1.0.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f297a9f4567d23df52ec2484532595fe2d0f05682258e617aafcf13d863af952
|
|
| MD5 |
501d0803f1770c70bf52a3de0444a212
|
|
| BLAKE2b-256 |
a6906d9d8082547c1b4f2b93628406126dd3be4effd6ae75b6ff708d90f8d1aa
|
File details
Details for the file fika_langwatch-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fika_langwatch-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0b1a8588735e778085d9841248ba84ce2a230854a1c7dfe5646706c9f5bc551
|
|
| MD5 |
6963e31cfa6b9a0d088eaa1e067bc46c
|
|
| BLAKE2b-256 |
c5d76c8b4261f469407b5c4e1b78c11556efad12ffc2aec0aef64791175a79c6
|