Pi Coding Agent Python SDK
Python facade for creating Pi Coding Agent sessions, wiring resources, running the bundled TypeScript bridge, exposing MCP servers, and serving the agent through ACP.
The package published to PyPI is pi-coding-agent-python-sdk. The import package is pi_coding_agent.
Install
pip install pi-coding-agent-python-sdk
ACP support is optional:
pip install "pi-coding-agent-python-sdk[acp]"
Provider-backed sessions use the bundled bridge worker and require either Node.js or Bun on PATH. The explicit backend="in_process" mode is a local deterministic mode for tests and offline examples.
Claude Code skill (optional)
This repo ships a Claude Code skill at skills/pi-coding-agent-sdk/ that bundles the SDK docs, all numbered SDK examples, and the hard-won-notes reference. Claude Code users can install it with one command:
npx -y skills add trotsky1997/pi-coding-agent-python-sdk
After install the skill auto-activates whenever a session imports pi_coding_agent, mentions HarnessConfig, or asks about embedding Pi sessions in a benchmark / harness.
Quick Start
import asyncio
from pi_coding_agent import MessageUpdateEvent, create_agent_session
async def main() -> None:
result = await create_agent_session(cwd=".", backend="in_process")
session = result.session
try:
session.subscribe(
lambda event: print(event.assistant_message_event.delta, end="")
if isinstance(event, MessageUpdateEvent)
else None
)
await session.prompt("List the files in this directory.")
finally:
await session.dispose()
asyncio.run(main())
This first example is a local smoke test. For a provider-backed session, use HarnessConfig and keep secrets in environment variables:
import asyncio
from pi_coding_agent import HarnessConfig, create_agent_session_from_config
config = HarnessConfig.from_dict(
{
"run": {"cwd": ".", "backend": "node"},
"env": {
"required": ["NOVITA_API_KEY"],
"optional": ["NOVITA_BASE_URL"],
},
"model": {
"provider": "novita",
"id": "deepseek/deepseek-v4-flash",
"api": "openai-completions",
"base_url": "https://api.novita.ai/v3/openai",
"api_key_env": "NOVITA_API_KEY",
"context_window": 128000,
"max_tokens": 32000,
"reasoning": True,
"compat": {
"thinkingFormat": "deepseek",
"requiresReasoningContentOnAssistantMessages": True,
},
},
"provider_options": {
"temperature": 0,
"tool_choice": "auto",
"parallel_tool_calls": True,
},
"tools": {"allow": ["read", "write", "edit", "bash", "grep", "find", "ls"]},
}
)
async def main() -> None:
result = await create_agent_session_from_config(config)
try:
await result.session.prompt("Inspect the project and summarize the test layout.")
finally:
await result.session.dispose()
asyncio.run(main())
Harness TOML
HarnessConfig can load and dump TOML. It covers runtime selection, model/provider setup, environment passthrough, tool allowlists, resources, extensions, skills, MCP servers, hooks, and session storage.
version = 1
[run]
cwd = "."
backend = "node"
[js]
runtime = "bun"
request_timeout_sec = 30
tool_timeout_sec = 60
compact_timeout_sec = 300
[env]
required = ["NOVITA_API_KEY"]
optional = ["NOVITA_BASE_URL"]
[model]
provider = "novita"
id = "deepseek/deepseek-v4-flash"
api = "openai-completions"
base_url = "https://api.novita.ai/v3/openai"
api_key_env = "NOVITA_API_KEY"
context_window = 128000
max_tokens = 32000
reasoning = true
[model.compat]
thinkingFormat = "deepseek"
requiresReasoningContentOnAssistantMessages = true
[provider_options]
temperature = 0
tool_choice = "auto"
parallel_tool_calls = true
[tools]
allow = ["read", "write", "edit", "bash", "grep", "find", "ls"]
[mcp.servers.time]
command = "uvx"
args = ["mcp-server-time"]
[hooks.PreToolUse.block_rm]
matcher = "bash"
command = "python ./hooks/block_rm.py"
timeout = 5
condition = "bash(*)"
run_async = false
[session]
mode = "create"
import asyncio
from pi_coding_agent import create_agent_session_from_config
async def main() -> None:
result = await create_agent_session_from_config("pi-harness.toml")
try:
await result.session.prompt("What time tools are available?")
finally:
await result.session.dispose()
asyncio.run(main())
Session Snapshots
Session state and harness config are separate by default. Persistent sessions are written by SessionManager as JSON under the configured session directory. Harness config is written as TOML and controls the runtime: model, env passthrough, tools, resources, extensions, skills, MCP, hooks, and session mode.
For a portable handoff, write both into one TOML file:
from pi_coding_agent import HarnessConfig
config = HarnessConfig.load_toml("pi-harness.toml")
config.dump_with_session_snapshot("portable-harness.toml", result.session)
This embeds local resource files by default, stores the current session under [session.snapshot] as compressed base64 JSON, and writes the TOML with an atomic replace in the target directory. Loading the generated TOML restores the snapshot through an in-memory SessionManager, so it does not continue writing to the original sessionFile.
If you only need a config object:
portable = config.with_session_snapshot(result.session, embed_resources=True)
toml_text = portable.to_toml()
External snapshot files are still supported:
[session]
snapshot_path = "session-snapshot.json"
Snapshot restore accepts the SDK JSON shape from session.serialize(), Pi JSONL session files, Harbor ATIF trajectory JSON, and AgentForge Qwen35 training records.
Main Capabilities
- Session creation through
create_agent_session,create_agent_session_from_config,create_agent_session_services, andcreate_agent_session_runtime. - Built-in tools:
read,write,edit,bash,grep,find, andls. - Python custom tools through
ToolDefinition. - TypeScript extension paths, package extension sources, and Python extension factories.
- Skills from local paths, GitHub sources through
npx skills add, and Pi package-loader sources. - Prompt templates,
AGENTS.mddiscovery, system prompt overrides, and append-system-prompt files. - MCP server configuration through
McpConfigor TOML. - Hooks for session, prompt, tool, compact, and stop events.
- Session JSON serialization, ATIF/Qwen35 import/export, and harness TOML snapshots.
- ACP server integration through
pi-coding-agent-acp.
Documentation
- Harness Config
- Resources, Extensions, Skills, MCP, And Hooks
- ACP Integration
- Development And Release Notes
- Example Parity Matrix
- Bridge Validation
Development
Run the default suite with uv:
uv run -m unittest discover -s tests
Build and inspect the package:
uv build
uv run --with twine python -m twine check dist/*
The test suite is designed to run without provider credentials by default. Provider-backed tests are opt-in and should use environment variables, not checked-in secrets.
Metadata
Release files for pi-coding-agent-python-sdk 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pi_coding_agent_python_sdk-0.4.3.tar.gz | 8.1 MB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pi_coding_agent_python_sdk-0.4.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.7 MB
Release files / pi_coding_agent_python_sdk-0.4.3.tar.gz
| Download URL | pi_coding_agent_python_sdk-0.4.3.tar.gz |
|---|---|
| Size | 8.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c2ca915db8a898b4281873abaee24c2c40f7a5de50ca5d1c7917b2b53eee040
|
|
BLAKE2b-256 checksum How to use checksums |
929748acd12eba02b8bf810bb91819e86a14bf52bf43b62d155dcd7058704401
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pi_coding_agent_python_sdk-0.4.3-py3-none-any.whl
| Download URL | pi_coding_agent_python_sdk-0.4.3-py3-none-any.whl |
|---|---|
| Size | 8.6 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
640369d7fef152c23f42d2ea337a8cf667c6de38ff7eccdedbb765aa59004694
|
|
BLAKE2b-256 checksum How to use checksums |
61f06c17a16ad4fd980c9597a54cf58eeda3af23cd5d69b40f84d59b3e9c262a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for pi-coding-agent-python-sdk 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.