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:
Method | Description |
---|---|
__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:
Adding Files
You can add files that will be available in the sandbox environment:
Creating a Tool Object
After configuring files, create a LangChain Tool object:
Using with an Agent
The tool works great with LangChain agents:
Working with Code Output
The tool supports various output types:
File Management
You can clear files when needed:
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
- Always verify API key is set before using the tool
- Add descriptive file descriptions when using
add_file()
- Use the sandbox environment responsibly - avoid heavy computations
- Clear files when they're no longer needed
- 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 DocsJoin 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.