huggingface/smolagents · error · AssertionError

Assertion failed: {test_code}

Error message

Assertion failed: {test_code}

What it means

An assert without a message failed; the interpreter unpar ses the failing test expression and raises AssertionError('Assertion failed: <code>'). This is the agent-written code's own assertion failing.

Source

Thrown at src/smolagents/local_python_executor.py:1233

        raise InterpreterError("Re-raise is not supported without an active exception")


def evaluate_assert(
    assert_node: ast.Assert,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
) -> None:
    test_result = evaluate_ast(assert_node.test, state, static_tools, custom_tools, authorized_imports)
    if not test_result:
        if assert_node.msg:
            msg = evaluate_ast(assert_node.msg, state, static_tools, custom_tools, authorized_imports)
            raise AssertionError(msg)
        else:
            # Include the failing condition in the assertion message
            test_code = ast.unparse(assert_node.test)
            raise AssertionError(f"Assertion failed: {test_code}")


def evaluate_with(
    with_node: ast.With,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
) -> None:
    contexts = []
    for item in with_node.items:
        context_expr = evaluate_ast(item.context_expr, state, static_tools, custom_tools, authorized_imports)
        enter_result = context_expr.__enter__()
        contexts.append(context_expr)
        if item.optional_vars:
            state[item.optional_vars.id] = enter_result

    try:

View on GitHub (pinned to 30bb116109)

Solutions

  1. Inspect the echoed test code to see exactly which condition failed and debug that value
  2. Add a descriptive message to the assert for future runs
  3. Guard instead: if not cond: raise ValueError(...) or use a default/fallback value

Example fix

# before
assert results  # no message
# after
assert results, f'expected non-empty results, got {results!r}'
Defensive patterns

Strategy: try-catch

Validate before calling

if not items:
    raise ValueError(f'expected non-empty items, got {items!r}')

Try / catch

try:
    evaluate_python(code, ...)
except AssertionError as e:
    # the echoed test code shows exactly which condition failed; fix and retry

Prevention

When it happens

Trigger: assert x, assert len(items) > 0, or assert isinstance(v, dict) where the condition evaluates False/None/empty.

Common situations: Same as any failed assert: agent self-validates intermediate results (non-empty list, expected type, truthy flag) and the assumption doesn't hold at runtime.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/6d52f977bad9b652. Report an issue: GitHub.