huggingface/smolagents · error · InterpreterError
Code execution failed at line '{ast.get_source_segment(code,
Error message
Code execution failed at line '{ast.get_source_segment(code, node)}' due to: {type(e).__name__}: {e} What it means
This is the executor's generic runtime-failure wrapper: any exception raised while executing a node that is not FinalAnswerException is re-raised as InterpreterError with the source segment of the failing line and the original exception type and message. The original exception is not chained by `from e`, so the message text is the primary diagnostic. It marks where in the agent's code action the failure occurred.
Source
Thrown at src/smolagents/local_python_executor.py:1659
try:
for node in expression.body:
result = evaluate_ast(node, state, static_tools, custom_tools, authorized_imports)
state["_print_outputs"].value = truncate_content(
str(state["_print_outputs"]), max_length=max_print_outputs_length
)
is_final_answer = False
return result, is_final_answer
except FinalAnswerException as e:
state["_print_outputs"].value = truncate_content(
str(state["_print_outputs"]), max_length=max_print_outputs_length
)
is_final_answer = True
return e.value, is_final_answer
except Exception as e:
state["_print_outputs"].value = truncate_content(
str(state["_print_outputs"]), max_length=max_print_outputs_length
)
raise InterpreterError(
f"Code execution failed at line '{ast.get_source_segment(code, node)}' due to: {type(e).__name__}: {e}"
)
# Apply timeout if specified
if timeout_seconds is not None:
_execute_code = timeout(timeout_seconds)(_execute_code)
return _execute_code()
@dataclass
class CodeOutput:
output: Any
logs: str
is_final_answer: bool
class PythonExecutor(ABC):View on GitHub (pinned to 30bb116109)
Solutions
- Read the inner `TypeName: message` to identify the real cause and fix the offending line quoted in the error
- For agent workflows, pass this error message back to the LLM for self-correction (step_number/output trimming is standard)
- Add defensive checks (None guards, isinstance checks) around tool outputs used in subsequent code
- If the inner error is itself InterpreterError about unsupported syntax, address that root cause instead
Example fix
# before
code = "result = alphabet_split('hello')[0]" # tool returns str, not list
# after
code = "res = alphabet_split('hello')\nresult = res[0] if isinstance(res, list) else res" Defensive patterns
Strategy: fallback
Try / catch
from smolagents.local_python_executor import InterpreterError
try:
output, is_final = evaluate_python_code(code, state=state)
except InterpreterError as e:
# parse 'due to: <Type>: <msg>' from the message
inner = str(e).split('due to: ', 1)[-1]
if inner.startswith(('NameError', 'TypeError')):
code = fix_with_llm(code, str(e)) # self-correction loop Prevention
- Include the error message in the next LLM prompt for self-correction
- Guard tool outputs (None checks, isinstance) before use in generated code
- Verify tool names/arguments exist before running the full action
When it happens
Trigger: Any runtime error inside executed code: NameError for undefined variables, TypeError on bad tool arguments, ZeroDivisionError, KeyError on subscripting, errors raised by called tools, or nested InterpreterErrors from unsupported nodes.
Common situations: LLM hallucinating variable or tool names; wrong argument types passed to tools; iterating over None returned by a previous step; errors inside imported authorized modules.
Related errors
- Error executing tool '{tool_name}' with arguments {str(argum
- Code parsing failed on line {e.lineno} due to: {type(e).__na
- Tool call needs to have a key '{tool_name_key}'. Got keys: {
- Error during jinja template rendering: {type(e).__name__}: {
- Cannot specify both 'messages' and 'steps' parameters. Use '
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/3df09408e258cf92.
Report an issue: GitHub.