Using the Bearly Code Interpreter Tool in LangChain

Posted: Feb 4, 2025.

The BearlyInterpreterTool is a powerful utility in LangChain that allows you to execute Python code in a sandboxed environment. This makes it ideal for running code safely within AI agents and automation workflows.

What is BearlyInterpreterTool?

BearlyInterpreterTool is a class that provides a secure way to evaluate Python code in an isolated environment. It supports:

  • Execution of arbitrary Python code
  • File handling capabilities
  • Access to common Python packages like pandas, numpy, matplotlib
  • Secure sandbox environment that resets between executions
  • File output handling

Reference

Here are the key methods available in BearlyInterpreterTool:

MethodDescription
__init__(api_key)Initialize with Bearly API key
add_file(source_path, target_path, description)Add a file to be available in the sandbox
clear_files()Remove all files from the sandbox
as_tool()Convert to a LangChain Tool object
make_input_files()Generate file input configuration

How to Use BearlyInterpreterTool

Basic Setup

First, initialize the tool with your Bearly API key:

from langchain_community.tools import BearlyInterpreterTool

bearly_tool = BearlyInterpreterTool(api_key="your_api_key")

Adding Files

You can add files that will be available in the sandbox environment:

bearly_tool.add_file(
    source_path="data/input.csv",
    target_path="input.csv", 
    description="Input data file"
)

Creating a Tool Object

After configuring files, create a LangChain Tool object:

tool = bearly_tool.as_tool()

Using with an Agent

The tool works great with LangChain agents:

from langchain.agents import AgentType, initialize_agent
from langchain_openai import ChatOpenAI

# Create agent
llm = ChatOpenAI(model="gpt-4", temperature=0)
agent = initialize_agent(
    [bearly_tool.as_tool()],
    llm,
    agent=AgentType.OPENAI_FUNCTIONS,
    verbose=True
)

# Run tasks
agent.run("Analyze the data in input.csv")

Working with Code Output

The tool supports various output types:

# Generate plots
agent.run("""
Create a chart and save it to output/chart.png:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('output/chart.png')
""")

# Print results
agent.run("""
Print calculation results:

x = 5 + 5
print(f"Result: {x}")
""")

File Management

You can clear files when needed:

bearly_tool.clear_files()

Remember that the sandbox environment resets between executions, so you need to handle any state or file operations within a single execution context.

Best Practices

  1. Always verify API key is set before using the tool
  2. Add descriptive file descriptions when using add_file()
  3. Use the sandbox environment responsibly - avoid heavy computations
  4. Clear files when they're no longer needed
  5. Handle execution errors appropriately in your agent implementation

The BearlyInterpreterTool provides a secure and flexible way to execute Python code within LangChain applications while maintaining isolation and safety.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs