diff --git a/docs/core_docs/docs/integrations/platforms/microsoft.mdx b/docs/core_docs/docs/integrations/platforms/microsoft.mdx
index 02e32eaa4eee..d3209ccda6ac 100644
--- a/docs/core_docs/docs/integrations/platforms/microsoft.mdx
+++ b/docs/core_docs/docs/integrations/platforms/microsoft.mdx
@@ -152,3 +152,21 @@ See a [usage example for the Azure Files](/docs/integrations/document_loaders/we
```typescript
import { AzureBlobStorageFileLoader } from "@langchain/community/document_loaders/web/azure_blob_storage_file";
```
+
+## Tools
+
+### Azure Container Apps Dynamic Sessions
+
+> [Azure Container Apps dynamic sessions](https://learn.microsoft.com/azure/container-apps/sessions) provide fast access to secure sandboxed environments that are ideal for running code or applications that require strong isolation from other workloads.
+
+
+
+```bash npm2yarn
+npm install @langchain/azure-dynamic-sessions
+```
+
+See a [usage example](/docs/integrations/tools/azure_dynamic_sessions).
+
+```typescript
+import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions";
+```
diff --git a/docs/core_docs/docs/integrations/tools/azure_dynamic_sessions.mdx b/docs/core_docs/docs/integrations/tools/azure_dynamic_sessions.mdx
new file mode 100644
index 000000000000..89aad3b76a91
--- /dev/null
+++ b/docs/core_docs/docs/integrations/tools/azure_dynamic_sessions.mdx
@@ -0,0 +1,42 @@
+# Azure Container Apps Dynamic Sessions
+
+> [Azure Container Apps dynamic sessions](https://learn.microsoft.com/azure/container-apps/sessions) provide fast access to secure sandboxed environments that are ideal for running code or applications that require strong isolation from other workloads.
+
+You can learn more about Azure Container Apps dynamic sessions and its code interpretation capabilities on [this page](https://learn.microsoft.com/azure/container-apps/sessions). If you don't have an Azure account, you can [create a free account](https://azure.microsoft.com/free/) to get started.
+
+## Setup
+
+You'll first need to install the [`@langchain/azure-dynamic-sessions`](https://www.npmjs.com/package/@langchain/azure-dynamic-sessions) package:
+
+import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
+
+
+
+```bash npm2yarn
+npm install @langchain/azure-dynamic-sessions
+```
+
+You'll also need to have a code interpreter session pool instance running. You can deploy a version using [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli) following [this guide](https://learn.microsoft.com/azure/container-apps/sessions-code-interpreter).
+
+Once you have your instance running, you need to make sure you have properly set up the Azure Entra authentication for it. You can find the instructions on how to do that [here](https://learn.microsoft.com/azure/container-apps/sessions?tabs=azure-cli#authentication).
+
+After you've added the role for your identity, you need to retrieve the **session pool management endpoint**. You can find it in the Azure Portal, under the "Overview" section of your instance. Then you need to set the following environment variable:
+
+import CodeBlock from "@theme/CodeBlock";
+import EnvVars from "@examples/tools/azure_dynamic_sessions/.env.example";
+
+{EnvVars}
+
+## Usage example
+
+Below is a simple example that creates a new Python code interpreter session, invoke the tool and prints the result.
+
+import Example from "@examples/tools/azure_dynamic_sessions/azure_dynamic_sessions.ts";
+
+{Example}
+
+Here is a complete example where we use an Azure OpenAI chat model to call the Python code interpreter session tool to execute the code and get the result:
+
+import AgentExample from "@examples/tools/azure_dynamic_sessions/azure_dynamic_sessions-agent.ts";
+
+{AgentExample}
diff --git a/examples/.env.example b/examples/.env.example
index 9b292352acf6..2abb8d8e6912 100644
--- a/examples/.env.example
+++ b/examples/.env.example
@@ -15,6 +15,8 @@ AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME=ADD_YOURS_HERE # Azure Portal -> Co
AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME=ADD_YOURS_HERE # Azure Portal -> Cognitive Services -> OpenAI -> Choose your instance -> Go to Azure OpenAI Studio -> Deployments
AZURE_OPENAI_API_VERSION=ADD_YOURS_HERE # Azure Portal -> Cognitive Services -> OpenAI -> Choose your instance -> Go to Azure OpenAI Studio -> Completions/Chat -> Choose Deployment -> View Code
AZURE_OPENAI_BASE_PATH=ADD_YOURS_HERE # Azure Portal -> Cognitive Services -> OpenAI -> Choose your instance -> Endpoint (optional)
+AZURE_OPENAI_BASE_PATH=ADD_YOURS_HERE # Azure Portal -> Cognitive Services -> OpenAI -> Choose your instance -> Endpoint (optional)
+AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=ADD_YOURS_HERE # Azure Portal -> Container App Session Pools -> Choose your app -> Pool management endpoint -> Copy the URL
CONNERY_RUNNER_URL=ADD_YOURS_HERE
CONNERY_RUNNER_API_KEY=ADD_YOURS_HERE
ELASTIC_URL=ADD_YOURS_HERE # http://127.0.0.1:9200
diff --git a/examples/package.json b/examples/package.json
index 78650c720e21..4388cc94f682 100644
--- a/examples/package.json
+++ b/examples/package.json
@@ -34,6 +34,7 @@
"@gomomento/sdk": "^1.51.1",
"@google/generative-ai": "^0.7.0",
"@langchain/anthropic": "workspace:*",
+ "@langchain/azure-dynamic-sessions": "workspace:^",
"@langchain/azure-openai": "workspace:*",
"@langchain/cloudflare": "workspace:*",
"@langchain/cohere": "workspace:*",
diff --git a/examples/src/tools/azure_dynamic_sessions/.env.example b/examples/src/tools/azure_dynamic_sessions/.env.example
new file mode 100644
index 000000000000..452e9d9a82c5
--- /dev/null
+++ b/examples/src/tools/azure_dynamic_sessions/.env.example
@@ -0,0 +1 @@
+AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT=
diff --git a/examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions-agent.ts b/examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions-agent.ts
new file mode 100644
index 000000000000..c80b4a002703
--- /dev/null
+++ b/examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions-agent.ts
@@ -0,0 +1,43 @@
+import type { ChatPromptTemplate } from "@langchain/core/prompts";
+import { pull } from "langchain/hub";
+import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
+import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions";
+import { AzureChatOpenAI } from "@langchain/openai";
+
+const tools = [
+ new SessionsPythonREPLTool({
+ poolManagementEndpoint:
+ process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "",
+ }),
+];
+
+// Note: you need a model deployment that supports function calling,
+// like `gpt-35-turbo` version `1106`.
+const llm = new AzureChatOpenAI({
+ temperature: 0,
+});
+
+// Get the prompt to use - you can modify this!
+// If you want to see the prompt in full, you can at:
+// https://smith.langchain.com/hub/hwchase17/openai-functions-agent
+const prompt = await pull(
+ "hwchase17/openai-functions-agent"
+);
+
+const agent = await createOpenAIFunctionsAgent({
+ llm,
+ tools,
+ prompt,
+});
+
+const agentExecutor = new AgentExecutor({
+ agent,
+ tools,
+});
+
+const result = await agentExecutor.invoke({
+ input:
+ "Create a Python program that prints the Python version and return the result.",
+});
+
+console.log(result);
diff --git a/examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions.ts b/examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions.ts
new file mode 100644
index 000000000000..c70d1a246702
--- /dev/null
+++ b/examples/src/tools/azure_dynamic_sessions/azure_dynamic_sessions.ts
@@ -0,0 +1,16 @@
+import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions";
+
+const tool = new SessionsPythonREPLTool({
+ poolManagementEndpoint:
+ process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "",
+});
+
+const result = await tool.invoke("print('Hello, World!')\n1+2");
+
+console.log(result);
+
+// {
+// stdout: "Hello, World!\n",
+// stderr: "",
+// result: 3,
+// }
diff --git a/libs/langchain-azure-dynamic-sessions/README.md b/libs/langchain-azure-dynamic-sessions/README.md
new file mode 100644
index 000000000000..2c0c4930f9da
--- /dev/null
+++ b/libs/langchain-azure-dynamic-sessions/README.md
@@ -0,0 +1,60 @@
+# @langchain/azure-dynamic-sessions
+
+This package contains the [Azure Container Apps dynamic sessions](https://learn.microsoft.com/azure/container-apps/sessions) tool integration.
+
+Learn more about how to use this tool in the [LangChain documentation](https://js.langchain.com/docs/integrations/tools/azure_dynamic_sessions).
+
+## Installation
+
+```bash npm2yarn
+npm install @langchain/azure-dynamic-sessions
+```
+
+This package, along with the main LangChain package, depends on [`@langchain/core`](https://npmjs.com/package/@langchain/core/).
+If you are using this package with other LangChain packages, you should make sure that all of the packages depend on the same instance of @langchain/core.
+You can do so by adding appropriate fields to your project's `package.json` like this:
+
+```json
+{
+ "name": "your-project",
+ "version": "0.0.0",
+ "dependencies": {
+ "@langchain/azure-openai": "^0.0.4",
+ "langchain": "0.0.207"
+ },
+ "resolutions": {
+ "@langchain/core": "0.1.5"
+ },
+ "overrides": {
+ "@langchain/core": "0.1.5"
+ },
+ "pnpm": {
+ "overrides": {
+ "@langchain/core": "0.1.5"
+ }
+ }
+}
+```
+
+The field you need depends on the package manager you're using, but we recommend adding a field for the common `yarn`, `npm`, and `pnpm` to maximize compatibility.
+
+## Tool usage
+
+```typescript
+import { SessionsPythonREPLTool } from "@langchain/azure-dynamic-sessions";
+
+const tool = new SessionsPythonREPLTool({
+ poolManagementEndpoint:
+ process.env.AZURE_CONTAINER_APP_SESSION_POOL_MANAGEMENT_ENDPOINT || "",
+});
+
+const result = await tool.invoke("print('Hello, World!')\n1+2");
+
+console.log(result);
+
+// {
+// stdout: "Hello, World!\n",
+// stderr: "",
+// result: 3,
+// }
+```
diff --git a/libs/langchain-azure-dynamic-sessions/package.json b/libs/langchain-azure-dynamic-sessions/package.json
index 1b019a22d12b..c6814fd23752 100644
--- a/libs/langchain-azure-dynamic-sessions/package.json
+++ b/libs/langchain-azure-dynamic-sessions/package.json
@@ -39,7 +39,7 @@
"license": "MIT",
"dependencies": {
"@azure/identity": "^4.2.0",
- "@langchain/core": "~0.1",
+ "@langchain/core": "~0.2",
"uuid": "^9.0.1"
},
"devDependencies": {
diff --git a/libs/langchain-azure-dynamic-sessions/src/tools.ts b/libs/langchain-azure-dynamic-sessions/src/tools.ts
index 494b5546c338..82a8b10e392f 100644
--- a/libs/langchain-azure-dynamic-sessions/src/tools.ts
+++ b/libs/langchain-azure-dynamic-sessions/src/tools.ts
@@ -1,3 +1,6 @@
+import { promises as fs } from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
import { Tool } from "@langchain/core/tools";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import {
@@ -5,9 +8,6 @@ import {
getBearerTokenProvider,
} from "@azure/identity";
import { v4 as uuidv4 } from "uuid";
-import { readFile } from "fs/promises";
-import path from "path";
-import { fileURLToPath } from "url";
const userAgentPrefix = "langchainjs-azure-dynamic-sessions";
@@ -15,10 +15,18 @@ let userAgent = "";
async function getuserAgentSuffix(): Promise {
try {
if (!userAgent) {
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- const data = await readFile(
- path.join(__dirname, "..", "package.json"),
+ let currentDir;
+ try {
+ currentDir = __dirname;
+ } catch (e) {
+ // Workaround to make the build compatible with both ESM and CJS
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ currentDir = path.dirname(fileURLToPath(import.meta.url));
+ }
+
+ const data = await fs.readFile(
+ path.join(currentDir, "..", "package.json"),
"utf8"
);
const json = await JSON.parse(data);
@@ -119,7 +127,7 @@ export class SessionsPythonREPLTool extends Tool {
"";
if (!this.poolManagementEndpoint) {
- throw new Error("poolManagementEndpoint is required.");
+ throw new Error("poolManagementEndpoint must be defined.");
}
this.sessionId = params?.sessionId ?? uuidv4();
diff --git a/yarn.lock b/yarn.lock
index 79c444275523..364be6d01791 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -8954,13 +8954,13 @@ __metadata:
languageName: unknown
linkType: soft
-"@langchain/azure-dynamic-sessions@workspace:libs/langchain-azure-dynamic-sessions":
+"@langchain/azure-dynamic-sessions@workspace:^, @langchain/azure-dynamic-sessions@workspace:libs/langchain-azure-dynamic-sessions":
version: 0.0.0-use.local
resolution: "@langchain/azure-dynamic-sessions@workspace:libs/langchain-azure-dynamic-sessions"
dependencies:
"@azure/identity": ^4.2.0
"@jest/globals": ^29.5.0
- "@langchain/core": ~0.1
+ "@langchain/core": ~0.2
"@langchain/scripts": ~0.0
"@swc/core": ^1.3.90
"@swc/jest": ^0.2.29
@@ -22596,6 +22596,7 @@ __metadata:
"@gomomento/sdk": ^1.51.1
"@google/generative-ai": ^0.7.0
"@langchain/anthropic": "workspace:*"
+ "@langchain/azure-dynamic-sessions": "workspace:^"
"@langchain/azure-openai": "workspace:*"
"@langchain/cloudflare": "workspace:*"
"@langchain/cohere": "workspace:*"