Fallback workflow for reliable code execution when sandbox fails repeatedly
Apply this pattern when you encounter repeated failures with execute_code_sandbox:
Track execution failures. After 2 consecutive failures with execute_code_sandbox, switch to the fallback approach.
Use write_file to save your Python script:
write_file(
path="/workspace/script_name.py",
content="# Your Python code here\nimport sys\n..."
)
Use run_shell to run the script:
run_shell(
command="python /workspace/script_name.py",
timeout=300
)
Parse stdout/stderr from run_shell output to verify success or diagnose issues.
# Instead of this (which may fail):
result = execute_code_sandbox(code="import pandas as pd\n...")
# Use this fallback pattern:
script_content = """
import pandas as pd
import sys