Metadata-Version: 2.4
Name: langchain-azure-dynamic-sessions
Version: 1.0.2
Summary: An integration package connecting Azure Container Apps dynamic sessions and LangChain
License: MIT License
         
         Copyright (c) 2023 LangChain, Inc.
         
         Permission is hereby granted, free of charge, to any person obtaining a copy
         of this software and associated documentation files (the "Software"), to deal
         in the Software without restriction, including without limitation the rights
         to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         copies of the Software, and to permit persons to whom the Software is
         furnished to do so, subject to the following conditions:
         
         The above copyright notice and this permission notice shall be included in all
         copies or substantial portions of the Software.
         
         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
         SOFTWARE.
License-File: LICENSE
Requires-Python: >=3.10,<4.0
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Dist: azure-core (>=1.38.0)
Requires-Dist: azure-identity (>=1.16,<2.0)
Requires-Dist: langchain-core (>=1.0.1)
Requires-Dist: requests (>=2.31,<3.0)
Project-URL: Repository, https://github.com/langchain-ai/langchain-azure
Project-URL: Release Notes, https://github.com/langchain-ai/langchain-azure/releases
Project-URL: Source Code, https://github.com/langchain-ai/langchain-azure/tree/main/libs/azure-dynamic-sessions
Description-Content-Type: text/markdown

# 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

```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?"}]})
```

### 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")
```

## Changelog

- **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)


