Skip to main content
Azure Container Apps dynamic sessions provides a secure and scalable way to run a Python code interpreter in Hyper-V isolated sandboxes. This allows your agents to run potentially untrusted code in a secure environment. The code interpreter environment includes many popular Python packages, such as NumPy, pandas, and scikit-learn. See the Azure Container App docs for more info on how sessions work.

Setup

By default, the SessionsPythonREPLTool tool uses DefaultAzureCredential to authenticate with Azure. Locally, it’ll use your credentials from the Azure CLI or VS Code. Install the Azure CLI and log in with az login to authenticate. To use the code interpreter you’ll also need to create a session pool, which you can do by following the session pool creation instructions. Once that’s done you should have a pool management session endpoint, which you’ll need to set below:
You’ll also need to install the langchain-azure-dynamic-sessions package:

Use tool

Instantiate and use tool:
Invoking the tool will return a json string with the result of the code, along with any stdout and stderr outputs. To get the raw dictionary results, use the execute() method:

Upload data

If we want to perform computation over specific data, we can use the upload_file() functionality to upload data to our session. You can upload data either via the data: BinaryIO arg or via the local_file_path: str arg (which points to a local file on your system). The data is automatically uploaded to the “/mnt/data/” directory in the sessions container. You can get the full file path via the upload metadata returned by upload_file().

Handling image results

Dynamic sessions results can include image outputs as base64 encoded strings. In these cases the value of ‘result’ will be a dictionary with keys “type” (which will be “image”), “format (the format of the image), and “base64_data”.
We can decode the image data and display it:

Simple agent example

LangGraph data analyst agent

For a more complex agent example check out the LangGraph data analyst example