{"record":{"id":"876f4c6dd09a12d9","repo":"huggingface/smolagents","slug":"code-execution-exceeded-the-maximum-execution-time","errorCode":null,"errorMessage":"Code execution exceeded the maximum execution time of {timeout_seconds} seconds","messagePattern":"Code execution exceeded the maximum execution time of (.+?) seconds","errorType":"error_code","errorClass":"ExecutionTimeoutError","httpStatus":null,"severity":"error","filePath":"src/smolagents/local_python_executor.py","lineNumber":314,"sourceCode":"        ExecutionTimeoutError: If the function execution exceeds the timeout period.\n\n    Note:\n        If a timeout occurs, the thread running the function cannot be forcefully killed\n        in Python, so it will continue running in the background until completion. However,\n        the caller will receive a TimeoutError and can continue execution.\n    \"\"\"\n\n    def decorator(func):\n        @wraps(func)\n        def wrapper(*args, **kwargs):\n            # Create a new ThreadPoolExecutor for each call to avoid threading issues\n            with ThreadPoolExecutor(max_workers=1) as executor:\n                future = executor.submit(func, *args, **kwargs)\n                try:\n                    result = future.result(timeout=timeout_seconds)\n                    return result\n                except FuturesTimeoutError:\n                    raise ExecutionTimeoutError(\n                        f\"Code execution exceeded the maximum execution time of {timeout_seconds} seconds\"\n                    )\n\n        return wrapper\n\n    return decorator\n\n\ndef get_iterable(obj):\n    if isinstance(obj, list):\n        return obj\n    elif hasattr(obj, \"__iter__\"):\n        return list(obj)\n    else:\n        raise InterpreterError(\"Object is not iterable\")\n\n\ndef fix_final_answer_code(code: str) -> str:","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L296-L332","documentation":"smolagents wraps script execution in a single-worker ThreadPoolExecutor with a per-call timeout; if future.result(timeout=...) times out, it converts the timeout into an ExecutionTimeoutError. The background thread keeps running (threads cannot be killed in Python), but the caller gets this error so runaway code cannot hang the agent.","triggerScenarios":"evaluate_python(code, timeout_seconds=N) or a CodeAgent run where the generated code enters an infinite/very long loop (e.g. while True, huge pandas merge, long training loop) exceeding the configured timeout (default max_print_outputs_length-adjacent run settings; commonly N seconds set in run_kwargs).","commonSituations":"Default timeout too low for heavy compute (model inference, big dataframes); generated code has an unbounded while loop; network call without its own timeout; slow first import of large libraries counting against the budget.","solutions":["Increase or set the timeout: pass run_kwargs={'timeout_seconds': ...} / configure executor timeout appropriately","Fix the generated code: bound loops, add early exits, reduce data size, or move heavy compute to a separate job","Pre-test the code snippet standalone to estimate runtime before handing it to the agent"],"exampleFix":"# before\nresult = agent.run(task, extra_variables={...})  # default timeout\n\n# after\nresult = agent.run(\n    task,\n    additional_args={'timeout_seconds': 120},\n)","handlingStrategy":"retry","validationCode":"# dry-run estimate: parse for unbounded loops before executing\nimport ast\ntree = ast.parse(code)\nunbounded = [n for n in ast.walk(tree) if isinstance(n, ast.While) and not n.orelse and all(not isinstance(x, ast.Break) for x in ast.walk(n))]\nif unbounded:\n    code = 'max_iter = 10000\\n' + code","typeGuard":null,"tryCatchPattern":"from smolagents.local_python_executor import ExecutionTimeoutError\ntry:\n    result = evaluate_python(code, timeout_seconds=60)\nexcept ExecutionTimeoutError:\n    result = fallback_stub_answer()  # note: the thread keeps running in background","preventionTips":["Set a realistic timeout_seconds based on a standalone dry-run","Bound every while loop in generated code","Keep heavy compute out of the sandbox (precompute and pass results in as variables)"],"tags":["smolagents","timeout","execution-timeout","sandbox","infinite-loop"],"backgroundTag":"code-execution-timeout","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}