1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
10 small wins to finish your pathNext lesson →
Errors: reported, not raised
An exception in the sandbox's code does not raise in your program. It arrives as an error message and becomes execution.error, which your code must check.
import json
from e2b_code_interpreter.models import Execution, parse_output
execution = Execution()
for message in [
{"type": "stdout", "text": "starting\n", "timestamp": 1},
{"type": "error", "name": "ZeroDivisionError", "value": "division by zero",
"traceback": "Traceback (most recent call last):\n Cell In[1], line 2\nZeroDivisionError: division by zero"},
]:
parse_output(execution, json.dumps(message))print(execution.error.name, "|", execution.error.value)
print(execution.error.traceback.splitlines()[-1])
print(execution.logs.stdout)
print(execution.text)The cell printed starting, then failed. execution.error has the exception's name, value and the traceback as text; there is no main result. run_code returned normally: the SDK's parser records an error message and moves on.
What does raise
| Situation | In your program |
|---|---|
| The code raises inside the sandbox | No exception; execution.error is set |
The code runs longer than timeout | TimeoutException, from the SDK's httpx.ReadTimeout handling |
| The sandbox was killed or expired | An API error from the HTTP response |
| No key, or an invalid key | AuthenticationException |
For an agent this split is right: a model should read a KeyError and fix its code, but your program should stop on a sandbox that no longer exists. timeout=0 in run_code removes the time limit, which an agent should never do.
Try it yourself
- Print
execution.error.to_json(). - Find
format_execution_timeout_errorin the SDK and read its message. - Write an
ifthat returns the error text when there is one and the result otherwise.
This is what real progress feels like.