LangChain and LangGraph

A Sandal sandbox as a LangChain tool, for LangChain agents and LangGraph graphs (Python).


A LangChain @tool that runs code in one Sandal sandbox. Bind it to any chat model or pass it to a LangGraph agent.

Install the Sandal SDK from your beta bundle and sign in once with sandal login (the SDK reads that profile), or set SANDAL_API_KEY (the key alone is enough).

pip install langchain-core langgraph   # plus the Sandal SDK (see above)
sandal_tool.py
from langchain_core.tools import tool
from sandal import SandalSync

sandal = SandalSync()  # the `sandal login` profile, or the SANDAL_* variables
sb = sandal.create_sandbox()


@tool
def run_python(code: str) -> str:
    """Run Python 3 code in an isolated Sandal sandbox and return its exit code and output."""
    sb.write_text("/workspace/main.py", code)
    result = sb.exec("python3 /workspace/main.py")
    return f"exit {result.exit_code}\n{result.stdout}{result.stderr}"


# Bind it to any chat model, e.g. with LangGraph:
#   from langgraph.prebuilt import create_react_agent
#   agent = create_react_agent(model, tools=[run_python])
#   agent.invoke({"messages": [("user", "What is the 30th Fibonacci number? Compute it.")]})
print(run_python.invoke({"code": "print(sum(range(10)))"}))

sb.delete()
sandal.close()

Verified against Sandal CLI/SDK 0.1.0-beta.10 on 2026-11-25: the tool was called directly and ran print(sum(range(10))) in a real sandbox (exit 0, 45). The model call itself needs your own model provider key. Tested with langchain-core 1.8.5.