Skip to content

feat(langchain): add LangChain DeepAgents backend adapter#310

Open
johannhartmann wants to merge 12 commits intokubernetes-sigs:mainfrom
mayflower:feat-langchain-deepagents-backend
Open

feat(langchain): add LangChain DeepAgents backend adapter#310
johannhartmann wants to merge 12 commits intokubernetes-sigs:mainfrom
mayflower:feat-langchain-deepagents-backend

Conversation

@johannhartmann
Copy link

What This Does

This adds a LangChain DeepAgents backend implementation that uses agent-sandbox for isolated code execution. It lets LangChain agents spin up Sandboxes on the fly to run code, read/write files, and clean up after themselves.

We've been using this at Mayflower to run LangChain agents on our clusters and wanted to contribute it back upstream.

Changes

1. SandboxClient enhancements (clients/python/agentic-sandbox-client/)

  • claim_name parameter for SandboxClaim-based provisioning (warm pool support)
  • delete_on_exit parameter for automatic cleanup
  • connect() classmethod for reconnecting to existing sandboxes
  • delete() method and was_reconnected property

2. New langchain-agent-sandbox package (clients/python/langchain-agent-sandbox/)

  • Implements BackendProtocol from langchain-deepagents
  • Factory pattern via SandboxBackendFactory for multi-agent use
  • Configurable cleanup policies (delete on exit, keep on error, etc.)
  • Warm pool support via SandboxClaim integration
  • Unit tests covering backend lifecycle, file operations, and policies

3. Example application (examples/langchain-deepagents/)

  • Working example with skills, sandbox template, and a kind cluster test script

4. E2E test (test/e2e/clients/python/)

  • End-to-end test for the backend running against a kind cluster

How to Test

# Unit tests
uv pip install -e clients/python/agentic-sandbox-client
uv pip install -e clients/python/langchain-agent-sandbox[test]
uv run pytest clients/python/langchain-agent-sandbox/tests/test_backend.py -v

# E2E (requires kind cluster with extensions)
make deploy-kind EXTENSIONS=true
uv run pytest test/e2e/clients/python/test_e2e_langchain_backend.py -v

Notes

  • This depends on the langchain-deepagents package from LangChain
  • Happy to split this into smaller PRs if that's easier to review
  • Any feedback on the approach or API design is very welcome!

@netlify
Copy link

netlify bot commented Feb 11, 2026

Deploy Preview for agent-sandbox canceled.

Name Link
🔨 Latest commit daeb330
🔍 Latest deploy log https://app.netlify.com/projects/agent-sandbox/deploys/69a30ede19a47c00087269ba

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Feb 11, 2026

CLA Not Signed

@k8s-ci-robot
Copy link
Contributor

Welcome @johannhartmann!

It looks like this is your first PR to kubernetes-sigs/agent-sandbox 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/agent-sandbox has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Feb 11, 2026
@k8s-ci-robot
Copy link
Contributor

Hi @johannhartmann. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Feb 11, 2026
@janetkuo
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 12, 2026

## Architecture

```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - in case you find it cumbersome to maintain this diagram, GitHub supports mermaid diagrams via ```mermaid: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-diagrams#creating-mermaid-diagrams

@@ -0,0 +1,59 @@
# Copyright 2025 The Kubernetes Authors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 2026 (and other places too!)

Suggested change
# Copyright 2025 The Kubernetes Authors.
# Copyright 2026 The Kubernetes Authors.

# SandboxTemplate for LangChain DeepAgents
# This template defines the container image and resources for sandbox pods.
---
apiVersion: agents.x-k8s.io/v1alpha1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API group for SandboxTemplate should be:

Suggested change
apiVersion: agents.x-k8s.io/v1alpha1
apiVersion: extensions.agents.x-k8s.io/v1alpha1

if self._custom_claim_name:
self.claim_name = self._custom_claim_name
# Check if the claim already exists
if self._claim_exists(self.claim_name):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before reconnecting, should it verify that the existing claim was created from the same template_name? Otherwise, it might be surprising.

raise

@trace_span("create_claim")
def _create_claim(self, trace_context_str: str = ""):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this needs to be renamed, given that it supports reconnecting as well (something like _setup_claim perhaps?) Reminder to update trace_span as well.


logging.info(f"Explicitly deleting SandboxClaim: {self.claim_name}")
try:
self.custom_objects_api.delete_namespaced_custom_object(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clear self.claim_name as well, so that we don't attempt to delete the same claim again (will result in 404)

secured = SandboxPolicyWrapper(
backend,
deny_prefixes=["/etc", "/sys", "/proc"],
deny_commands=["rm -rf", "curl", "wget"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be bypassed by variations like rm -r -f or using aliases. Suggest adding a warning in the doc that this is a best-effort guardrail and not a substitute for kernel-level isolation (like gVisor).

# Wrap with security policies
secured = SandboxPolicyWrapper(
backend,
deny_prefixes=["/etc", "/sys", "/proc"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A malicious agent could bypass /etc using a path like /app/../etc. Does SandboxPolicyWrapper use something like os.path.abspath() or os.path.realpath()?

@janetkuo
Copy link
Member

@johannhartmann thanks for the PR! Reminder to sign the CLA following #310 (comment)

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 18, 2026
@janetkuo janetkuo self-assigned this Feb 18, 2026
…upport

Add claim_name parameter for SandboxClaim-based provisioning, delete_on_exit
parameter for automatic cleanup, connect() classmethod for reconnecting to
existing sandboxes, delete() method for explicit teardown, and was_reconnected
property for detecting reconnection state.
Implement BackendProtocol from langchain-deepagents providing sandbox
lifecycle management, file I/O, and code execution. Includes factory
pattern with SandboxBackendFactory, configurable cleanup policies, and
warm pool support via SandboxClaim integration. Comprehensive test suite
covers backend lifecycle, file operations, and policy configurations.
Add end-to-end test for the LangChain DeepAgents backend running against
a kind cluster with sandbox templates and warm pools.
Add example application demonstrating the LangChain DeepAgents backend
with sandbox templates, skills support, and a kind cluster test script.
ls -a -p on Linux outputs ./ and ../ with trailing slashes, but the
filter only checked for . and .. (without slash), letting dot entries
leak into results. Also handles empty lines from trailing newlines.
… root_dir=/

- AgentSandboxBackend now inherits from SandboxBackendProtocol so
  isinstance checks in DeepAgents FilesystemMiddleware pass without
  needing ABC.register().
- Fix _to_internal path validation when root_dir='/': the check
  startswith(root_dir + '/') became startswith('//') which rejected
  every valid absolute path. Now handles the root case separately.
@johannhartmann johannhartmann force-pushed the feat-langchain-deepagents-backend branch from f202be6 to f7f6921 Compare February 19, 2026 18:10
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: johannhartmann
Once this PR has been reviewed and has the lgtm label, please ask for approval from janetkuo. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 19, 2026
@janetkuo
Copy link
Member

From the test failure test/e2e/clients/python/test_e2e_langchain_backend.py: ModuleNotFoundError: No module named 'langchain_agent_sandbox' the langchain-agent-sandbox package needs to be installed first

@k8s-ci-robot
Copy link
Contributor

@johannhartmann: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
presubmit-agent-sandbox-e2e-test daeb330 link true /test presubmit-agent-sandbox-e2e-test

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants