huggingface/smolagents · error · AssertionError
{msg}
Error message
{msg} What it means
An assert statement failed and included a custom message (assert cond, msg); the interpreter evaluates msg and raises AssertionError with it. This is the agent-code's own assertion failing, not an interpreter bug.
Source
Thrown at src/smolagents/local_python_executor.py:1229
raise exc from cause
else:
raise exc
else:
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)View on GitHub (pinned to 30bb116109)
Solutions
- Read the custom message to identify which check failed and fix the upstream data/logic
- Replace assert with explicit if not cond: raise ValueError(...) for better control
- Make assertions robust to legitimate edge cases (e.g. allow empty results if valid)
Example fix
# before
assert resp['status'] == 'ok', 'bad status'
# after
if resp.get('status') != 'ok':
raise ValueError(f"unexpected status: {resp.get('status')}") Defensive patterns
Strategy: try-catch
Validate before calling
if not (resp and resp.get('status') == 'ok'):
raise ValueError(f"bad response: {resp!r}") Try / catch
try:
evaluate_python(code, ...)
except AssertionError as e:
# read the custom msg, fix upstream data/logic, retry
... Prevention
- Prefer explicit if/raise over assert for validating external data
- Include descriptive messages in every assert
When it happens
Trigger: assert 1 == 2, 'sanity check failed' or assert result is not None, 'API returned nothing' where the condition evaluates falsy.
Common situations: Agent code self-checks LLM/tool outputs and asserts non-None, correct type, or expected value; validation assertions on parsed JSON; asserting a file exists after a download step.
Related errors
- Assertion failed: {test_code}
- Check {check_function.__name__} failed with error: {e}
- {e}
- Cannot unpack non-dict value in **kwargs: {type(starred_dict
- super() needs at least one argument
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/802b521835ea2946.
Report an issue: GitHub.