in src/smolagents/remote_executors.py [0:0]
def run_code_raise_errors(self, code: str) -> CodeOutput:
execution = self.sandbox.run_code(code)
execution_logs = "\n".join([str(log) for log in execution.logs.stdout])
# Handle errors
if execution.error:
# Check if the error is a FinalAnswerException
if execution.error.name == RemotePythonExecutor.FINAL_ANSWER_EXCEPTION:
final_answer = pickle.loads(base64.b64decode(execution.error.value))
return CodeOutput(output=final_answer, logs=execution_logs, is_final_answer=True)
# Construct error message
error_message = (
f"{execution_logs}\n"
f"Executing code yielded an error:\n"
f"{execution.error.name}\n"
f"{execution.error.value}\n"
f"{execution.error.traceback}"
)
raise AgentError(error_message, self.logger)
# Handle results
if not execution.results:
return CodeOutput(output=None, logs=execution_logs, is_final_answer=False)
for result in execution.results:
if not result.is_main_result:
continue
# Handle image outputs
for attribute_name in ["jpeg", "png"]:
img_data = getattr(result, attribute_name, None)
if img_data is not None:
decoded_bytes = base64.b64decode(img_data.encode("utf-8"))
return CodeOutput(
output=PIL.Image.open(BytesIO(decoded_bytes)), logs=execution_logs, is_final_answer=False
)
# Handle other data formats
for attribute_name in [
"chart",
"data",
"html",
"javascript",
"json",
"latex",
"markdown",
"pdf",
"svg",
"text",
]:
data = getattr(result, attribute_name, None)
if data is not None:
return CodeOutput(output=data, logs=execution_logs, is_final_answer=False)
# If no main result found, return None
return CodeOutput(output=None, logs=execution_logs, is_final_answer=False)