pdf-gen-fallback-501bcc
它能做什么
这项技能是代码沙箱以不明确的信息失败时,文档制作的备用方案。它适合即使沙箱不可靠,也需要生成 PDF 或 Word 文件的人。
适用场景
- ✓当我在制作文档时,代码沙箱返回不明确的错误的时候。
- ✓当沙箱中似乎缺少某个必需的 Python 库的时候。
- ✓当我需要在这台电脑上创建 PDF 或 Word 文件的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-gen-fallback-501bcc/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我在制作 PDF 时,沙箱一直返回不明确的错误。请检查所需的库,并在 shell 中生成文件。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会检查 reportlab、fpdf2、pypdf 或 python-docx 是否已安装,用 pip 安装缺少的库,然后在 shell 中运行 python 命令生成文档。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PDF Generation Fallback Pattern
When to Use
Apply this pattern when execute_code_sandbox returns opaque or unhelpful errors during PDF, DOCX, or other document generation tasks. This fallback uses run_shell with direct python -c execution for more reliable document generation.
Step-by-Step Procedure
1. Detect Sandbox Failure
If execute_code_sandbox fails during document generation with errors like:
- Opaque runtime errors
- Missing library errors that don't resolve with import statements
- Silent failures with no output
Proceed to the fallback approach.
2. Check Library Availability
Before generating documents, verify the required Python libraries are available in the shell environment:
python -c "import reportlab; print('OK')" 2>&1 || echo "reportlab missing"
python -c "import fpdf; print('OK')" 2>&1 || echo "fpdf missing"
python -c "import pypdf; print('OK')" 2>&1 || echo "pypdf missing"
python -c "import docx; print('OK')" 2>&1 || echo "python-docx missing"
3. Install Missing Libraries (If Needed)
If a library is missing, install it via pip:
pip install reportlab fpdf2 pypdf python-docx
4. Execute Document Generation via run_shell
Use run_shell with python -c and multi-line strings for document generation:
python -c "
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas('output.pdf', pagesize=letter)
c.drawString(100, 750, 'Document Title')
c.drawString(100, 700, 'Content here')
c.save()
print('SUCCESS: PDF created')
"
For DOCX files:
python -c "
from docx import Document
doc = Document()
doc.add_heading('Document Title', 0)
doc.add_paragraph('Content here')
doc.save('output.docx')
print('SUCCESS: DOCX created')
"
5. Include Explicit Error Handling
Always wrap generation code with try/except for clear error reporting:
python -c "
try:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas('output.pdf', pagesize=letter)
c.drawString(100, 750, 'Content')
c.save()
print('SUCCESS')
except ImportError as e:
print(f'MISSING_LIBRARY: {e}')
exit(1)
except Exception as e:
print(f'GENERATION_ERROR: {e}')
exit(1)
"
6. Verify Output File
After generation, confirm the file was created and is valid:
ls -la output.pdf
file output.pdf
test -f output.pdf && echo "File exists" || echo "File missing"
Complete Example Workflow
# Step 1: Check and install library
python -c "import reportlab" 2>&1 || pip install reportlab
# Step 2: Generate PDF with error handling
python -c "
try:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas('report.pdf', pagesize=letter)
c.setFont('Helvetica', 12)
c.drawString(100, 750, 'Surveillance Report')
c.drawString(100, 700, 'Generated via fallback pattern')
c.save()
print('SUCCESS: report.pdf created')
except Exception as e:
print(f'ERROR: {e}')
exit(1)
"
# Step 3: Verify
test -f report.pdf && echo "Verification: PASSED" || echo "Verification: FAILED"
Why This Works
- Direct Execution:
run_shellwithpython -cbypasses sandbox limitations that may affect document generation libraries - Explicit Errors: Try/except blocks provide actionable error messages instead of opaque failures
- Environment Control: Shell execution gives more control over the Python environment and library paths
- Verification: Explicit file checks confirm success rather than assuming it
Common Libraries
| Library | Purpose | Import Statement |
|---|---|---|
| reportlab | PDF generation | from reportlab.pdfgen import canvas |
| fpdf2 | PDF generation | from fpdf import FPDF |
| pypdf | PDF manipulation | from pypdf import PdfReader |
| python-docx | DOCX generation | from docx import Document |
| xlsxwriter | Excel files | import xlsxwriter |
常见问题
检查哪些库?
指南检查 reportlab、fpdf2、pypdf 和 python-docx。
缺少的库如何添加?
指南会先用 pip 安装,再生成文档。
这也适用于 Word 文件吗?
适用。python-docx 是指南中用于 Word 文档的库之一。
详情
作者的原始描述
Fallback pattern for PDF/document generation when execute_code_sandbox fails with opaque errors
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12926
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。