Skip to main content

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-ai and @earendil-works/pi-coding-agent resolvable (install them where the sidecar can reach them, or point node_modules_dir at a node_modules directory that has them). Note: NODE_PATH does not work for ESM, so node_modules_dir resolves each package via its package.json entry.

Install

pip install langchain-pi          # or: uv add langchain-pi
npm install @earendil-works/pi-ai @earendil-works/pi-coding-agent

Usage

create_chat is the simplest entry — pass a provider and it routes to the right backend (opencode/opencode-go → native Zen, everything else → Pi):

from langchain_pi import create_chat

free = create_chat("opencode", "deepseek-v4-flash-free")          # free, no key
go = create_chat("opencode-go", "glm-5", api_key="...")           # subscription
codex = create_chat("openai-codex", "gpt-5.3-codex-spark")        # via Pi

Or construct a backend directly:

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="openai-codex", model="gpt-5.3-codex-spark",
       node_modules_dir="/path/to/your/project/node_modules")

Native opencode/Zen (no Pi)

OpenCode Zen is an OpenAI-compatible endpoint, so you don't need Pi. langchain_pi.opencode ships ChatOpencode, a thin ChatOpenAI subclass pointed at it (langchain-openai ships as a dependency).

The API key is optional: free models work with no key (anonymous, IP-rate- limited). For paid models pass a key via OPENCODE_API_KEY (env) or api_key.

from langchain_pi.opencode import ChatOpencode

free = ChatOpencode("deepseek-v4-flash-free")  # no key
paid = ChatOpencode("glm-5")  # OPENCODE_API_KEY or api_key
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.

License

MIT

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

langchain_pi-0.4.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_pi-0.4.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file langchain_pi-0.4.0.tar.gz.

File metadata

  • Download URL: langchain_pi-0.4.0.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

Hashes for langchain_pi-0.4.0.tar.gz
Algorithm Hash digest
SHA256 70061d8a090bdf999643e5e9f3aa6f93996629c931b60b654c60088057d1e552
MD5 4ef414e12573d4a3cee4ea75d6dd0014
BLAKE2b-256 2aa1372a5e2acbb5586d691079dc7a519247293c6f1e35cd23ddda5d5b6f300b

See more details on using hashes here.

File details

Details for the file langchain_pi-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: langchain_pi-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 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

Hashes for langchain_pi-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2aa8db5dac18ced8469b7677c87be9d704afc58c6522163ae1e8772f1281f54a
MD5 bc0f5f47fe8040d36e269361cd0df618
BLAKE2b-256 bee740ceeb7012d575e69ebe253a30eba9ccb4d80b4766802ebe8ec7c062db2c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page