跳到正文
Skillpedia
通用编程7.8k · #13001

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/shell-python-fallback/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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

  1. Identify the failure: When execute_code_sandbox or shell_agent returns "unknown error" repeatedly (2+ attempts), switch to this fallback.

  2. Write your Python script: Prepare the complete Python code you need to execute.

  3. Embed in run_shell: Use run_shell with a heredoc syntax:

Command: python3 << 'EOF'
import json
# Your complete script here
data = {"key": "value"}
print(json.dumps(data))
EOF
  1. Handle multi-line scripts: For longer scripts, ensure proper indentation is preserved. The heredoc preserves whitespace exactly.

  2. Check output: Parse the stdout from run_shell to 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能