LangChain BaseChatModel adapter for Pi (@earendil-works/pi-ai) via a Node sidecar — use Pi with any provider, model and auth from LangChain/LangGraph in Python. Includes a native opencode/Zen chat model (no pi).
Project description
langchain-pi
A LangChain BaseChatModel adapter for Pi
(@earendil-works/pi-ai),
the Python twin of langchain-pi-ts.
Use Pi — and any provider, model and credential it resolves — from LangChain and
LangGraph in Python, with native tool calling and streaming.
Pi is a TypeScript/Node stack with no Python SDK, so langchain-pi drives a tiny
Node sidecar that wraps pi-ai's streamSimple + ModelRegistry/AuthStorage.
Node keeps owning provider dispatch, OAuth refresh and the provider stealth
headers; Python only frames the request and reconstructs the streamed events.
Python never touches a token.
Requirements
- Python ≥ 3.9
- Node ≥ 22.19.0 on
PATH, with@earendil-works/pi-aiand@earendil-works/pi-coding-agentresolvable (install them where the sidecar can reach them, or pointnode_modules_dirat anode_modulesdirectory that has them). Note:NODE_PATHdoes not work for ESM, sonode_modules_dirresolves each package via itspackage.jsonentry. - A provider authenticated in
~/.pi(e.g.openai-codex), exactly as for the Pi CLI / the TS package.
Install
pip install langchain-pi # or: uv add langchain-pi
npm install @earendil-works/pi-ai @earendil-works/pi-coding-agent
Usage
from langchain_pi import ChatPi
model = ChatPi(
provider="openai-codex",
model="gpt-5.3-codex-spark",
reasoning="minimal",
system="You are a helpful assistant.",
)
print(model.invoke("Hello!").content)
Tool calling
Accepts any LangChain tool; schemas are converted to the JSON Schema pi-ai expects.
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"It is sunny in {city}, 24C."
agent_model = model.bind_tools([get_weather])
Streaming
for chunk in model.stream("Write a haiku."):
print(chunk.content, end="")
LangGraph
from langgraph.prebuilt import create_react_agent
agent = create_react_agent(model, [get_weather])
agent.invoke({"messages": [("user", "What's the weather in Paris?")]})
Pointing at your Node install
If pi-ai isn't resolvable from the sidecar's location, pass the node_modules
directory that contains it:
ChatPi(provider="opencode", model="deepseek-v4-flash-free",
node_modules_dir="/path/to/your/project/node_modules")
Native opencode/Zen (no Pi)
For OpenCode Zen you don't need Pi — it's an
OpenAI-compatible endpoint. langchain_pi.opencode ships a ChatOpencode (a thin
ChatOpenAI subclass) that points at it and reads the key opencode auto-provisions
into ~/.local/share/opencode/auth.json. Install the optional extra:
pip install "langchain-pi[opencode]"
Free models work two ways — with or without an API key:
- No key → the anonymous IP-rate-limited trial (works out of the box).
- With a key (auto-read from
auth.json, orOPENCODE_API_KEY/api_key) → higher limits.
Paid models require a key.
from langchain_pi.opencode import ChatOpencode
# Free — no key needed (anonymous), or auto-uses a key if present for higher limits:
free = ChatOpencode("deepseek-v4-flash-free")
# Paid — requires a key (auto-read or explicit api_key):
paid = ChatOpencode("glm-5")
go = ChatOpencode("glm-5", tier="go")
Free models: deepseek-v4-flash-free, big-pickle, mimo-v2.5-free, nemotron-3-super-free.
Notes
- Node is a runtime prerequisite — the heavy provider/auth logic lives in pi-ai.
- Tool-call deltas and usage/cost metadata are reconstructed 1:1 with the TS twin.
- Cancellation (e.g. LangGraph) aborts the in-flight provider request via the sidecar.
License
MIT
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 langchain_pi-0.2.1.tar.gz.
File metadata
- Download URL: langchain_pi-0.2.1.tar.gz
- Upload date:
- Size: 24.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abeb8c2fd64e3da84fcd3024c9fd9d239d1db37187a9c130178f48dd06ad13a6
|
|
| MD5 |
cf32976fd69bfecc647f06017225c2f0
|
|
| BLAKE2b-256 |
3211879865ec4e75a3863b87ec8afe2678ff7f6d276bb83c642b78d9df6ced01
|
File details
Details for the file langchain_pi-0.2.1-py3-none-any.whl.
File metadata
- Download URL: langchain_pi-0.2.1-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
909f76dc0d6cebf4e6bbc22eacec214eb0306bc9ede3f464c4271134b0d280aa
|
|
| MD5 |
5aef2e0d47061ccf52b49134721fb6a5
|
|
| BLAKE2b-256 |
449702265084244dc68de76214e1811a230e7860b5a987fb78f23ac6f9376959
|