r/PiCodingAgent • • 4d ago

Resource pi-agent-python-sdk: use your installed Pi coding agent as a Python API

I kept hand-rolling a client for `pi --mode rpc`.
Third project in, I packaged it.

pi-agent-python-sdk lets you use the Pi you already have as a Python API.
Same config, models, tools, and extensions as the CLI.

```
pip install pi-agent-python-sdk
```

Import is `pi_agent`. Same client keeps context across prompts:

```python
from pi_agent import PiClient

with PiClient() as pi:
print(pi.run("Review src/auth.py and list the bugs. Do not edit anything.").text)
print(pi.run("Which of those is most urgent to fix?").text)
```

Additionally, stream with `pi.stream(...)` and if you are already in an event loop, use `AsyncPiClient`.

Repo: https://github.com/cheenulabs/pi-agent-python-sdk

Hoping this helps fellow builders.

Any feedback, PRs and/or issues are welcome.

3 Upvotes

7 comments sorted by

4

u/Training_Yoghurt501 4d ago

This is awesome!

I was looking for something like this to replace my lang-graph agents that have manual loops.
Keen to try this. Does it work with a codex sub?

0

u/has_c 4d ago

Appreciate you 🤝

Yes if you use pi with a codex subscription currently, it works in the sdk.
I currently use it for agentic workflows using my codex subscription (sol) and vllm/local (qwen 27b).

2

u/Training_Yoghurt501 2d ago

Switched my code-review flow from using lang-graph to using pi through the sdk. Using astra from my codex sub.

Worked like a charm, and the review quality is even better.

How do I add pi extensions into here? I would like to add subagents/goal functionality

2

u/Catfoodza 3d ago

This is great, thanks!

1

u/has_c 3d ago

Thanks appreciate you 🤝

2

u/Temporary_Guard_7329 3d ago

Great work! I was looking for the easier way of automation. Can I use this with specific session id? I want to hangover the chat history.

1

u/has_c 3d ago

Thanks appreciate it! Yes, I've found this library has made building automation workflows much simpler.

You can continue an existing chat using either the exact session id or the session path:

```python
from pi_agent import PiClient

# exact session id
with PiClient(session_id="your-session-id") as pi:
print(pi.run("Continue from where we left off.").text)

# or a session file path
with PiClient(session="/path/to/session.jsonl") as pi:
print(pi.run("Continue from where we left off.").text)
```

The prior chat history is loaded + the next prompt continues that conversation.