AliHaider1's picture
Upload folder using huggingface_hub
aed39aa verified
Raw History Blame Contribute Delete
530 Bytes
import io
import contextlib
from langchain_core.tools import tool
@tool
def python(code: str) -> str:
"""Execute Python code and return anything printed to stdout. Use print() to see results."""
output = io.StringIO()
try:
with contextlib.redirect_stdout(output):
exec(code, {})
result = output.getvalue().strip()
return result if result else "Code executed successfully (no output printed)."
except Exception as e:
return f"Python execution error: {e}"