Skip to main content

langchain-azure-dynamic-sessions

This package contains the LangChain integration for Azure Container Apps dynamic sessions. You can use it to add a secure and scalable code interpreter to your agents.

Installation

pip install -U langchain-azure-dynamic-sessions

Configuration

You first need to create an Azure Container Apps session pool and obtain its management endpoint. Learn how to configure a pool in Azure Container Apps.

By default, both tools use DefaultAzureCredential to authenticate with Azure. If you're using a user-assigned managed identity, you must set the AZURE_CLIENT_ID environment variable to the ID of the managed identity. Only Microsoft Entra ID tokens from an identity belonging to the Azure ContainerApps Session Executor role on the session pool are authorized to call the pool management API.

az role assignment create \
    --role "Azure ContainerApps Session Executor" \
    --assignee <PRINCIPAL_ID> \
    --scope <SESSION_POOL_RESOURCE_ID>

Usage

SessionsPythonREPLTool

Use the SessionsPythonREPLTool tool to give your agent the ability to execute Python code.

from langchain.agents import create_agent
from langchain_azure_dynamic_sessions.tools import SessionsPythonREPLTool


# get the management endpoint from the session pool in the Azure portal
tool = SessionsPythonREPLTool(pool_management_endpoint=POOL_MANAGEMENT_ENDPOINT)

agent = create_agent(model=llm, tools=[tool])
result = agent.invoke(
    {
        "messages": [
            {
                "role": "user",
                "content": "What is the current time in Vancouver, Canada?",
            }
        ]
    }
)

When you're done with a session, call close() (or delete_session()) to release the pool slot immediately:

tool.close()

SessionsBashTool

Use the SessionsBashTool tool to give your agent the ability to execute bash commands.

from langchain.agents import create_agent
from langchain_azure_dynamic_sessions.tools import SessionsBashTool


# get the management endpoint from the session pool in the Azure portal
tool = SessionsBashTool(pool_management_endpoint=POOL_MANAGEMENT_ENDPOINT)

agent = create_agent(model=llm, tools=[tool])
result = agent.invoke(
    {
        "messages": [
            {"role": "user", "content": "List the files in the current directory."}
        ]
    }
)

You can also execute bash commands directly:

from langchain_azure_dynamic_sessions import SessionsBashTool


tool = SessionsBashTool(pool_management_endpoint=POOL_MANAGEMENT_ENDPOINT)

# execute a bash command
result = tool.run("echo hello world")
# Returns: '{"stdout": "hello world\n", "stderr": "", "exitCode": 0}'

The tool supports file operations as well:

tool = SessionsBashTool(pool_management_endpoint=POOL_MANAGEMENT_ENDPOINT)

# upload a file to the session
tool.upload_file(
    local_file_path="./local_script.sh", remote_file_path="/mnt/user/script.sh"
)

# list files in the session
files = tool.list_files()

# download a file from the session
tool.download_file(
    remote_file_path="/mnt/user/output.txt", local_file_path="./output.txt"
)

Both tools also support context-manager usage so sessions are deleted automatically when the block exits:

with SessionsBashTool(pool_management_endpoint=POOL_MANAGEMENT_ENDPOINT) as tool:
    tool.run("echo hello world")

Changelog

  • 1.0.4:

    • We raised the minimum supported Python version from 3.10 to 3.11 in accordance with the repository's Python support policy. Users running Python 3.10 must upgrade their runtime to install this release. #1021
    • We refreshed runtime dependency pins with security and compatibility updates. #754 #776
  • 1.0.3:

    • We added the delete_session_after_invocation parameter so both session tools can automatically clean up a session after each run, and we introduced asynchronous deletion with immediate session ID rotation to make that cleanup safe under concurrent use. #715
    • We refreshed runtime security patches and dependency constraints to improve package stability. #496 #580 #690 #708
  • 1.0.2:

    • We aligned SessionsBashTool request and response handling with the Azure Container Apps shell session pool API contract. #450
    • We fixed a NameError that could occur when importing SessionsBashBackend. #473
    • We patched dependency vulnerabilities and refreshed core dependencies to improve package stability and security. #442

Release files for langchain-azure-dynamic-sessions 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-azure-dynamic-sessions 1.0.4
File Size Uploaded
langchain_azure_dynamic_sessions-1.0.4.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-azure-dynamic-sessions 1.0.4
File Interpreter ABI Platform
langchain_azure_dynamic_sessions-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 37.3 kB

Release files / langchain_azure_dynamic_sessions-1.0.4.tar.gz

Download URL langchain_azure_dynamic_sessions-1.0.4.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f7e4b480cd0c3991e7a9afc22cb8ff916e4bea8d9b299ab1e8bfaf6fd1df36eb
BLAKE2b-256 checksum
How to use checksums
7d6165c7cb9172552af65809d6b5a252ac8db02fde1f1c63ca4efa5a563bd52f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / langchain_azure_dynamic_sessions-1.0.4-py3-none-any.whl

Download URL langchain_azure_dynamic_sessions-1.0.4-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b5a01275931bd6b64e6b07a0357f3d6260442e714fc53952912e719e4d9c6d5
BLAKE2b-256 checksum
How to use checksums
e38692acfc9ba6bcf9fca1830cbab83aa4029668e29c70877e7e2634db4b71ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page