langchain-core 中基类的子类。示例包括 chat models、tools、retrievers 等。
你的集成包通常会至少实现这些组件之一的子类。展开下方标签页查看各组件的详细信息。
- Chat Models
- Embeddings
- Tools
- Middleware
- Checkpointers
- Sandboxes
Chat model 是
BaseChatModel 类的子类。它们实现用于生成 chat completion、处理消息格式化和管理模型参数的方法。Chat model 集成指南目前仍在编写中。在此期间,请阅读 chat model 概念指南,了解 LangChain chat model 的工作方式。你也可以参考 LangChain repo 中的现有集成
Embedding model 是
Embeddings 类的子类。Embedding model 集成指南目前仍在编写中。在此期间,请阅读 embedding model 概念指南,了解 LangChain embedding model 的工作方式。
Tool 主要有两种用途:
- 定义一个传给 chat model 工具调用功能的 “input schema” 或 “args schema”,并附带文本请求,使 chat model 可以生成 “tool call”,即调用该工具所需的参数。
- 接收上面生成的 “tool call”,执行某个动作,并返回可以作为 ToolMessage 传回 chat model 的响应。
BaseTool 基类。该接口有 3 个属性和 2 个方法,应在子类中实现。Tools 集成指南目前仍在编写中。在此期间,请阅读 tools 概念指南,了解 LangChain tool 的工作方式。
Middleware 让你可以通过 hook model call、tool call 和 agent 生命周期事件来自定义 agent 行为。Middleware 类会继承
提供商特定 middleware 位于提供商的集成包中,例如
AgentMiddleware 基类。构建集成之前,请阅读自定义 middleware 指南,了解 hooks、state updates 和 middleware 模式。Middleware 集成通常分为两类:| 类型 | 描述 | 示例 |
|---|---|---|
| 提供商特定 | 利用提供商的独特能力 | Prompt caching、native tool execution、content moderation |
| 跨提供商 | 适用于任何模型或工具 | Rate limiting、PII detection、logging、guardrails |
langchain-anthropic。跨提供商 middleware 可以作为独立包发布。你也可以使用以下现有 middleware 集成作为参考:OpenAI content moderation
带有配置选项和退出行为的单个 middleware。
Anthropic middleware
多个 middleware 类,用于 prompt caching、tools、memory 和 file search。
AWS prompt caching
提供商特定 prompt caching,包含模型行为表。
Custom middleware guide
Hooks、state updates 和模式的完整参考。
Checkpointer 在 LangGraph 中启用持久化,允许 agent 跨交互保存和恢复 state。查看 LangGraph repo 中的现有 checkpointer 集成,了解实现示例。
Sandbox 集成让 Deep Agents 可以在隔离环境中运行代码。实现 Deep Agents 中的 将其放入
SandboxBackendProtocol。该协议包括 execute()、async 变体,以及 ls、read、write、edit、glob 和 grep 等文件系统工具方法。实践中,如果你的 sandbox 环境可以运行 shell 命令并且有可用的 python3,通常应继承 BaseSandbox。BaseSandbox 会通过 python3 提供文件系统操作,因此你主要需要实现 execute()、upload_files()、download_files() 和 id。Example BaseSandbox scaffold
from __future__ import annotations
from deepagents.backends.protocol import (
ExecuteResponse,
FileDownloadResponse,
FileUploadResponse,
)
from deepagents.backends.sandbox import BaseSandbox
class MySandbox(BaseSandbox):
def __init__(self, client: MySandboxSdkClient) -> None:
self._client = client
@property
def id(self) -> str:
return self._client.sandbox_id
def execute(
self,
command: str,
*,
timeout: int | None = None,
) -> ExecuteResponse:
# Execute `command` in your sandbox and map the provider response
# into ExecuteResponse.
result = self._client.run(command=command, timeout=timeout)
output = result.stdout or ""
if result.stderr:
output += f"\n<stderr>{result.stderr}</stderr>"
return ExecuteResponse(
output=output,
exit_code=result.exit_code,
truncated=False,
)
def upload_files(
self,
files: list[tuple[str, bytes]],
) -> list[FileUploadResponse]:
# Validate paths, batch requests where possible, and map provider
# results back into FileUploadResponse objects in input order.
# Only catch and normalize errors that an LLM can plausibly retry
# or fix, such as invalid_path or file_not_found.
return self._client.upload_files(files)
def download_files(self, paths: list[str]) -> list[FileDownloadResponse]:
# Validate paths, batch requests where possible, and map provider
# results back into FileDownloadResponse objects in input order.
# Only catch and normalize errors that an LLM can plausibly retry
# or fix, such as invalid_path or file_not_found.
return self._client.download_files(paths)
async def aexecute(
self,
command: str,
*,
timeout: int | None = None,
) -> ExecuteResponse:
...
async def aupload_files(
self,
files: list[tuple[str, bytes]],
) -> list[FileUploadResponse]:
...
async def adownload_files(
self,
paths: list[str],
) -> list[FileDownloadResponse]:
...
测试你的集成
使用 sandbox 标准测试套件验证你的集成。Python 套件使用langchain_tests.integration_tests 中的 SandboxIntegrationTests;继承它,并提供一个 sandbox fixture,yield 一个干净的 SandboxBackendProtocol 实例。Example sandbox standard test setup
from __future__ import annotations
from collections.abc import Iterator
import pytest
from deepagents.backends.protocol import SandboxBackendProtocol
from langchain_tests.integration_tests import SandboxIntegrationTests
from langchain_myprovider import MySandbox
from myprovider_sdk import MySandboxSdkClient
class TestMySandboxStandard(SandboxIntegrationTests):
@pytest.fixture(scope="class")
def sandbox(self) -> Iterator[SandboxBackendProtocol]:
client = MySandboxSdkClient()
backend = MySandbox(client=client)
try:
yield backend
finally:
# Replace this with your provider's cleanup logic.
client.delete_sandbox(backend.id)
tests/integration_tests/test_sandbox.py 等文件中。标准套件会为你处理实际的文件系统和命令执行断言。参考实现: 请参阅 Daytona partner integration,它继承 BaseSandbox 并实现 execute()、upload_files()、download_files() 和 id。Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

