Metadata-Version: 2.4
Name: langchain-azure-dynamic-sessions
Version: 1.0.4
Summary: Deprecated: use langchain-azure-compute. Connects Azure Container Apps dynamic sessions and LangChain
License-Expression: MIT
License-File: LICENSE
Requires-Dist: azure-core>=1.38.0
Requires-Dist: azure-identity~=1.16
Requires-Dist: langchain-core>=1.0.1
Requires-Dist: requests~=2.31
Requires-Python: >=3.11, <4.0
Project-URL: repository, https://github.com/langchain-ai/langchain-azure
Project-URL: Source Code, https://github.com/langchain-ai/langchain-azure/tree/main/libs/azure-dynamic-sessions
Project-URL: Release Notes, https://github.com/langchain-ai/langchain-azure/releases
Description-Content-Type: text/markdown

# langchain-azure-dynamic-sessions

> [!WARNING]
> **This package is deprecated and will not receive further fixes.** It has
> been superseded by
> [`langchain-azure-compute`](https://github.com/langchain-ai/langchain-azure/tree/main/libs/azure-compute),
> which carries the dynamic sessions integrations forward under the wider
> Azure Container Apps umbrella — and additionally covers Azure Container Apps
> sandboxes, the stateful counterpart to dynamic sessions.
>
> ```bash
> pip install "langchain-azure-compute[dynamic-sessions]"
> ```
>
> Then update imports:
>
> ```diff
> - from langchain_azure_dynamic_sessions import SessionsPythonREPLTool
> + from langchain_azure_compute.dynamic_sessions import SessionsPythonREPLTool
> - from langchain_azure_dynamic_sessions import SessionsBashTool
> + from langchain_azure_compute.dynamic_sessions import SessionsBashTool
> - from langchain_azure_dynamic_sessions.backends import SessionsBashBackend
> + from langchain_azure_compute.dynamic_sessions.backends import SessionsBashBackend
> ```
>
> Migrating also fixes `SessionsBashBackend` on **deepagents >= 0.7**, where it
> is broken here: its file-operation overrides are silently ignored and
> `read()` returns the wrong type. Neither failure raises, so an affected agent
> degrades quietly rather than erroring.

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

```bash
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](https://learn.microsoft.com/azure/container-apps/session-pool#configure-a-pool).

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.

```azurecli
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.

```python
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:

```python
tool.close()
```

### SessionsBashTool

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

```python
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:

```python
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:

```python
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:

```python
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](https://github.com/langchain-ai/langchain-azure/pull/1021)
    - We refreshed runtime dependency pins with security and compatibility updates. [#754](https://github.com/langchain-ai/langchain-azure/pull/754) [#776](https://github.com/langchain-ai/langchain-azure/pull/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](https://github.com/langchain-ai/langchain-azure/pull/715)
    - We refreshed runtime security patches and dependency constraints to improve package stability. [#496](https://github.com/langchain-ai/langchain-azure/pull/496) [#580](https://github.com/langchain-ai/langchain-azure/pull/580) [#690](https://github.com/langchain-ai/langchain-azure/pull/690) [#708](https://github.com/langchain-ai/langchain-azure/pull/708)

- **1.0.2**:

    - We aligned `SessionsBashTool` request and response handling with the Azure Container Apps shell session pool API contract. [#450](https://github.com/langchain-ai/langchain-azure/pull/450)
    - We fixed a `NameError` that could occur when importing `SessionsBashBackend`. [#473](https://github.com/langchain-ai/langchain-azure/pull/473)
    - We patched dependency vulnerabilities and refreshed core dependencies to improve package stability and security. [#442](https://github.com/langchain-ai/langchain-azure/pull/442)
