shell-python-fallback
它能做什么
这项技能为 AI 智能体的 Python 工作提供备用方案,适用于其代码工具返回未知错误的情况。它适合需要可靠地完成数据处理或 PDF 生成的人。
适用场景
- ✓当代码工具对同一任务两次或以上返回未知错误的时候。
- ✓当你需要用 Python 完成数据处理、文件解析或 PDF 生成的时候。
- ✓当脚本需要运行,而你希望有清晰的输出以供检查的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/shell-python-fallback/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的代码工具在这次数据清理中一直返回未知错误。请通过 shell 运行 Python,并把输出展示给我。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会用 heredoc 把 Python 脚本嵌入一条 shell 命令中。它运行脚本,读取打印的输出以判断成功或错误,并在需要时调整代码。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Shell Python Fallback
When to Use
Use this pattern when execute_code_sandbox or shell_agent tools consistently fail with "unknown error" for tasks such as:
- Data processing and transformation
- PDF generation (reportlab, etc.)
- File parsing and manipulation
- Any Python script execution that requires reliability
The Pattern
Instead of using code execution tools, embed your Python script directly in a run_shell command using a heredoc:
python3 << 'EOF'
# Your Python code here
import sys
print("Hello from embedded Python")
EOF
Step-by-Step Instructions
Identify the failure: When
execute_code_sandboxorshell_agentreturns "unknown error" repeatedly (2+ attempts), switch to this fallback.Write your Python script: Prepare the complete Python code you need to execute.
Embed in run_shell: Use
run_shellwith a heredoc syntax:
Command: python3 << 'EOF'
import json
# Your complete script here
data = {"key": "value"}
print(json.dumps(data))
EOF
Handle multi-line scripts: For longer scripts, ensure proper indentation is preserved. The heredoc preserves whitespace exactly.
Check output: Parse the stdout from
run_shellto verify success or capture errors.
Example: PDF Generation
python3 << 'EOF'
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas("output.pdf", pagesize=letter)
c.drawString(100, 750, "Hello World")
c.save()
print("PDF created successfully")
EOF
Example: Data Processing
python3 << 'EOF'
import csv
import json
data = []
with open('input.csv', 'r') as f:
reader = csv.DictReader(f)
for row in reader:
data.append(row)
with open('output.json', 'w') as f:
json.dump(data, f, indent=2)
print(f"Processed {len(data)} records")
EOF
Best Practices
- Quote the EOF delimiter (
<< 'EOF'not<< EOF) to prevent shell variable expansion in your Python code - Include error handling in your Python script with try/except blocks
- Print status messages to stdout so you can verify execution succeeded
- Use absolute paths or ensure working directory is correct
- Test incrementally: Start with a minimal script, then expand
Troubleshooting
| Issue | Solution |
|---|---|
| Script doesn't run | Ensure python3 is available in the shell environment |
| Indentation errors | Check that heredoc preserves spaces (use spaces, not tabs) |
| Module not found | Use python3 -m pip install <module> before running script |
| File not found | Use absolute paths or pwd to verify working directory |
Why This Works
The run_shell tool executes commands directly in the system shell, bypassing the sandbox restrictions or internal errors that affect execute_code_sandbox and shell_agent. The heredoc approach allows multi-line Python scripts while keeping everything in a single shell command.
常见问题
失败多少次会触发切换?
这项技能要求在两次或以上未知错误后切换。
heredoc 会保留缩进吗?
这项技能说明 heredoc 会完整保留空白,因此多行代码保持完整。
它能制作 PDF 吗?
可以。这项技能给出了一个使用 reportlab 制作 PDF 的示例。
详情
作者的原始描述
Use run_shell with embedded Python heredoc as reliable fallback when code execution tools fail
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #13001
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。