pi-json-tools
Smart Recovery for Qwen Coder - Auto hallucination detection, auto reminder, smart recovery for qwen2.5-coder family
Package details
Install pi-json-tools from npm and Pi will load the resources declared by the package manifest.
$ pi install npm:pi-json-tools- Package
pi-json-tools- Version
5.2.2- Published
- May 3, 2026
- Downloads
- 920/mo ยท 920/wk
- Author
- dayuwidayadi
- License
- MIT
- Types
- extension
- Size
- 25.1 KB
- Dependencies
- 0 dependencies ยท 0 peers
Pi manifest JSON
{
"extensions": [
"./extensions"
]
}Security note
Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.
README
๐ ๏ธ pi-json-tools
Smart Recovery Extension for Local LLMs on Pi Agent Auto-detect hallucination ยท Hallucinated path gate ยท Smart JSON recovery ยท Multi-language
[
](https://www.npmjs.com/package/pi-json-tools) [
](./LICENSE) [
](https://pi.ai) [
](https://www.typescriptlang.org/)
Why This Exists
Local LLMs like Qwen Coder, Llama, Mistral, and others are powerful but have common issues:
- Output commands as free text instead of valid JSON tool calls
- Generate hallucinations like double braces, empty code blocks, or chat template artifacts
- Forget tool-call format after a few turns
- Reference fabricated file paths that do not exist
pi-json-tools fixes all of this automatically โ no model switching required.
Features
- Hallucination Detection โ auto-detect broken output patterns
- Hallucinated Path Gate โ block commands with fabricated paths before execution
- Smart JSON Parser โ parse and recover malformed output into valid tool calls
- Auto Reminder โ remind the model to use JSON format every N turns
- Timeout Protection โ safe bash execution with configurable timeout
- Multi-language โ auto-detect Indonesian, Chinese, Japanese, Korean, Arabic, English
- Auto Retry โ retry parsing up to maxRetries times before giving up
- Auto Model Detection โ auto-switch to JSON mode if model does not support native tools
Installation
Local
npm install pi-json-tools
Global
npm install -g pi-json-tools
Register in pi agent config (~/.pi/agent/settings.json):
{
"packages": [
{
"source": "npm:pi-json-tools",
"extensions": ["+extensions/qwen-json-tools.ts"]
}
]
}
Configuration
File: ~/.pi/agent/pi-json-tools.json
{
"logging": true,
"maxRetries": 2,
"timeout": 30000,
"language": "auto",
"injectEnvContext": true,
"models": [
"qwen", "gemma", "llama", "mistral", "phi", "deepseek",
"yi", "solar", "vicuna", "orca", "wizard", "openchat",
"neural", "nous", "hermes", "zephyr", "falcon"
]
}
Options:
- logging (boolean, default: true) โ enable logging to ~/.pi/agent/pi-json-tools.log
- maxRetries (number, default: 2) โ max JSON parse retry attempts
- timeout (number, default: 30000) โ bash execution timeout in ms
- language (string, default: auto) โ auto, id, zh, en, ja, ko, ar
- injectEnvContext (boolean, default: true) โ inject real env context at session start
- models (string[]) โ list of model name keywords to intercept
How It Works
LLM Output
|
v
Smart JSON Parser --> No tool call? --> Truncation retry / Pass as chat
|
v
Hallucinated Path Gate --> Detected? --> Block + warn
|
v
Execute Tool / Bash
Supported Tools
bash ยท read ยท write ยท edit ยท grep ยท find ยท ls
Accepted JSON Formats
Standard: {"name": "bash", "arguments": {"command": "ls -la"}}
Qwen alternative: {"tool": "bash", "input": {"command": "pwd"}}
Direct command: {"command": "npm install"}
OpenAI function call: {"type": "function", "function": {"name": "bash", "arguments": {"command": "ls"}}}
Hallucination Patterns Detected
- double_brace โ double closing braces at end of output
- empty_array โ empty array with stray comma
- artifact โ Qwen chat template leaking into output
- path_error โ wrong command format
- empty_codeblock โ empty code block
Hallucinated Paths Blocked
- @mariozechner in path โ pi agent internal path fabrication
- piadmin โ non-existent admin path
- /app/config.json โ generic hallucinated config path
- /dist/cli.js โ hallucinated CLI path
- keybindings.json โ editor config hallucination
Logging
Logs saved to ~/.pi/agent/pi-json-tools.log
To disable: { "logging": false }
Multi-language Support
Language is auto-detected from user input:
- Indonesian โ keyword matching (apa, ini, tolong, coba, etc.)
- Chinese โ CJK character range
- Japanese โ Hiragana/Katakana range
- Korean โ Hangul range
- Arabic โ Arabic character range
- English โ default fallback
Pi Agent Events Used
- session_start โ reset state, show version
- agent_start โ reset retry counter
- turn_start โ reset per-turn state
- before_agent_start โ inject system prompt
- context โ inject real env context
- message_end โ parse, gate, and execute
Troubleshooting
Model keeps hallucinating? Type /reset or exit and re-enter Pi (Ctrl+D twice).
Commands not executing? Check ~/.pi/agent/pi-json-tools.log and verify JSON format.
Timeout too short? Increase timeout in config, e.g. 60000 for 60 seconds.
Wrong reminder language? Set language manually: id, en, zh, ja, ko, ar.
Model not intercepted? Add the model name keyword to the models array in config.
Project Structure
pi-json-tools/
โโโ extensions/
โ โโโ qwen-json-tools.ts
โโโ CHANGELOG.md
โโโ package.json
โโโ README.md
Contributing
- Fork this repository
- Create a branch: git checkout -b feat/feature-name
- Commit: git commit -m "feat: add feature X"
- Push: git push origin feat/feature-name
- Open a Pull Request
To add hallucination patterns, edit HALLUCINATED_PATH_PATTERNS in qwen-json-tools.ts.
License
MIT ยฉ DayuWidayadi
Made with love for the Pi Agent and local LLM community. If this extension helps you, please give it a star on GitHub!