Execute Python Scripts and Capture Output
Run Python scripts and capture stdout/stderr from a LlamaIndex agent - unsandboxed, dev use only.
Why it matters
Empower your agent to execute arbitrary Python scripts and capture their standard output and error streams. This allows for dynamic code execution within your agent's workflow.
Outcomes
What it gets done
Run Python scripts provided by the agent.
Capture stdout and stderr from script execution.
Integrate code execution directly into agent workflows.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-code-interpreter | bash Overview
Code Interpreter Tool
A LlamaIndex tool that runs agent-written Python code via subprocess and captures its output, with no built-in sandboxing. Use only in development or fully sandboxed environments - not in production - due to arbitrary code execution risk.
What it does
The Code Interpreter Tool lets a LlamaIndex agent run Python scripts and capture the resulting stdout and stderr. CodeInterpreterToolSpec exposes a single function, code_interpreter, which evaluates a Python script the agent writes and returns its output.
The source carries an explicit warning: this tool gives the agent access to the subprocess.run command, meaning arbitrary code execution is possible on the machine running it. The source states plainly that it is not recommended for production use, and would require heavy sandboxing or virtual machines to be used safely.
When to use - and when NOT to
Use it only in development, testing, or fully isolated environments where you understand and accept that the agent can execute arbitrary code on the host machine - for example prototyping an agent that writes and runs small calculations or scripts. Do not use it in production, or on any machine with access to sensitive data or systems, without first adding real sandboxing or running it inside a disposable virtual machine; the tool itself provides no isolation.
Capabilities
code_interpreter evaluates a Python script the agent supplies and returns its stdout and stderr output, with no built-in execution isolation.
How to install
from llama_index.tools.code_interpreter import CodeInterpreterToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
code_spec = CodeInterpreterToolSpec()
agent = FunctionAgent(
tools=code_spec.to_tool_list(), llm=OpenAI(model="gpt-4.1")
)
Who it's for
Developers prototyping agents that need to write and run Python code, in development or fully sandboxed environments only - not for production or unisolated use given the arbitrary code execution risk.
Source README
Code Interpreter Tool
This tool can be used to run python scripts and capture the results of stdout and stderr
WARNING: This tool provides the Agent access to the subprocess.run command.
Arbitrary code execution is possible on the machine running this tool.
This tool is not recommended to be used in a production setting, and would require heavy sandboxing or virtual machines
Usage
Here's an example usage of the CodeInterpreterToolSpec.
from llama_index.tools.code_interpreter import CodeInterpreterToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
code_spec = CodeInterpreterToolSpec()
agent = FunctionAgent(
tools=code_spec.to_tool_list(), llm=OpenAI(model="gpt-4.1")
)
### Prime the agent to use the tool
resp = await agent.run(
"Can you help me write some python code to pass to the code_interpreter tool"
)
resp = await agent.run(
"write a python function to calculate volume of a sphere with radius 4.3cm"
)
The tools available are:
code_interpreter: A tool to evaluate a python script
This loader is designed to be used as a way to load data as a Tool in a Agent.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.