Skip to main content

Python interpreter tool


This tool executes code and can potentially perform destructive actions. Be careful that you trust any code passed to it!

LangChain offers an experimental tool for executing arbitrary Python code. This can be useful in combination with an LLM that can generate code to perform more powerful computations.


npm install @langchain/openai
import { OpenAI } from "@langchain/openai";
import { PythonInterpreterTool } from "langchain/experimental/tools/pyinterpreter";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";

const prompt = ChatPromptTemplate.fromTemplate(
`Generate python code that does {input}. Do not generate anything else.`

const model = new OpenAI({});

const interpreter = await PythonInterpreterTool.initialize({
indexURL: "../node_modules/pyodide",

// Note: In Deno, it may be easier to initialize the interpreter yourself:
// import pyodideModule from "npm:pyodide/pyodide.js";
// import { PythonInterpreterTool } from "npm:langchain/experimental/tools/pyinterpreter";

// const pyodide = await pyodideModule.loadPyodide();
// const pythonTool = new PythonInterpreterTool({instance: pyodide})

const chain = prompt
.pipe(new StringOutputParser())

const result = await chain.invoke({
input: `prints "Hello LangChain"`,


// To install python packages:
// This uses the loadPackages command.
// This works for packages built with pyodide.
await interpreter.addPackage("numpy");
// But for other packages, you will want to use micropip.
// See:
// for more information
await interpreter.addPackage("micropip");
// The following is roughly equivalent to:
// pyodide.runPython(`import ${pkgname}; ${pkgname}`);
const micropip = interpreter.pyodideInstance.pyimport("micropip");
await micropip.install("numpy");

API Reference:

Help us out by providing feedback on this documentation page: