# langmiddle
Middlewares for LangChain / LangGraph
[](https://github.com/alpha-xone/langmiddle/actions/workflows/ci.yml)
[](https://pypi.org/project/langmiddle/)
[](https://pypi.org/project/langmiddle/)
[](https://github.com/alpha-xone/langmiddle/blob/main/LICENSE)
## Overview
Production-ready middleware for **LangChain** and **LangGraph v1** with multi-backend chat history persistence. Store conversations in SQLite, Supabase, or Firebase with zero configuration required.
**Key Features:**
- ✅ **LangChain/LangGraph v1 Compatible**: Native middleware pattern support
- ⚡ **Zero Config Start**: Defaults to in-memory SQLite—no setup needed
- 🔄 **Multi-Backend Storage**: Switch between SQLite, PostgreSQL, Supabase, Firebase with one parameter
- 🔒 **Production Ready**: JWT authentication, RLS support, type-safe
## Available middleware
| Middleware | Description |
|---|---|
| ToolMessageRemover | Removes tool-related messages from the conversation state (pre/post agent). |
| ChatSaver | Persists chat history to configurable backends (SQLite, Postgres, Supabase, Firebase). |
## Installation
**Core Package** (SQLite only):
```bash
pip install langmiddle
```
**With Optional Backends:**
```bash
# For PostgreSQL support
pip install langmiddle[postgres]
# For Supabase support
pip install langmiddle[supabase]
# For Firebase support
pip install langmiddle[firebase]
# All backends
pip install langmiddle[all]
```
## Quick Start - LangChain Middleware
```python
from langmiddle.history import ChatSaver, ToolMessageRemover, StorageContext
# Initialize middleware with desired backend
agent = create_agent(
model="gpt-4o",
tools=[],
context_schema=StorageContext,
middleware=[
ToolMessageRemover(),
ChatSaver(backend="sqlite", db_path="./chat_history.db")
],
)
agent.invoke(
input={"messages": [{"role": "user", "content": "Hello!"}]},
context={
"user_id": "user-123",
"session_id": "session-123",
}
)
```
## Storage Backends
| Backend | Use Case | Pros | Cons | Setup |
|----------|----------|------|------|-------|
| **SQLite** | Development, Single-user | Simple, Local, Fast, No setup | Not distributed | None |
| **PostgreSQL** | Self-hosted, Custom auth | Full control, Standard SQL, Flexible | Manual setup | Connection string |
| **Supabase** | Production Web Apps | Managed PostgreSQL, RLS, Real-time | Supabase-specific | Environment vars |
| **Firebase** | Mobile, Google ecosystem | Real-time, Managed, Global | Google-specific | Service account |
### SQLite Configuration
```python
# Local file
backend_type="sqlite", db_path="./chat.db"
# In-memory (testing)
backend_type="sqlite", db_path=":memory:"
```
### PostgreSQL Configuration
```bash
# .env file or environment variables
POSTGRES_CONNECTION_STRING=postgresql://user:password@localhost:5432/dbname
```
```python
# Or pass directly
backend_type="postgres",
connection_string="postgresql://user:password@localhost:5432/dbname",
auto_create_tables=True
```
See [PostgreSQL Setup Guide](docs/POSTGRES_SETUP.md) for details.
### Supabase Configuration
```bash
# .env file or environment variables
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_anon_key
```
### Firebase Configuration
```python
# Service account credentials file
backend_type="firebase", credentials_path="./firebase-creds.json"
# Or use GOOGLE_APPLICATION_CREDENTIALS environment variable
```
Raw data
{
"_id": null,
"home_page": null,
"name": "langmiddle",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "langchain, langgraph, middleware, ai, llm, agents",
"author": null,
"author_email": "Alpha x1 <alpha.xone@outlook.com>",
"download_url": "https://files.pythonhosted.org/packages/c5/d7/9995c5e7c45347bee8a88f05455480cb44499b5b345e10031352d7459a57/langmiddle-0.1.2.tar.gz",
"platform": null,
"description": "# langmiddle\n\nMiddlewares for LangChain / LangGraph\n\n[](https://github.com/alpha-xone/langmiddle/actions/workflows/ci.yml)\n[](https://pypi.org/project/langmiddle/)\n[](https://pypi.org/project/langmiddle/)\n[](https://github.com/alpha-xone/langmiddle/blob/main/LICENSE)\n\n## Overview\n\nProduction-ready middleware for **LangChain** and **LangGraph v1** with multi-backend chat history persistence. Store conversations in SQLite, Supabase, or Firebase with zero configuration required.\n\n**Key Features:**\n- \u2705 **LangChain/LangGraph v1 Compatible**: Native middleware pattern support\n- \u26a1 **Zero Config Start**: Defaults to in-memory SQLite\u2014no setup needed\n- \ud83d\udd04 **Multi-Backend Storage**: Switch between SQLite, PostgreSQL, Supabase, Firebase with one parameter\n- \ud83d\udd12 **Production Ready**: JWT authentication, RLS support, type-safe\n\n## Available middleware\n\n| Middleware | Description |\n|---|---|\n| ToolMessageRemover | Removes tool-related messages from the conversation state (pre/post agent). |\n| ChatSaver | Persists chat history to configurable backends (SQLite, Postgres, Supabase, Firebase). |\n\n## Installation\n\n**Core Package** (SQLite only):\n```bash\npip install langmiddle\n```\n\n**With Optional Backends:**\n```bash\n# For PostgreSQL support\npip install langmiddle[postgres]\n\n# For Supabase support\npip install langmiddle[supabase]\n\n# For Firebase support\npip install langmiddle[firebase]\n\n# All backends\npip install langmiddle[all]\n```\n\n## Quick Start - LangChain Middleware\n\n```python\nfrom langmiddle.history import ChatSaver, ToolMessageRemover, StorageContext\n\n# Initialize middleware with desired backend\nagent = create_agent(\n model=\"gpt-4o\",\n tools=[],\n context_schema=StorageContext,\n middleware=[\n ToolMessageRemover(),\n ChatSaver(backend=\"sqlite\", db_path=\"./chat_history.db\")\n ],\n)\n\nagent.invoke(\n input={\"messages\": [{\"role\": \"user\", \"content\": \"Hello!\"}]},\n context={\n \"user_id\": \"user-123\",\n \"session_id\": \"session-123\",\n }\n)\n```\n\n## Storage Backends\n\n| Backend | Use Case | Pros | Cons | Setup |\n|----------|----------|------|------|-------|\n| **SQLite** | Development, Single-user | Simple, Local, Fast, No setup | Not distributed | None |\n| **PostgreSQL** | Self-hosted, Custom auth | Full control, Standard SQL, Flexible | Manual setup | Connection string |\n| **Supabase** | Production Web Apps | Managed PostgreSQL, RLS, Real-time | Supabase-specific | Environment vars |\n| **Firebase** | Mobile, Google ecosystem | Real-time, Managed, Global | Google-specific | Service account |\n\n### SQLite Configuration\n\n```python\n# Local file\nbackend_type=\"sqlite\", db_path=\"./chat.db\"\n\n# In-memory (testing)\nbackend_type=\"sqlite\", db_path=\":memory:\"\n```\n\n### PostgreSQL Configuration\n\n```bash\n# .env file or environment variables\nPOSTGRES_CONNECTION_STRING=postgresql://user:password@localhost:5432/dbname\n```\n\n```python\n# Or pass directly\nbackend_type=\"postgres\",\nconnection_string=\"postgresql://user:password@localhost:5432/dbname\",\nauto_create_tables=True\n```\n\nSee [PostgreSQL Setup Guide](docs/POSTGRES_SETUP.md) for details.\n\n### Supabase Configuration\n\n```bash\n# .env file or environment variables\nSUPABASE_URL=your_supabase_url\nSUPABASE_ANON_KEY=your_anon_key\n```\n\n### Firebase Configuration\n\n```python\n# Service account credentials file\nbackend_type=\"firebase\", credentials_path=\"./firebase-creds.json\"\n\n# Or use GOOGLE_APPLICATION_CREDENTIALS environment variable\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Middlewares for LangChain / LangGraph",
"version": "0.1.2",
"project_urls": {
"Documentation": "https://github.com/alpha-xone/langmiddle#readme",
"Homepage": "https://github.com/alpha-xone/langmiddle",
"Issues": "https://github.com/alpha-xone/langmiddle/issues",
"Repository": "https://github.com/alpha-xone/langmiddle"
},
"split_keywords": [
"langchain",
" langgraph",
" middleware",
" ai",
" llm",
" agents"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "4a3992fe2667d0a615aacba592a5e653f5e18670accc0c3435b92b42903e6229",
"md5": "149291b01e802dd9fcfe98dff79472b0",
"sha256": "bec97516dbf13fe5af398f5e68a08219b6f833b935900ce739457906ea02e2ef"
},
"downloads": -1,
"filename": "langmiddle-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "149291b01e802dd9fcfe98dff79472b0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 35131,
"upload_time": "2025-10-27T15:48:10",
"upload_time_iso_8601": "2025-10-27T15:48:10.945009Z",
"url": "https://files.pythonhosted.org/packages/4a/39/92fe2667d0a615aacba592a5e653f5e18670accc0c3435b92b42903e6229/langmiddle-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "c5d79995c5e7c45347bee8a88f05455480cb44499b5b345e10031352d7459a57",
"md5": "52f865c3f7e58ccdf0978a910ea774ef",
"sha256": "344ec87c7b83de940ee4ebced9df99b667e59f9be6397440492aa850db566dfb"
},
"downloads": -1,
"filename": "langmiddle-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "52f865c3f7e58ccdf0978a910ea774ef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 28093,
"upload_time": "2025-10-27T15:48:11",
"upload_time_iso_8601": "2025-10-27T15:48:11.786386Z",
"url": "https://files.pythonhosted.org/packages/c5/d7/9995c5e7c45347bee8a88f05455480cb44499b5b345e10031352d7459a57/langmiddle-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-27 15:48:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "alpha-xone",
"github_project": "langmiddle#readme",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "langchain",
"specs": [
[
">=",
"1.0.0"
]
]
},
{
"name": "langgraph",
"specs": [
[
">=",
"1.0.0"
]
]
},
{
"name": "langchain_core",
"specs": [
[
">=",
"1.0.0"
]
]
},
{
"name": "python-dotenv",
"specs": [
[
">=",
"1.0.0"
]
]
}
],
"lcname": "langmiddle"
}