# https://github.com/langchain-ai/langchainjs 项目说明书

生成时间：2026-05-16 06:23:32 UTC

## 目录

- [Introduction to LangChain.js](#introduction)
- [Getting Started](#getting-started)
- [Package Architecture](#package-architecture)
- [Core Abstractions](#core-abstractions)
- [Chat Models and LLM Providers](#chat-models)
- [Embeddings Integration](#embeddings)
- [Agent Framework](#agent-framework)
- [Tools and Toolkits](#tools-toolkits)
- [Vector Stores](#vector-stores)
- [Memory Systems](#memory-system)

<a id='introduction'></a>

## Introduction to LangChain.js

### 相关页面

相关主题：[Getting Started](#getting-started), [Package Architecture](#package-architecture)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [README.md](https://github.com/langchain-ai/langchainjs/blob/main/README.md)
- [libs/langchain/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/README.md)
- [libs/langchain-core/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/README.md)
- [libs/langchain-classic/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/README.md)
</details>

# Introduction to LangChain.js

LangChain.js is a JavaScript/TypeScript framework designed for building applications with Large Language Models (LLMs) through composability. It provides a standardized interface for working with different LLM providers, offers tools for prompt management, and enables the creation of complex chains and agents that can interact with external data sources and APIs.

## Architecture Overview

LangChain.js is organized into a modular monorepo structure with multiple packages that serve different purposes. Each package focuses on specific functionality while maintaining interoperability through shared dependencies.

```mermaid
graph TD
    A[Application Layer] --> B[libs/langchain]
    A --> C[@langchain/classic]
    B --> D[@langchain/core]
    C --> D
    E[Provider Integrations] --> F[libs/providers/*]
    F --> D
    G[Utility Packages] --> H[@langchain/textsplitters]
    H --> D
    
    style A fill:#e1f5fe
    style D fill:#f3e5f5
    style F fill:#fff3e0
```

## Core Packages

### @langchain/core

The foundational package containing essential abstractions and schemas used throughout the LangChain.js ecosystem.

| Property | Value |
|----------|-------|
| Name | @langchain/core |
| Version | 1.1.46 |
| Type | ES Module |
| Engine | Node.js >= 20 |

**Key Dependencies:**

- `@cfworker/json-schema` - JSON schema validation
- `@standard-schema/spec` - Standard schema support
- `js-tiktoken` - Token counting
- `langsmith` - Tracing and evaluation (>= 0.5.0 < 1.0.0)
- `mustache` - Template rendering
- `p-queue` - Promise queue management
- `zod` - Schema validation

资料来源：[libs/langchain-core/package.json:1-35](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/package.json)

### libs/langchain

The main LangChain package that provides the primary API surface for building LLM applications. This package focuses on the essential building blocks for modern agent development, including the `createAgent` API.

### @langchain/classic

A backward-compatibility package containing functionality migrated from LangChain v0.x. This package exists to support existing applications while the core `langchain` package focuses on modern agent development.

**When to use @langchain/classic:**

- Existing code using legacy chains (LLMChain, ConversationalRetrievalQAChain, RetrievalQAChain)
- Using the indexing API with RecordManager
- Depending on community integrations previously re-exported from langchain
- Maintaining existing applications not yet ready for the new `createAgent` API

资料来源：[libs/langchain-classic/README.md:1-45](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/README.md)

## Provider Integrations

LangChain.js provides integration packages for various LLM providers through the `libs/providers` directory.

### Available Provider Packages

| Package | Purpose |
|---------|---------|
| @langchain/anthropic | Anthropic Claude models integration |
| @langchain/google-common | Common utilities for Google AI models |
| @langchain/tavily | Tavily search integration |
| @langchain/qdrant | Qdrant vector database integration |

Each integration package follows a consistent template structure and includes:

- TypeScript source code
- ESLint linting configuration
- Vitest testing framework
- TypeScript build compilation via tsdown

资料来源：[libs/providers/langchain-tavily/package.json:1-35](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-tavily/package.json)
资料来源：[libs/providers/langchain-qdrant/package.json:1-35](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-qdrant/package.json)

## Text Processing

### @langchain/textsplitters

A dedicated package for splitting text into chunks, commonly used in retrieval-augmented generation (RAG) pipelines.

**Supported Splitting Strategies:**

| Strategy | Use Case |
|----------|----------|
| RecursiveCharacterTextSplitter | General purpose text splitting |
| HTML Text Splitter | Structured HTML content |
| Custom Separators | Domain-specific splitting requirements |

```typescript
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";

const splitter = RecursiveCharacterTextSplitter.fromLanguage("html", {
  chunkSize: 175,
  chunkOverlap: 20,
});
const output = await splitter.createDocuments([text]);
```

资料来源：[libs/langchain-textsplitters/README.md:1-40](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-textsplitters/README.md)
资料来源：[examples/src/langchain-classic/indexes/html_text_splitter.ts:1-20](https://github.com/langchain-ai/langchainjs/blob/main/examples/src/langchain-classic/indexes/html_text_splitter.ts)

## Testing Infrastructure

LangChain.js includes custom Jest/Vitest matchers for testing chains, agents, and messages.

```typescript
export const langchainMatchers = {
  toBeHumanMessage,
  toBeAIMessage,
  toBeSystemMessage,
  toBeToolMessage,
  toHaveToolCalls,
  toHaveToolCallCount,
  toContainToolCall,
  toHaveToolMessages,
  toHaveBeenInterrupted,
  toHaveStructuredResponse,
};
```

**Matcher Usage:**

```typescript
expect(someMessage).toBeHumanMessage();
expect(someMessage).toBeAIMessage();
expect(someChain).toHaveToolCalls([
  { name: "calculator", args: { expression: "2+2" } }
]);
```

资料来源：[libs/langchain-core/src/testing/matchers.ts:80-95](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/testing/matchers.ts)

## Agent Middleware

### Human-in-the-Loop (HITL) Middleware

The framework supports human-in-the-loop workflows through configurable interrupt mechanisms.

```typescript
const config: InterruptOnConfig = {
  allowedDecisions: ["approve", "edit"],
  description: formatToolDescription
};
```

**Configuration Schema:**

| Parameter | Type | Description |
|-----------|------|-------------|
| allowedDecisions | string[] | Actions the human can take |
| description | string \| Function | Description of the interrupt |
| argsSchema | Record | JSON schema for action arguments |

资料来源：[libs/langchain/src/agents/middleware/hitl.ts:60-80](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/middleware/hitl.ts)

## Message Block Translators

LangChain.js provides standardized message translation between different API formats, particularly for OpenAI and Anthropic.

```typescript
const BlockTranslator = {
  translateContent: (message) => {
    if (typeof message.content === "string") {
      return convertToV1FromChatCompletions(message);
    }
    return convertToV1FromResponses(message);
  },
  translateContentChunk: (message) => {
    if (typeof message.content === "string") {
      return convertToV1FromChatCompletionsChunk(message);
    }
    return convertToV1FromResponsesChunk(message);
  },
};
```

资料来源：[libs/langchain-core/src/messages/block_translators/openai.ts:1-15](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/messages/block_translators/openai.ts)

## Namespace System

LangChain.js uses a namespace utility for tracking and identifying LangChain-specific objects.

```typescript
export const ns = createNamespace("langchain");
```

Namespaces provide:
- Hierarchical path organization (`ns.sub("component")`)
- Instance type checking (`ns.isInstance(obj)`)
- Branded type creation for type safety

资料来源：[libs/langchain-core/src/utils/namespace.ts:1-45](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/utils/namespace.ts)

## Legacy Chains (Deprecated)

The `@langchain/classic` package includes deprecated chain implementations:

| Chain | Purpose |
|-------|---------|
| LLMChain | Basic LLM call with prompt template |
| ConversationalRetrievalQAChain | Q&A with conversation memory |
| RetrievalQAChain | Q&A over documents |
| StuffDocumentsChain | Stuff documents into prompt |
| MapReduceDocumentsChain | Map-reduce document operations |
| RefineDocumentsChain | Iterative document refinement |

资料来源：[libs/langchain-classic/README.md:15-30](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/README.md)

## Development Workflow

### Repository Structure

```
langchainjs/
├── libs/
│   ├── langchain/              # Main LangChain package
│   ├── langchain-core/         # Core abstractions
│   ├── langchain-classic/      # Legacy v0.x functionality
│   ├── langchain-textsplitters/ # Text splitting utilities
│   └── providers/              # Provider integrations
├── examples/                   # Usage examples
└── libs/create-langchain-integration/  # Package template
```

### Building Packages

```bash
pnpm install
pnpm build
```

### Running Examples

```bash
cp .env.example .env
# Edit .env with API keys
pnpm run start ./src/path/to/example.ts
```

资料来源：[examples/src/README.md:1-25](https://github.com/langchain-ai/langchainjs/blob/main/examples/src/README.md)

## Migration Guide

### From langchain v0.x to v1.0

**For new projects:** Use the `langchain` v1.0 package with `createAgent` API.

**For existing projects:** Install `@langchain/classic` for backward compatibility:

```bash
npm install @langchain/classic @langchain/core
```

The new APIs provide:
- Cleaner, more powerful agent building
- Middleware support
- Better performance for modern workflows
- Focused API surface with less complexity
- Active development on v1.0 features

资料来源：[libs/langchain-classic/README.md:30-55](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/README.md)

## Version Compatibility

When using multiple LangChain packages, ensure they share the same `@langchain/core` instance:

```json
{
  "dependencies": {
    "@langchain/core": "^0.3.0"
  },
  "overrides": {
    "@langchain/core": "^0.3.0"
  },
  "pnpm": {
    "overrides": {
      "@langchain/core": "^0.3.0"
    }
  }
}
```

资料来源：[libs/create-langchain-integration/template/README.md:20-35](https://github.com/langchain-ai/langchainjs/blob/main/libs/create-langchain-integration/template/README.md)

---

<a id='getting-started'></a>

## Getting Started

### 相关页面

相关主题：[Introduction to LangChain.js](#introduction)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [README.md](https://github.com/langchain-ai/langchainjs/blob/main/README.md)
- [libs/langchain-textsplitters/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-textsplitters/README.md)
- [libs/langchain-classic/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/README.md)
- [libs/providers/langchain-anthropic/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-anthropic/README.md)
- [libs/providers/langchain-cohere/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-cohere/README.md)
- [libs/providers/langchain-aws/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-aws/README.md)
- [examples/src/README.md](https://github.com/langchain-ai/langchainjs/blob/main/examples/src/README.md)
- [libs/create-langchain-integration/template/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/create-langchain-integration/template/README.md)
</details>

# Getting Started

LangChain.js is a comprehensive framework for building LLM-powered applications. It enables developers to chain together interoperable components and third-party integrations, simplifying AI application development while future-proofing decisions as underlying technology evolves.

资料来源：[README.md:1-15]()

## Prerequisites

### System Requirements

| Requirement | Version/Notes |
|-------------|--------------|
| Node.js | v18.x or higher recommended |
| Package Manager | npm, yarn, or pnpm |
| Runtime | Node.js or Edge (Vercel, Cloudflare Workers) |

### Environment Setup

Before installing LangChain.js packages, ensure your environment is properly configured. Most integrations require API keys from respective service providers.

```bash
# Example API key environment variables
export ANTHROPIC_API_KEY="your-key-here"
export OPENAI_API_KEY="your-key-here"
export COHERE_API_KEY="your-key-here"
export TOGETHER_AI_API_KEY="your-key-here"
export BEDROCK_AWS_REGION="us-east-1"
```

资料来源：[libs/providers/langchain-anthropic/README.md:1-30]()

## Installation

LangChain.js follows a modular architecture with packages organized by functionality. Understanding the package structure is essential for proper installation.

### Package Architecture Overview

```mermaid
graph TD
    A[Application] --> B[langchain v1.0]
    A --> C[@langchain/classic]
    B --> D[@langchain/core]
    C --> D
    B --> E[@langchain/textsplitters]
    B --> F[Provider Packages]
    F --> G[@langchain/anthropic]
    F --> H[@langchain/openai]
    F --> I[@langchain/cohere]
    F --> J[@langchain/aws]
    F --> K[@langchain/google-common]
```

资料来源：[libs/langchain-classic/README.md:1-50]()

### Core Packages

#### @langchain/core

The foundational package that all other LangChain packages depend on. It provides base classes, interfaces, and utilities used across the ecosystem.

```bash npm2yarn
npm install @langchain/core
```

资料来源：[libs/providers/langchain-anthropic/README.md:1-15]()

#### langchain (v1.0)

The main package for building agents with modern APIs, including the `createAgent` function with middleware support.

```bash npm2yarn
npm install langchain
```

#### @langchain/textsplitters

Specialized package for splitting documents into chunks, commonly used in retrieval-augmented generation (RAG) pipelines.

```bash npm2yarn
npm install @langchain/textsplitters @langchain/core
```

资料来源：[libs/langchain-textsplitters/README.md:1-25]()

### Provider Packages

LangChain.js provides official integrations for various LLM providers:

| Package | Purpose | Documentation |
|---------|---------|---------------|
| `@langchain/anthropic` | Anthropic Claude models | [Link](https://js.langchain.com/docs/integrations/chat/anthropic) |
| `@langchain/openai` | OpenAI GPT models | [Link](https://js.langchain.com/docs/integrations/chat/openai) |
| `@langchain/cohere` | Cohere models | [Link](https://js.langchain.com/docs/integrations/chat/cohere) |
| `@langchain/aws` | AWS Bedrock models | [Link](https://js.langchain.com/docs/integrations/chat/aws-bedrock) |
| `@langchain/together-ai` | Together AI models | [Link](https://js.langchain.com/docs/integrations/chat/together) |

```bash npm2yarn
npm install @langchain/anthropic @langchain/core
npm install @langchain/openai @langchain/core
npm install @langchain/cohere @langchain/core
```

资料来源：[libs/providers/langchain-anthropic/README.md:1-25]()

### @langchain/classic (Legacy)

For existing applications using LangChain v0.x, the `@langchain/classic` package provides backward compatibility with legacy chains and functionality.

```bash npm2yarn
npm install @langchain/classic
```

This package requires `@langchain/core` as a peer dependency:

```bash npm2yarn
npm install @langchain/core
```

资料来源：[libs/langchain-classic/README.md:1-60]()

## Core Dependency Management

When using multiple LangChain packages together, ensure all packages resolve to the same instance of `@langchain/core` to avoid conflicts.

Add the following configuration to your `package.json`:

```json
{
  "name": "your-project",
  "version": "0.0.0",
  "dependencies": {
    "@langchain/anthropic": "^0.0.9",
    "@langchain/core": "^0.3.0"
  },
  "resolutions": {
    "@langchain/core": "^0.3.0"
  },
  "overrides": {
    "@langchain/core": "^0.3.0"
  },
  "pnpm": {
    "overrides": {
      "@langchain/core": "^0.3.0"
    }
  }
}
```

资料来源：[libs/providers/langchain-cohere/README.md:15-45]()

## Development Setup

### Building from Source

To develop with LangChain.js locally or run examples:

```bash
# Install dependencies from repository root
pnpm install

# Build all packages
pnpm build
```

资料来源：[examples/src/README.md:1-20]()

### Running Examples

Most examples require API keys. Configure your environment by copying the example environment file:

```bash
cp .env.example .env
```

Then edit `.env` with your API keys. Run examples using the provided script:

```bash
# From the examples/ directory
pnpm run start <path to example>

# Example
pnpm run start ./src/prompts/few_shot.ts
```

To run examples with transpiled JavaScript:

```bash
pnpm run start:dist <path to example>

# Example
pnpm run start:dist ./dist/prompts/few_shot.js
```

资料来源：[examples/src/README.md:1-45]()

### Testing

Test files follow naming conventions:
- Unit tests: `*.test.ts`
- Integration tests: `*.int.test.ts`

```bash
# Run all tests
pnpm test

# Run integration tests specifically
pnpm test:int
```

For individual packages:

```bash
# Build a specific package
pnpm build --filter @langchain/textsplitters

# Run tests for a specific package
cd libs/langchain-textsplitters
pnpm test
```

资料来源：[libs/langchain-textsplitters/README.md:25-50]()

## Quick Start Example

### Basic Chat Model Usage

```typescript
import { ChatAnthropic } from "@langchain/anthropic";
import { HumanMessage } from "@langchain/core/messages";

const model = new ChatAnthropic({
  model: "claude-sonnet-4-5-20250514",
});

const response = await model.invoke([
  new HumanMessage("Translate "I love programming" into French.")
]);

console.log(response.content);
// Output: J'adore la programmation.
```

资料来源：[libs/providers/langchain-anthropic/README.md:1-50]()

### Document Splitting for RAG

```typescript
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";

const text = `<!DOCTYPE html>
<html>
  <head>
    <title>LangChain</title>
  </head>
  <body>
    <h1>Welcome</h1>
    <p>Building applications with LLMs</p>
  </body>
</html>`;

const splitter = RecursiveCharacterTextSplitter.fromLanguage("html", {
  chunkSize: 175,
  chunkOverlap: 20,
});

const output = await splitter.createDocuments([text]);
console.log(output);
```

资料来源：[examples/src/langchain-classic/indexes/html_text_splitter.ts:1-30]()

## Choosing the Right Package

### For New Projects

Use `langchain` v1.0 for new projects. It provides:

- **`createAgent`**: A cleaner, more powerful way to build agents with middleware support
- **Better performance**: Optimized for modern agent workflows
- **Focused API surface**: Less complexity, easier to learn
- **Active development**: New features and improvements focus on v1.0 APIs

### For Legacy Projects

Use `@langchain/classic` if you:

- Have existing code using legacy chains (e.g., `LLMChain`, `ConversationalRetrievalQAChain`)
- Use the indexing API
- Depend on functionality from `@langchain/community` previously re-exported from `langchain`
- Are maintaining an existing application and not yet ready to migrate

### Package Decision Flow

```mermaid
graph TD
    A[New Project?] -->|Yes| B[Use langchain v1.0]
    A -->|No| C[Maintaining Existing Code?]
    C -->|Yes| D[Using Legacy Chains?]
    C -->|No| E[Use langchain v1.0]
    D -->|Yes| F[Use @langchain/classic]
    D -->|No| E
```

资料来源：[libs/langchain-classic/README.md:40-80]()

## Additional Resources

| Resource | Description |
|----------|-------------|
| [Documentation](https://docs.langchain.com/oss/javascript/langchain/overview) | Official LangChain.js documentation |
| [Deep Agents](https://docs.langchain.com/oss/javascript/deepagents/) | Higher-level package for common agent patterns |
| [Release Notes](https://docs.langchain.com/oss/javascript/releases/langchain-v1) | Version-specific changelog and migration guides |
| [API Reference](https://api.js.langchain.com/) | Generated API documentation |

资料来源：[README.md:15-30]()

---

<a id='package-architecture'></a>

## Package Architecture

### 相关页面

相关主题：[Introduction to LangChain.js](#introduction), [Core Abstractions](#core-abstractions)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [pnpm-workspace.yaml](https://github.com/langchain-ai/langchainjs/blob/main/pnpm-workspace.yaml)
- [turbo.json](https://github.com/langchain-ai/langchainjs/blob/main/turbo.json)
- [libs/langchain-core/package.json](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/package.json)
- [libs/langchain/package.json](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/package.json)
- [libs/langchain-classic/package.json](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/package.json)
- [libs/langchain-mcp-adapters/package.json](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-mcp-adapters/package.json)
- [libs/langchain-textsplitters/package.json](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-textsplitters/package.json)
</details>

# Package Architecture

## Overview

The LangChain.js repository employs a sophisticated **monorepo architecture** built on pnpm workspaces and Turbo. This design enables modular development, shared tooling, and independent versioning across multiple packages. The monorepo structure separates concerns into distinct layers: core abstractions, main integration packages, provider-specific SDKs, and community contributions. This architectural approach allows developers to install only the dependencies they need while maintaining a coherent ecosystem where packages can reference and compose with each other seamlessly.

## Monorepo Structure

### Workspace Organization

The repository uses **pnpm workspaces** to manage multiple packages under a single repository. This enables efficient dependency management, shared node_modules, and unified versioning strategies across the ecosystem.

```mermaid
graph TD
    A[Root: langchainjs] --> B[libs/]
    A --> C[examples/]
    A --> D[docs/]
    B --> E[langchain-core/]
    B --> F[langchain/]
    B --> G[langchain-classic/]
    B --> H[providers/]
    B --> I[langchain-textsplitters/]
    B --> J[community/]
    H --> K[langchain-openai/]
    H --> L[langchain-anthropic/]
    H --> M[langchain-google/]
    H --> N[langchain-pinecone/]
    H --> O[langchain-qdrant/]
```

### Build Pipeline with Turbo

The build system uses **Turbo** to orchestrate builds, tests, and linting across packages. Turbo's caching mechanism significantly improves build times by only rebuilding packages that have changed since the last build.

```mermaid
graph LR
    A[pnpm install] --> B[turbo build]
    B --> C[langchain-core]
    C --> D[langchain]
    C --> E[providers]
    C --> F[community]
    D --> G[langchain-classic]
    G --> H[examples]
```

## Core Packages

### @langchain/core

The foundational package that contains all core abstractions, interfaces, and utilities. This package is a peer dependency for all other LangChain packages and must be kept synchronized across the ecosystem.

```mermaid
graph TD
    A[@langchain/core] --> B[Messages & Chat Models]
    A --> C[Tools & Toolkits]
    A --> D[Vector Stores]
    A --> E[Document Loaders]
    A --> F[Output Parsers]
    A --> G[Retrievers]
    A --> H[Callbacks]
    A --> I[Memory]
```

**Key Responsibilities:**

- Defines base classes for all major abstractions (BaseLanguageModel, BaseRetriever, BaseChatModel)
- Provides TypeScript utilities and type guards for common patterns
- Exports core message types (AIMessage, HumanMessage, SystemMessage, ToolMessage)
- Implements hash utilities and data transformation functions

### @langchain/langchain

The main package that re-exports functionality from core and community packages. This serves as a convenient entry point for users who want access to all integrations without managing individual package dependencies.

### @langchain/classic

Contains legacy chain implementations from v0.x that have been deprecated or replaced in v1.0. This package provides migration paths for existing users while maintaining backward compatibility.

| Component | Description |
|-----------|-------------|
| LLMChain | Basic chain for calling an LLM with a prompt template |
| ConversationalRetrievalQAChain | Chain for conversational question-answering over documents |
| RetrievalQAChain | Chain for question-answering over documents without conversation memory |
| StuffDocumentsChain | Chain for stuffing documents into a prompt |
| MapReduceDocumentsChain | Chain for map-reduce operations over documents |
| RefineDocumentsChain | Chain for iterative refinement over documents |

## Text Processing Packages

### @langchain/textsplitters

Provides various implementations of text splitters commonly used in retrieval-augmented generation (RAG) pipelines. Text splitters break large documents into smaller chunks for embedding and retrieval.

**Supported Languages:**

| Language | Use Case |
|----------|----------|
| html | HTML document splitting |
| markdown | Markdown document splitting |
| javascript | JavaScript/TypeScript code splitting |
| python | Python code splitting |
| text | Plain text splitting |

**Example Usage:**

```typescript
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";

const splitter = RecursiveCharacterTextSplitter.fromLanguage("html", {
  chunkSize: 175,
  chunkOverlap: 20,
});
const output = await splitter.createDocuments([text]);
```

## Provider Integration Packages

The provider packages in `libs/providers/` contain integrations for specific third-party services. Each package follows a consistent structure and can be used independently or alongside other LangChain packages.

### Package Naming Convention

Provider packages follow the naming pattern `@langchain/[provider-name]`, where the provider name typically corresponds to the service being integrated. All provider packages depend on `@langchain/core` and may optionally depend on the provider's official SDK.

### Provider Packages

| Package | Dependency |
|---------|------------|
| @langchain/anthropic | @langchain/core, @anthropic-ai/sdk |
| @langchain/openai | @langchain/core, openai |
| @langchain/google | @langchain/core, @google-ai/generativelanguage |
| @langchain/pinecone | @langchain/core, @pinecone-database/pinecone |
| @langchain/qdrant | @langchain/core, qdrant-client |

### Common Package Structure

Each provider package follows a standardized structure:

```mermaid
graph TD
    A[Package Root] --> B[README.md]
    A --> C[package.json]
    A --> D[src/index.ts]
    A --> E[src/chat_models/]
    A --> F[src/output_parsers/]
    A --> G[tests/]
    E --> H[index.ts]
    G --> I[*.test.ts]
    G --> J[*.int.test.ts]
```

## Dependency Management

### Core Dependency Synchronization

When using multiple LangChain packages in a project, it is critical to ensure all packages depend on the same instance of `@langchain/core`. Package managers may resolve different versions of `@langchain/core` as separate instances, causing runtime errors.

**Recommended Configuration (package.json):**

```json
{
  "name": "your-project",
  "version": "0.0.0",
  "dependencies": {
    "@langchain/openai": "^0.0.0",
    "@langchain/anthropic": "^0.0.0",
    "@langchain/core": "^0.3.0"
  },
  "resolutions": {
    "@langchain/core": "^0.3.0"
  },
  "overrides": {
    "@langchain/core": "^0.3.0"
  }
}
```

Different package managers require specific configuration fields:

| Package Manager | Field |
|----------------|-------|
| npm/yarn | `resolutions` |
| pnpm | `pnpm.overrides` |

## Entry Points and Exports

### Export Strategy

Packages can export functionality through two mechanisms: re-exports from `src/index.ts` or explicit entry points defined in the `exports` field of `package.json`. The exports field enables conditional imports and tree-shaking optimization.

### Deprecation Warnings

When importing from deprecated entry points, packages emit console warnings directing users to the new import paths. These warnings can be suppressed by setting the environment variable `LANGCHAIN_SUPPRESS_MIGRATION_WARNINGS` to `"true"`.

```typescript
// Deprecated warning format
if (getEnvironmentVariable("LANGCHAIN_SUPPRESS_MIGRATION_WARNINGS") !== "true") {
  console.warn(warningText);
}
```

## Development Workflow

### Installing Dependencies

```bash
pnpm install
```

### Building Packages

Individual packages can be built using the Turbo filter:

```bash
pnpm build --filter @langchain/core
```

### Running Tests

| Test Type | Command | File Pattern |
|-----------|---------|--------------|
| Unit Tests | `pnpm test` | `*.test.ts` |
| Integration Tests | `pnpm test:int` | `*.int.test.ts` |

Test files should be located in a `tests/` directory within the `src/` folder.

### Linting and Formatting

```bash
pnpm lint && pnpm format
```

## Migration Path

### From langchain v0.x to v1.0

The v1.0 release introduces significant architectural changes. Legacy functionality has been moved to `@langchain/classic`, while new abstractions live in `@langchain/core` and provider-specific packages. Users upgrading from v0.x should:

1. Install `@langchain/classic` for backward compatibility
2. Install new provider packages as needed
3. Update import statements to use new package paths
4. Remove imports from deprecated `langchain/` subpaths

```bash
npm install @langchain/classic
npm install @langchain/anthropic @langchain/core
```

## Summary

The LangChain.js package architecture reflects a commitment to modularity, performance, and developer experience. By separating concerns across well-defined packages, the ecosystem enables efficient development while maintaining interoperability. The monorepo structure, powered by pnpm workspaces and Turbo, provides the foundation for managing this complexity at scale.

---

<a id='core-abstractions'></a>

## Core Abstractions

### 相关页面

相关主题：[Package Architecture](#package-architecture), [Chat Models and LLM Providers](#chat-models)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [libs/langchain-core/src/runnables/base.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/runnables/base.ts)
- [libs/langchain-core/src/language_models/chat_models.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/language_models/chat_models.ts)
- [libs/langchain-core/src/language_models/llms.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/language_models/llms.ts)
- [libs/langchain-core/src/embeddings.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/embeddings.ts)
- [libs/langchain-core/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/vectorstores.ts)
- [libs/langchain-core/src/callbacks/base.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/callbacks/base.ts)
- [libs/langchain-core/src/prompts/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/prompts/index.ts)
- [libs/langchain-core/src/output_parsers/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/output_parsers/index.ts)
</details>

# Core Abstractions

The **Core Abstractions** in LangChain.js form the foundational building blocks that enable interoperability between different components in LLM-powered applications. These abstractions define standardized interfaces for language models, embeddings, vector stores, prompts, output parsers, and runnable components, allowing developers to swap implementations without changing application logic.

## Overview

LangChain.js implements a set of abstract base classes in `@langchain/core` that establish contracts for:

- **Runnable components** that can be composed into processing pipelines
- **Language models** (chat and completion models)
- **Embedding models** for vector representations
- **Vector stores** for semantic search and retrieval
- **Callback systems** for observability and event handling
- **Prompt templates** for structured input formatting
- **Output parsers** for structured response extraction

This abstraction layer enables loose coupling between components, making it straightforward to migrate between different model providers, vector stores, or embedding implementations while maintaining the same application code.

## Runnable Architecture

### BaseRunnable

The `BaseRunnable` class is the central abstraction that all runnable components extend. It provides a unified interface for invoking, batching, streaming, and composing operations.

```mermaid
graph TD
    A[BaseRunnable] --> B[invoke input]
    A --> C[batch inputs]
    A --> D[stream output]
    A --> E[async operations]
    
    B --> F[Runnable Sequence]
    B --> G[Runnable Branch]
    B --> H[Runnable Map]
    
    style A fill:#e1f5fe
    style F fill:#fff3e0
    style G fill:#fff3e0
    style H fill:#fff3e0
```

The `BaseRunnable` interface defines the following core methods:

| Method | Description | Signature |
|--------|-------------|-----------|
| `invoke` | Synchronous single input processing | `(input: Input, options?: RunnableConfig): Promise<Output>` |
| `batch` | Process multiple inputs efficiently | `(inputs: Input[], options?: RunnableConfig): Promise<Output[]>` |
| `stream` | Stream output chunks | `(input: Input, options?: RunnableConfig): Promise<Readable>` |
| `pipe` | Chain runnables together | `(coerceToRunnable(other)): BaseRunnable` |
| `withConfig` | Attach configuration | `(config: RunnableConfig): Runnable` |

资料来源：[libs/langchain-core/src/runnables/base.ts]()

### RunnableConfig

Configuration options passed to runnable operations:

```typescript
interface RunnableConfig {
  tags?: string[];
  metadata?: Record<string, unknown>;
  callbacks?: CallbackManager | CallbackHandler | Array<CallbackManager | CallbackHandler>;
  maxConcurrency?: number;
  maxTokens?: number;
  recursionLimit?: number;
  runName?: string;
}
```

资料来源：[libs/langchain-core/src/runnables/base.ts]()

## Language Model Abstractions

### BaseChatModel

The `BaseChatModel` abstract class defines the contract for chat-oriented language models that process messages and return chat results.

```mermaid
graph LR
    A[HumanMessage] --> B[BaseChatModel]
    C[SystemMessage] --> B
    D[AIMessage] --> B
    
    B --> E[invoke]
    B --> F[batch]
    B --> G[stream]
    
    E --> H[BaseMessageChunk]
    F --> I[BaseMessage[]]
    G --> J[Generator of chunks]
    
    style B fill:#e8f5e9
```

Key properties and methods:

| Property/Method | Type | Description |
|-----------------|------|-------------|
| `_llmType` | `string` (protected) | Identifier for the specific LLM type |
| `_invocationParams` | `Record<string, unknown>` (protected) | Parameters for the API call |
| `invoke` | `Method` | Process input and return chat result |
| `generate` | `Method` | Generate responses with usage metadata |
| `stream` | `Method` | Stream response chunks |

资料来源：[libs/langchain-core/src/language_models/chat_models.ts]()

### BaseLLM

The `BaseLLM` class provides the abstraction for traditional completion-based language models:

| Property/Method | Type | Description |
|-----------------|------|-------------|
| `_llmType` | `string` (protected) | Identifier for the LLM type |
| `_call` | `protected abstract method` | Core completion logic |
| `_streamResponseChunks` | `protected method` | Optional streaming support |
| `generate` | `Method` | Generate completions for prompts |

资料来源：[libs/langchain-core/src/language_models/llms.ts]()

### Message Types

LangChain.js uses a structured message system:

| Message Type | Description |
|-------------|-------------|
| `HumanMessage` | Input from the user |
| `AIMessage` | Response from the model |
| `SystemMessage` | System-level instructions |
| `ToolMessage` | Response from tool execution |
| `FunctionMessage` | Legacy function call responses |

## Embedding Abstractions

### BaseEmbeddings

The `BaseEmbeddings` abstract class provides a standardized interface for generating vector embeddings from text:

```mermaid
graph TD
    A[BaseEmbeddings] --> B[embedQuery text]
    A --> C[embedDocuments texts]
    
    B --> D[float array]
    C --> E[float array[]]
    
    style A fill:#fce4ec
```

| Method | Description | Parameters |
|--------|-------------|------------|
| `embedQuery` | Generate embedding for a single query | `document: string` |
| `embedDocuments` | Generate embeddings for multiple documents | `documents: string[]` |

Both methods return `Promise<number[][]>` representing the embedding vectors.

资料来源：[libs/langchain-core/src/embeddings.ts]()

## Vector Store Abstractions

### VectorStore

The `VectorStore` class provides an abstraction for storing and querying vector embeddings:

```mermaid
graph TD
    A[VectorStore] --> B[addDocuments]
    A --> C[similaritySearch]
    A --> D[similaritySearchVectorWithScore]
    A --> E[maxMarginalRelevanceSearch]
    
    B --> F[Document[]]
    C --> G[Document[] by query]
    D --> H[Document[] with scores]
    E --> I[Diverse results]
    
    style A fill:#fff8e1
```

Core methods:

| Method | Description | Parameters |
|--------|-------------|------------|
| `addDocuments` | Add documents to the store | `documents: Document[], addOptions?` |
| `similaritySearch` | Find similar documents | `query: string, k?, filter?` |
| `similaritySearchVectorWithScore` | Search by embedding vector | `query: number[], k?, filter?` |
| `maxMarginalRelevanceSearch` | MMR diversity search | `query: string, k?, fetchK?, lambda?` |
| `delete` | Remove documents by ID | `params` |

资料来源：[libs/langchain-core/src/vectorstores.ts]()

### Document Structure

Documents are the fundamental unit stored in vector stores:

```typescript
interface Document {
  pageContent: string;  // The text content
  metadata: Record<string, unknown>;  // Associated metadata
}
```

## Callback System

### BaseCallbackHandler

The `BaseCallbackHandler` provides a hook system for observing and logging LangChain operations:

```mermaid
graph LR
    A[LangChain Event] --> B[CallbackManager]
    B --> C[onChainStart]
    B --> D[onChainEnd]
    B --> E[onChainError]
    B --> F[onLLMStart]
    B --> G[onLLMEnd]
    B --> H[onToolStart]
    B --> I[onToolEnd]
    
    style B fill:#e3f2fd
```

Event handlers:

| Event | Handler | Trigger |
|-------|---------|---------|
| Chain lifecycle | `onChainStart`, `onChainEnd`, `onChainError` | Chain execution |
| LLM lifecycle | `onLLMStart`, `onLLMEnd`, `onLLMError` | Model calls |
| Tool lifecycle | `onToolStart`, `onToolEnd`, `onToolError` | Tool execution |
| Retriever | `onRetrieverStart`, `onRetrieverEnd` | Retrieval operations |
| Text generation | `onText`, `onLLMNewToken` | Streaming events |

资料来源：[libs/langchain-core/src/callbacks/base.ts]()

## Prompt Abstractions

### BasePromptTemplate

The `BasePromptTemplate` abstract class standardizes prompt creation and formatting:

```mermaid
graph TD
    A[BasePromptTemplate] --> B[merge]
    A --> C[partial]
    A --> D[invoke]
    
    E[Input Values] --> C
    F[Partial Values] --> B
    G[Runtime Input] --> D
    
    B --> H[PromptTemplate]
    C --> I[PromptTemplate]
    D --> J[Formatted String]
    
    style A fill:#f3e5f5
```

Key methods:

| Method | Description |
|--------|-------------|
| `invoke` | Format prompt with input values |
| `partial` | Create partially filled template |
| `merge` | Combine multiple templates |
| `save` | Serialize template to file |

资料来源：[libs/langchain-core/src/prompts/index.ts]()

### PromptValue

Prompt values represent the formatted input to language models:

```typescript
interface PromptValue {
  toChatMessages(): BaseMessage[];
  toString(): string;
}
```

## Output Parser Abstractions

### BaseOutputParser

The `BaseOutputParser` class provides a standardized interface for parsing and validating model outputs:

```mermaid
graph TD
    A[BaseOutputParser] --> B[parse text]
    A --> C[parseWithPrompt]
    A --> D[getFormatInstructions]
    
    E[Raw Output] --> B
    F[Prompt + Output] --> C
    
    B --> G[Structured Result]
    C --> G
    D --> H[Format Instructions]
    
    style A fill:#e0f7fa
```

Required methods:

| Method | Description | Return Type |
|--------|-------------|-------------|
| `parse` | Parse raw output | `Promise<T>` |
| `parseWithPrompt` | Parse with prompt context | `Promise<T>` |
| `getFormatInstructions` | Get parsing instructions | `string` |

Optional methods:

| Method | Description |
|--------|-------------|
| `invoke` | Unified parsing interface |
| `resultType` | TypeScript type information |

资料来源：[libs/langchain-core/src/output_parsers/index.ts]()

## Component Composition

LangChain.js enables powerful composition through the pipe operator:

```mermaid
graph LR
    A[PromptTemplate] -->|"pipe"| B[ChatModel]
    B -->|"pipe"| C[OutputParser]
    
    style A fill:#fff8e1
    style B fill:#e8f5e9
    style C fill:#e0f7fa
```

Example composition:

```typescript
const chain = prompt.pipe(chatModel).pipe(outputParser);
const result = await chain.invoke({ input: "your question" });
```

This composition model allows:

- Sequential processing through `pipe()`
- Parallel execution with `RunnableParallel`
- Conditional branching with `RunnableBranch`
- Error handling and fallbacks

## Configuration and Initialization

All core abstractions accept configuration through constructor options:

```typescript
interface BaseLanguageModelParams {
  callbacks?: CallbackManager;
  tags?: string[];
  metadata?: Record<string, unknown>;
}

interface BaseChatModel extends BaseLanguageModelParams {
  temperature?: number;
  topP?: number;
  maxTokens?: number;
  modelName?: string;
}
```

## Extension Pattern

To implement a custom component, extend the base class and implement required abstract methods:

```typescript
import { BaseLLM } from "@langchain/core/language_models/llms";

class MyCustomLLM extends BaseLLM {
  _llmType() {
    return "my_custom_llm";
  }

  async _call(prompt: string): Promise<string> {
    // Implementation
    return response;
  }
}
```

## Summary

The Core Abstractions in LangChain.js provide:

| Layer | Purpose | Key Classes |
|-------|---------|-------------|
| **Runnable** | Unified component interface | `BaseRunnable`, `RunnableSequence` |
| **Language Models** | Model interoperability | `BaseChatModel`, `BaseLLM` |
| **Embeddings** | Vector generation | `BaseEmbeddings` |
| **Vector Stores** | Semantic storage | `VectorStore` |
| **Callbacks** | Observability | `BaseCallbackHandler` |
| **Prompts** | Structured inputs | `BasePromptTemplate` |
| **Output Parsers** | Structured outputs | `BaseOutputParser` |

These abstractions enable portable, testable, and composable LLM applications while maintaining flexibility to swap implementations as requirements evolve.

---

<a id='chat-models'></a>

## Chat Models and LLM Providers

### 相关页面

相关主题：[Embeddings Integration](#embeddings), [Agent Framework](#agent-framework)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [libs/providers/langchain-openai/src/chat_models/base.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-openai/src/chat_models/base.ts)
- [libs/providers/langchain-anthropic/src/chat_models.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-anthropic/src/chat_models.ts)
- [libs/providers/langchain-google-common/src/chat_models.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-google-common/src/chat_models.ts)
- [libs/providers/langchain-mistralai/src/chat_models.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-mistralai/src/chat_models.ts)
- [libs/providers/langchain-ollama/src/chat_models.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-ollama/src/chat_models.ts)
- [libs/providers/langchain-deepseek/src/chat_models.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-deepseek/src/chat_models.ts)
- [libs/providers/langchain-groq/src/chat_models.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-groq/src/chat_models.ts)
- [libs/providers/langchain-xai/src/chat_models/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-xai/src/chat_models/index.ts)
</details>

# Chat Models and LLM Providers

LangChain.js provides a unified abstraction layer for interacting with various Large Language Model (LLM) providers through a standardized chat model interface. This system enables developers to seamlessly switch between different providers (OpenAI, Anthropic, Google, Mistral AI, Ollama, Deepseek, Groq, xAI) while maintaining consistent API patterns for invocation, streaming, and tool usage.

## Architecture Overview

The chat model architecture follows a provider-specific implementation pattern where each LLM provider package contains its own chat model class that extends common base abstractions from `@langchain/core`.

```mermaid
graph TD
    A[Application Code] --> B[Chat Model Interface]
    B --> C[Provider Implementations]
    C --> D[OpenAI ChatModels]
    C --> E[Anthropic ChatModels]
    C --> F[Google ChatModels]
    C --> G[MistralAI ChatModels]
    C --> H[Ollama ChatModels]
    C --> I[Deepseek ChatModels]
    C --> J[Groq ChatModels]
    C --> K[xAI ChatModels]
    B --> L[Tool Calling Abstraction]
    B --> M[Streaming Abstraction]
```

## Core Message Types

LangChain.js defines standardized message types that form the foundation of the chat model system. All providers return variations of these message types with provider-specific metadata.

### AIMessage

The primary return type from chat model invocations, containing the model's response along with usage and response metadata.

```typescript
AIMessage {
  "id": "msg_01QDpd78JUHpRP6bRRNyzbW3",
  "content": "Here's the translation to French:\n\nJ'adore la programmation.",
  "response_metadata": {
    "id": "msg_01QDpd78JUHpRP6bRRNyzbW3",
    "model": "claude-sonnet-4-5-20250929",
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
      "input_tokens": 25,
      "output_tokens": 19
    },
    "type": "message",
    "role": "assistant"
  },
  "usage_metadata": {
    "input_tokens": 25,
    "output_tokens": 19,
    "total_tokens": 44
  }
}
```

资料来源：[libs/providers/langchain-anthropic/src/chat_models.ts:1-100]()

### AIMessageChunk

Used for streaming responses, representing incremental pieces of the complete response. Each chunk contains partial content that accumulates to form the full message.

```typescript
AIMessageChunk {
  "id": "msg_01N8MwoYxiKo9w4chE4gXUs4",
  "content": "Here",
  "additional_kwargs": {
    "id": "msg_01N8MwoYxiKo9w4chE4gXUs4",
    "type": "message",
    "role": "assistant",
    "model": "claude-sonnet-4-5-20250929"
  },
  "usage_metadata": {
    "input_tokens": 25,
    "output_tokens": 1,
    "total_tokens": 26
  }
}
```

资料来源：[libs/providers/langchain-anthropic/src/chat_models.ts:1-100]()

### Message Structure Types

The core message system defines flexible structures for tools and tool calls:

| Type | Purpose | Fields |
|------|---------|--------|
| `MessageToolDefinition` | Defines a tool's input/output schema | `input: TInput`, `output: TOutput` |
| `MessageToolSet` | Collection of available tools | `{ [key: string]: MessageToolDefinition }` |
| `MessageStructure` | Base structure for messages | Extends to include tools, tool calls |
| `$MessageToolCallBlock` | Tool call invocation | `type`, `name`, `args`, `id?` |

资料来源：[libs/langchain-core/src/messages/message.ts:1-100]()

## Invocation Patterns

### Single Invocation

The primary method for synchronous chat completion, accepting various input formats:

```typescript
const input = `Translate "I love programming" into French.`;

// Models accept string, list of messages, or formatted prompt
const result = await llm.invoke(input);
console.log(result);
```

资料来源：[libs/providers/langchain-openai/src/chat_models/index.ts:1-100]()

### Streaming

All chat models support streaming for real-time response generation:

```typescript
for await (const chunk of await llm.stream(input)) {
  console.log(chunk);
}
```

资料来源：[libs/providers/langchain-anthropic/src/chat_models.ts:1-100]()

## Tool Calling System

LangChain.js provides a unified tool calling abstraction that works across providers with provider-specific translations.

### Tool Format Translation

The OpenAI provider demonstrates how tools are translated to provider-specific formats:

```typescript
function convertToCustomTool(tool: FunctionDef & {
  description?: string;
  parameters?: JsonSchema;
}): IClient.Chat.ChatCompletionCustomTool {
  const getFormat = () => {
    if (!tool.format) {
      return undefined;
    }
    if (tool.format.type === "grammar") {
      return {
        type: "grammar" as const,
        grammar: {
          definition: tool.format.definition,
          syntax: tool.format.syntax,
        },
      };
    }
    if (tool.format.type === "text") {
      return {
        type: "text" as const,
      };
    }
    return undefined;
  };
  return {
    type: "custom",
    custom: {
      name: tool.name,
      description: tool.description,
      format: getFormat(),
    },
  };
}
```

资料来源：[libs/providers/langchain-openai/src/utils/tools.ts:1-50]()

### Block Translators

Each provider implements block translators for converting between internal message formats and provider-specific APIs:

```typescript
BlockTranslator = {
  translateContent: (message) => {
    if (typeof message.content === "string") {
      return convertToV1FromChatCompletions(message);
    }
    return convertToV1FromResponses(message);
  },
  translateContentChunk: (message) => {
    if (typeof message.content === "string") {
      return convertToV1FromChatCompletionsChunk(message);
    }
    return convertToV1FromResponsesChunk(message);
  },
};
```

资料来源：[libs/langchain-core/src/messages/block_translators/openai.ts:1-30]()

## Provider Implementations

### OpenAI

OpenAI's chat models use the Chat Completions API with comprehensive token usage tracking:

```typescript
AIMessage {
  "id": "chatcmpl-9u4Mpu44CbPjwYFkTbeoZgvzB00Tz",
  "content": "J'adore la programmation.",
  "response_metadata": {
    "tokenUsage": {
      "completionTokens": 5,
      "promptTokens": 28,
      "totalTokens": 33
    },
    "finish_reason": "stop",
    "system_fingerprint": "fp_3aa7262c27"
  },
  "usage_metadata": {
    "input_tokens": 28,
    "output_tokens": 5,
    "total_tokens": 33
  }
}
```

资料来源：[libs/providers/langchain-openai/src/chat_models/index.ts:1-100]()

### Anthropic

Anthropic's implementation includes cache control and extended reasoning features:

```typescript
// System messages support array content for cache control
const systemMessage = new SystemMessage([
  { type: "text", text: "You are a helpful assistant." },
  { type: "cache_control", ... }
]);
```

资料来源：[libs/providers/langchain-anthropic/src/chat_models.ts:1-100]()

### Ollama

Ollama provides local model hosting integration:

```typescript
import { ChatOllama } from "@langchain/ollama";

const model = new ChatOllama({
  model: "llama3", // Default value.
});

const result = await model.invoke(["human", "Hello, how are you?"]);
```

资料来源：[libs/providers/langchain-ollama/README.md:1-50]()

## Usage Metadata

All chat models track token usage consistently across providers:

| Metadata Field | Description | Availability |
|----------------|-------------|--------------|
| `input_tokens` | Tokens in the prompt | All providers |
| `output_tokens` | Tokens in the completion | All providers |
| `total_tokens` | Sum of input and output | All providers |
| `finish_reason` | Why generation stopped | OpenAI, Groq |
| `stop_reason` | Stop sequence trigger | Anthropic |
| `system_fingerprint` | Model version fingerprint | OpenAI |

## Message Content Utilities

### Standard Message Casting

The language models utility provides helpers for message handling:

```typescript
function castStandardMessageContent<T extends BaseMessage>(message: T) {
  const Cls = message.constructor as Constructor<T>;
  return new Cls({
    ...message,
    content: message.contentBlocks,
    response_metadata: {
      ...message.response_metadata,
      output_version: "v1",
    },
  });
}
```

资料来源：[libs/langchain-core/src/language_models/utils.ts:1-30]()

## Workflow: Stream Aggregation

For applications requiring the complete response after streaming:

```typescript
import { AIMessageChunk } from '@langchain/core/messages';
import { concat } from '@langchain/core/utils';

// Stream and accumulate chunks
const stream = await llm.stream(input);
let fullResponse = new AIMessageChunk({ content: "" });

for await (const chunk of stream) {
  fullResponse = concat(fullResponse, chunk);
}
```

资料来源：[libs/providers/langchain-openai/src/chat_models/index.ts:1-100]()

## Testing with Matchers

LangChain provides custom Jest matchers for testing chat model outputs:

```typescript
import { langchainMatchers } from '@langchain/core/testing/matchers';

expect.extend(langchainMatchers);

// Common matchers
expect(message).toBeAIMessage();
expect(message).toBeHumanMessage();
expect(message).toHaveToolCalls([{ name: 'calculator' }]);
expect(response).toHaveStructuredResponse({ type: 'json' });
```

资料来源：[libs/langchain-core/src/testing/matchers.ts:1-100]()

## Summary Table: Provider Capabilities

| Provider | Streaming | Tool Calling | Cache Control | Base URL Config |
|----------|-----------|--------------|---------------|-----------------|
| OpenAI | ✓ | ✓ | Via API | ✓ |
| Anthropic | ✓ | ✓ | ✓ | ✓ |
| Google | ✓ | ✓ | ✗ | ✓ |
| Mistral AI | ✓ | ✓ | ✗ | ✓ |
| Ollama | ✓ | ✓ | ✗ | Local |
| Deepseek | ✓ | ✓ | ✗ | ✓ |
| Groq | ✓ | ✓ | ✗ | ✓ |
| xAI | ✓ | ✓ | ✗ | ✓ |

## Common Configuration Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `model` | `string` | Model identifier (e.g., "gpt-4o", "claude-3-opus") |
| `temperature` | `number` | Sampling temperature (0.0 - 2.0) |
| `maxTokens` | `number` | Maximum tokens in response |
| `timeout` | `number` | Request timeout in milliseconds |
| `maxRetries` | `number` | Maximum retry attempts |

## See Also

- [Base Chat Models Documentation](../chat_models/base.md)
- [Tool Calling Guide](../tools/tool_calling.md)
- [Streaming Guide](../messages/streaming.md)
- [Message Types Reference](../messages/types.md)

---

<a id='embeddings'></a>

## Embeddings Integration

### 相关页面

相关主题：[Vector Stores](#vector-stores), [Chat Models and LLM Providers](#chat-models)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [libs/providers/langchain-openai/src/embeddings.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-openai/src/embeddings.ts)
- [libs/langchain-classic/src/vectorstores/memory.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/vectorstores/memory.ts)
- [libs/langchain-core/src/messages/block_translators/openai.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/messages/block_translators/openai.ts)
- [README.md](https://github.com/langchain-ai/langchainjs/blob/main/README.md)
- [libs/langchain-textsplitters/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-textsplitters/README.md)
</details>

# Embeddings Integration

## Overview

Embeddings integration in LangChain.js provides a standardized interface for converting text into dense vector representations suitable for machine learning tasks. These embeddings enable semantic search, similarity comparison, and retrieval-augmented generation (RAG) pipelines.

LangChain.js supports multiple embedding providers through a unified `Embeddings` base class, allowing developers to swap between providers like OpenAI, AWS Bedrock, Cohere, Google Gemini, and Mistral without changing application code.

资料来源：[README.md:1-15]()

## Architecture

### Class Hierarchy

```mermaid
graph TD
    A[Embeddings Base Class] --> B[OpenAIEmbeddings]
    A --> C[AWS Embeddings]
    A --> D[Cohere Embeddings]
    A --> E[Google GenAI Embeddings]
    A --> F[Google VertexAI Embeddings]
    A --> G[Fireworks Embeddings]
    A --> H[MistralAI Embeddings]
    
    B --> I[Vector Store Integration]
    C --> I
    D --> I
    E --> I
    F --> I
    G --> I
    H --> I
```

### Data Flow

```mermaid
graph LR
    A[Text Input] --> B[Embeddings.embedQuery]
    C[Document List] --> D[Embeddings.embedDocuments]
    B --> E[Vector Representation]
    D --> F[Batch Vector Output]
    E --> G[Vector Store]
    F --> G
    G --> H[Similarity Search]
    H --> I[Retrieved Results]
```

## OpenAI Embeddings

### Class Definition

The `OpenAIEmbeddings` class extends the base `Embeddings` class and implements the `Partial<OpenAIEmbeddingsParams>` interface. It provides access to OpenAI's embedding models including `text-embedding-ada-002` and the newer `text-embedding-3` series.

资料来源：[libs/providers/langchain-openai/src/embeddings.ts:1-50]()

### Configuration Parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `model` | `string` | `"text-embedding-ada-002"` | The embedding model to use |
| `batchSize` | `number` | `512` | Maximum documents per batch |
| `stripNewLines` | `boolean` | `true` | Remove newlines from input (deprecated) |
| `dimensions` | `number` | `undefined` | Output dimensions for text-embedding-3+ |
| `timeout` | `number` | `undefined` | Request timeout in milliseconds |
| `organization` | `string` | `undefined` | OpenAI organization ID |
| `encodingFormat` | `"float" \| "base64"` | `"float"` | Output encoding format |

资料来源：[libs/providers/langchain-openai/src/embeddings.ts:30-60]()

### Constructor Options

```typescript
constructor(
  fields?: Partial<OpenAIEmbeddingsParams> & {
    verbose?: boolean;
    openAIApiKey?: OpenAIApiKey;
    apiKey?: OpenAIApiKey;
    configuration?: ClientOptions;
  }
)
```

The constructor accepts API keys through `openAIApiKey` or `apiKey` properties, and allows custom client configuration for advanced use cases like proxy settings or custom endpoints.

资料来源：[libs/providers/langchain-openai/src/embeddings.ts:50-70]()

## Base Embeddings Interface

The `Embeddings` base class defines the contract that all embedding implementations must follow. It provides:

- `embedQuery(text: string)`: Embed a single text string
- `embedDocuments(texts: string[])`: Embed multiple documents in batches

The base implementation handles batching logic, ensuring that large document sets are processed efficiently within the configured `batchSize` limits.

## Integration with Vector Stores

### MemoryVectorStore Example

The `MemoryVectorStore` class demonstrates typical embeddings integration patterns. It stores documents and performs similarity search using embedded vectors.

资料来源：[libs/langchain-classic/src/vectorstores/memory.ts:1-60]()

### Usage Pattern

```typescript
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { OpenAIEmbeddings } from '@langchain/openai';

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
});

const vectorStore = new MemoryVectorStore(embeddings);

// Add documents
const documents = [
  { pageContent: "foo", metadata: { baz: "bar" } },
  { pageContent: "thud", metadata: { bar: "baz" } },
];

await vectorStore.addDocuments(documents);

// Search
const results = await vectorStore.similaritySearch("thud", 1);
```

资料来源：[libs/langchain-classic/src/vectorstores/memory.ts:30-55]()

## Provider Implementations

### Available Providers

| Provider | Package | Models |
|----------|---------|--------|
| OpenAI | `@langchain/openai` | text-embedding-ada-002, text-embedding-3-small, text-embedding-3-large |
| AWS | `@langchain/aws` | Amazon Titan, Cohere embeddings via Bedrock |
| Cohere | `@langchain/cohere` | embed-english-v3.0, embed-multilingual-v3.0 |
| Google GenAI | `@langchain/google-genai` | text-embedding-004 |
| Google VertexAI | `@langchain/google-vertexai` | textembedding-gecko |
| Fireworks | `@langchain/fireworks` | fireworks-embedding |
| MistralAI | `@langchain/mistralai` | mistral-embed |

### Common Features

All embedding providers support:

- Single text embedding via `embedQuery()`
- Batch embedding via `embedDocuments()` with automatic batching
- Custom dimensions for supported models
- Configurable timeouts and API keys
- Streaming-compatible response handling

## Batch Processing

### Automatic Batching

The embeddings system automatically handles batching when processing large document sets. The default batch size is 512 documents, but this can be configured per instance.

```typescript
const embeddings = new OpenAIEmbeddings({
  batchSize: 256, // Reduce for lower memory usage
});
```

### Batch Size Considerations

| Batch Size | Use Case |
|------------|----------|
| 512+ | High throughput, large documents |
| 256 | Balanced memory/throughput |
| 64-128 | Low memory environments |
| 1 | Streaming or real-time processing |

## Output Formats

### Float Arrays

The default output format returns embeddings as float32 arrays:

```typescript
const embedding = await embeddings.embedQuery("Hello world");
// Returns: number[]
```

### Base64 Encoding

For reduced payload sizes, use base64 encoding:

```typescript
const embeddings = new OpenAIEmbeddings({
  encodingFormat: "base64",
});
```

资料来源：[libs/providers/langchain-openai/src/embeddings.ts:45]()

## Error Handling

### Common Errors

| Error | Cause | Resolution |
|-------|-------|------------|
| `AuthenticationError` | Invalid API key | Verify API key configuration |
| `RateLimitError` | Too many requests | Implement retry with backoff |
| `TimeoutError` | Request exceeded timeout | Increase timeout or reduce batch size |
| `InvalidRequestError` | Invalid parameters | Check model name, dimensions, etc. |

## Best Practices

### Performance Optimization

1. **Batch Operations**: Always use `embedDocuments()` for multiple texts instead of calling `embedQuery()` in a loop
2. **Dimension Selection**: Use lower dimensions (256/512) when full precision isn't required
3. **Connection Pooling**: Reuse embedding instances across requests

### Security Considerations

1. Store API keys in environment variables, never in source code
2. Use `.env` files with appropriate access controls
3. Set up API key restrictions in provider dashboards

### Production Deployment

```typescript
// Recommended production configuration
const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
  batchSize: 512,
  dimensions: 1536,
  timeout: 60000,
});
```

## Related Components

### Text Splitters

Before embedding documents, use text splitters to break large texts into manageable chunks:

```typescript
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";

const splitter = RecursiveCharacterTextSplitter.fromLanguage("html", {
  chunkSize: 175,
  chunkOverlap: 20,
});

const docs = await splitter.createDocuments([htmlContent]);
const embeddings = await embeddings.embedDocuments(docs.map(d => d.pageContent));
```

资料来源：[examples/src/langchain-classic/indexes/html_text_splitter.ts:1-25]()
资料来源：[libs/langchain-textsplitters/README.md:1-30]()

### Message Translation

LangChain.js includes block translators for handling OpenAI message formats when working with embeddings in chat contexts:

```typescript
BlockTranslator = {
  translateContent: (message) => {
    if (typeof message.content === "string") {
      return convertToV1FromChatCompletions(message);
    }
    return convertToV1FromResponses(message);
  },
};
```

资料来源：[libs/langchain-core/src/messages/block_translators/openai.ts:1-20]()

## API Reference Summary

### OpenAIEmbeddings Constructor

```typescript
new OpenAIEmbeddings(fields?: Partial<OpenAIEmbeddingsParams>)
```

### Methods

| Method | Parameters | Returns | Description |
|--------|------------|---------|-------------|
| `embedQuery` | `text: string` | `Promise<number[]>` | Embed single text |
| `embedDocuments` | `texts: string[]` | `Promise<number[][]>` | Embed document batch |

### Environment Variables

| Variable | Description |
|----------|-------------|
| `OPENAI_API_KEY` | OpenAI API authentication key |

---

<a id='agent-framework'></a>

## Agent Framework

### 相关页面

相关主题：[Tools and Toolkits](#tools-toolkits), [Chat Models and LLM Providers](#chat-models)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [libs/langchain/src/agents/runtime.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/runtime.ts)
- [libs/langchain/src/agents/stream.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/stream.ts)
- [libs/langchain/src/agents/middleware.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/middleware.ts)
- [libs/langchain/src/agents/middleware/hitl.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/middleware/hitl.ts)
- [libs/langchain/src/agents/nodes/types.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/nodes/types.ts)
- [libs/langchain-classic/src/agents/react/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/react/index.ts)
- [libs/langchain-classic/src/agents/openai_functions/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/openai_functions/index.ts)
- [libs/langchain-classic/src/agents/openai_tools/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/openai_tools/index.ts)
- [libs/langchain-classic/src/agents/structured_chat/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/structured_chat/index.ts)
- [libs/langchain-classic/src/agents/format_scratchpad/xml.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/format_scratchpad/xml.ts)
</details>

# Agent Framework

The LangChain.js Agent Framework provides a comprehensive, modular system for building autonomous and semi-autonomous agents that can reason about tasks, utilize tools, and interact with external environments. The framework is designed around the concept of agents as stateful, middleware-extensible runtime systems that coordinate between large language models (LLMs), tools, and human interventions.

## Architecture Overview

The Agent Framework follows a layered architecture that separates concerns between agent definition, runtime execution, and middleware extension. At the core, agents are implemented as state machines that manage a series of steps, each potentially involving LLM calls, tool executions, and middleware hooks.

The architecture is built on several foundational components that work together to create a flexible agent system. The runtime layer manages the execution context, state transitions, and tool invocations, while the middleware layer provides hooks for observability, intervention, and customization at key points in the agent lifecycle.

The agent types themselves are modular, with different implementations optimized for various use cases—from classic ReAct-style reasoning agents to modern function-calling agents that leverage structured output capabilities from providers like OpenAI and Anthropic.

## Core Components

### Runtime System

The runtime system is the execution engine that orchestrates agent behavior. It maintains the agent state, manages step-by-step execution, handles tool calls, and integrates middleware for extensibility. The runtime provides a clean interface for starting, pausing, resuming, and terminating agent executions.

The runtime is responsible for managing the agent's internal memory and context across multiple steps, ensuring that observations from previous tool executions are properly passed back to the LLM for the next reasoning cycle. This cyclical pattern of reasoning, action, and observation forms the fundamental loop of agent execution.

```mermaid
graph TD
    A[User Input] --> B[Runtime Engine]
    B --> C[LLM Reasoning]
    C --> D{Tool Call?}
    D -->|Yes| E[Execute Tool]
    E --> F[Get Observation]
    F --> B
    D -->|No| G[Final Response]
    B --> H[Middleware Hooks]
    H --> I[Logging/Observability]
    H --> J[Human-in-the-Loop]
    H --> K[Custom Logic]
```

### Action Interface

Actions represent the fundamental unit of work that an agent can perform. The `Action` interface defines what an agent can request, including the action name and associated arguments:

```typescript
export interface Action {
  /**
   * The type or name of action being requested (e.g., "add_numbers").
   */
  name: string;
  /**
   * Key-value pairs of arguments needed for the action (e.g., {"a": 1, "b": 2}).
   */
  args: Record<string, any>;
}
```

This abstraction allows the framework to treat all agent actions uniformly, whether they represent tool invocations, control flow operations, or communication with external systems. The `args` field uses a flexible Record structure to accommodate any argument types required by different tools.

### Agent Nodes

The agent node system provides a structured way to define the behavior and capabilities of agents. Node types define the available operations, their parameters, and how they should be executed within the agent's runtime context.

The node type definitions establish a contract for what actions an agent can perform, ensuring type safety and providing documentation for available capabilities. Each node type can define its own schema for arguments, return types, and execution behavior.

## Middleware System

Middleware in the Agent Framework operates as a plugin system that intercepts and can modify agent behavior at defined points in the execution lifecycle. This pattern enables cross-cutting concerns like logging, monitoring, security, and human oversight without coupling these concerns to the core agent logic.

### Middleware Architecture

The middleware system follows a chain-of-responsibility pattern where each middleware component can inspect, modify, or terminate agent operations. Middleware is applied at key lifecycle points including before and after LLM calls, before and after tool executions, and at state transitions.

Middleware components receive context about the current operation, including the agent's state, the action being performed, and any intermediate results. This rich context enables sophisticated decision-making within middleware handlers.

### Human-in-the-Loop (HITL) Middleware

The HITL middleware provides a mechanism for human oversight and intervention in agent execution. This is particularly valuable for production systems where agent actions need validation before execution, or where humans should have the ability to redirect agent behavior.

The HITL middleware is configured through the `InterruptOnConfig` interface:

```typescript
export const InterruptOnConfigSchema = z.object({
  /**
   * When true, interrupts execution before the action runs.
   * When false, interrupts after the action runs.
   */
  interruptBefore: z.boolean().optional(),
  /**
   * When true, waits for human approval before continuing.
   * When false, triggers the interrupt but allows automatic continuation.
   */
  waitForApproval: z.boolean().optional(),
  /**
   * List of action names that should trigger interrupts.
   * If undefined, all actions trigger interrupts.
   */
  allowedDecisions: z.array(z.string()).optional(),
  /**
   * Dynamic callable description
   */
  description: z.union([z.string(), DescriptionFunctionSchema]).optional(),
  /**
   * JSON schema for the arguments associated with the action, if edits are allowed.
   */
  argsSchema: z.record(z.any()).optional(),
});
```

The configuration supports several operation modes. When `interruptBefore` is true, execution pauses before the action runs, allowing review of the planned action. When false, the action executes first and then interrupts, enabling review of both the action and its results. The `allowedDecisions` array restricts which actions trigger interrupts, allowing fine-grained control over when human oversight is required.

The `description` field accepts either a static string or a dynamic function that generates descriptions at runtime. This is useful for providing contextual information about actions:

```typescript
const formatToolDescription: DescriptionFactory = (
  toolCall: ToolCall,
  state: AgentBuiltInState,
  runtime: Runtime<unknown>
) => {
  return `Tool: ${toolCall.name}\nArguments:\n${JSON.stringify(toolCall.args, null, 2)}`;
};

const config: InterruptOnConfig = {
  allowedDecisions: ["approve", "edit"],
  description: formatToolDescription
};
```

## Agent Types

The Agent Framework supports multiple agent implementations, each optimized for different reasoning patterns and use cases.

### ReAct Agent

The ReAct (Reasoning + Acting) agent implements the classic ReAct pattern where the agent alternates between reasoning steps and action execution. This agent type is particularly effective for tasks requiring multi-step reasoning with tool usage.

The ReAct agent formats its reasoning in a structured scratchpad that combines observations, thoughts, and actions. The XML formatter provides a standardized way to represent this reasoning trace:

```typescript
export function formatXml(intermediateSteps: AgentStep[]) {
  let log = "";
  for (const step of intermediateSteps) {
    const { action, observation } = step;
    log += `<tool>${action.tool}</tool><tool_input>${action.toolInput}\n</tool_input><observation>${observation}</observation>`;
  }
  return log;
}
```

This XML format enables the LLM to easily parse and reason about previous actions and their outcomes, maintaining a clear trace of the agent's reasoning chain.

### OpenAI Functions Agent

The OpenAI Functions agent leverages OpenAI's function calling capability to constrain agent outputs to a specific set of tool definitions. This approach provides more reliable tool selection compared to open-ended generation, as the LLM must choose from the provided function schemas.

The agent passes tool definitions directly to the OpenAI API, which returns structured function calls. This eliminates the need for parsing unstructured text and reduces the likelihood of malformed tool invocations.

### OpenAI Tools Agent

The OpenAI Tools agent is an evolution of the functions agent that uses a more flexible tool definition format. It supports both the traditional function calling interface and newer tool-use patterns, providing compatibility across different OpenAI model versions.

This agent type is recommended for new projects using OpenAI models, as it supports the latest tool-use capabilities and provides better compatibility with models that have tool-use fine-tuning.

### Structured Chat Agent

The Structured Chat agent uses structured output parsing to extract tool calls from LLM responses. This approach works with any LLM that supports structured output, not just providers with native function calling support.

The agent defines a JSON schema for valid actions and uses parsing techniques to extract tool calls from the text response. This provides flexibility in model choice while maintaining reliable tool usage.

## Event System

The agent framework includes a comprehensive event system for monitoring and debugging agent execution. Events are emitted at key points in the agent lifecycle, allowing external systems to observe and respond to agent behavior.

| Event Type | Description | Common Use Cases |
|------------|-------------|------------------|
| `content-block-delta` | Emitted during streaming when content blocks are being updated | Real-time UI updates, partial result handling |
| `content-block-finish` | Emitted when a content block is complete | Result aggregation, final processing |
| `usage` | Emitted when usage information is updated | Cost tracking, monitoring, rate limiting |
| `provider` | Passthrough for provider-specific events | Integration with provider SDKs, custom logging |

The event system is designed to work seamlessly with streaming responses, where content may arrive incrementally. Events include positional indices and delta information that enable proper reconstruction of complete responses.

## Streaming Support

Agents support streaming responses through the runtime's streaming capabilities. Streaming is particularly valuable for long-running agent tasks where users benefit from seeing partial results as they become available.

The streaming implementation works by emitting events as the agent produces output. These events can be consumed incrementally, allowing applications to display results in real-time without waiting for complete agent execution.

## Configuration and Integration

### Tool Configuration

Tools are passed to agents at initialization and define the actions available to the agent. Each tool has a name, description, and schema that describes its arguments. The schema is typically defined using Zod for runtime type validation.

```typescript
const getWeather = tool(
  async (input) => `Weather in ${input.location}`,
  {
    name: "get_weather",
    description: "Get weather for a location",
    schema: z.object({ location: z.string() }),
  }
);
```

### Provider Configuration

Agents can be configured with any LLM provider that LangChain.js supports. The provider configuration includes API keys, model selection, and provider-specific parameters:

```typescript
import { ChatAnthropicMessages } from "@langchain/langchain-anthropic";
import { ChatOpenAI } from "@langchain/langchain-openai";

const anthropicAgent = createAgent({
  llm: new ChatAnthropicMessages({ model: "claude-sonnet-4-5-20250929" }),
  tools: [getWeather, searchTool],
});

const openaiAgent = createAgent({
  llm: new ChatOpenAI({ model: "gpt-4" }),
  tools: [getWeather, searchTool],
});
```

## Migration from Legacy Agents

The `@langchain/classic` package provides backward compatibility for legacy agent types that were part of LangChain v0.x. These include `LLMChain`, `ConversationalRetrievalQAChain`, and `RetrievalQAChain`.

For new projects, the recommended approach is to use the new `createAgent` API from the main `langchain` package. This provides better performance, cleaner abstractions, and access to middleware capabilities that are not available in the legacy agents.

The legacy agents will continue to be supported but will not receive new features. Applications should plan to migrate to the new API over time, taking advantage of improvements in the agent framework.

## Best Practices

When building agents with the LangChain.js Agent Framework, several practices help ensure reliable and maintainable implementations. Tool definitions should include clear, descriptive names and comprehensive descriptions that help the LLM understand when and how to use each tool. Argument schemas should validate inputs and provide clear error messages when invalid arguments are passed.

Middleware should be used judiciously to avoid performance impacts. For high-volume production systems, consider which middleware operations can be performed asynchronously or in batch mode. Human-in-the-loop configurations should be tested thoroughly to ensure the interrupt flow matches expected user interactions.

Agent state should be monitored and logged appropriately. The event system provides hooks for integrating with observability platforms, enabling debugging and performance analysis of agent behavior in production environments.

---

资料来源：[libs/langchain/src/agents/middleware/hitl.ts:35-52](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/middleware/hitl.ts)
资料来源：[libs/langchain/src/agents/middleware/hitl.ts:56-69](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain/src/agents/middleware/hitl.ts)
资料来源：[libs/langchain-classic/src/agents/format_scratchpad/xml.ts:3-12](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/format_scratchpad/xml.ts)
资料来源：[libs/langchain-classic/README.md:1-45](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/README.md)
资料来源：[libs/langchain-core/src/language_models/event.ts:1-60](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-core/src/language_models/event.ts)

---

<a id='tools-toolkits'></a>

## Tools and Toolkits

### 相关页面

相关主题：[Agent Framework](#agent-framework), [Memory Systems](#memory-system)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [libs/langchain-classic/src/tools/base.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/tools/base.ts)
- [libs/langchain-classic/src/tools/dynamic.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/tools/dynamic.ts)
- [libs/langchain-classic/src/tools/sql.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/tools/sql.ts)
- [libs/langchain-classic/src/tools/webbrowser.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/tools/webbrowser.ts)
- [libs/langchain-classic/src/agents/toolkits/sql/sql.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/toolkits/sql/sql.ts)
- [libs/langchain-classic/src/agents/toolkits/openapi/openapi.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/toolkits/openapi/openapi.ts)
- [libs/langchain-classic/src/agents/toolkits/vectorstore/vectorstore.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/agents/toolkits/vectorstore/vectorstore.ts)
- [libs/providers/langchain-openai/src/tools/index.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-openai/src/tools/index.ts)
</details>

# Tools and Toolkits

## Overview

In LangChain.js, **Tools** and **Toolkits** form the fundamental building blocks that enable Large Language Models (LLMs) to interact with external systems, APIs, databases, and web resources. Tools provide a standardized interface for defining callable functions that models can invoke during their reasoning process, while Toolkits bundle collections of related tools into cohesive units optimized for specific use cases.

The architecture follows a clear separation between individual tool implementations and composed toolkit aggregations, allowing developers to use built-in tools, create custom tools, or assemble pre-built toolkits for common workflows.

## Core Concepts

### What Are Tools?

Tools in LangChain.js are essentially callable functions with structured metadata that enables LLMs to understand when and how to use them. Each tool consists of:

| Component | Description |
|-----------|-------------|
| **Name** | Unique identifier for the tool |
| **Description** | Human-readable explanation of tool purpose |
| **Input Schema** | Zod schema defining acceptable parameters |
| **Function** | Actual implementation that executes the tool logic |

### What Are Toolkits?

Toolkits are pre-configured collections of related tools designed to work together for specific domains or workflows. They provide:

- Optimized tool combinations for common tasks
- Shared initialization logic and configuration
- Coordinated error handling and retry strategies
- Consistent authentication patterns where applicable

## Tool Architecture

### Base Tool Implementation

The foundational tool abstraction in LangChain.js is defined in `libs/langchain-classic/src/tools/base.ts`. All custom and built-in tools inherit from or conform to this interface.

```mermaid
graph TD
    A[BaseTool Abstract Class] --> B[StructuredTool]
    A --> C[Tool Interface]
    B --> D[Custom Tool Implementations]
    C --> E[Dynamic Tool]
    D --> F[SQL Tool]
    D --> G[Web Browser Tool]
```

### Tool Creation Patterns

LangChain.js supports multiple patterns for creating tools:

#### 1. Function-Based Tool Definition

The `tool()` function from `@langchain/core/tools` provides a concise API for defining tools:

```typescript
import { tool } from "@langchain/core/tools";
import { z } from "zod";

const getWeather = tool(
  async (input) => `Weather in ${input.location}`,
  {
    name: "get_weather",
    description: "Get weather for a location",
    schema: z.object({ location: z.string() }),
  }
);
```

#### 2. Dynamic Tool Creation

Dynamic tools allow runtime tool creation, useful for scenarios where tool definitions are not known at compile time. The implementation in `libs/langchain-classic/src/tools/dynamic.ts` provides the `DynamicTool` class.

资料来源：[libs/langchain-classic/src/tools/dynamic.ts:1-50]()

#### 3. Tool with Extra Parameters

Tools can include additional configuration through the `extras` field:

```typescript
const deferredTool = tool(
  async (input) => `Result: ${input.query}`,
  {
    name: "deferred_search",
    description: "Search tool with deferred loading",
    schema: z.object({ query: z.string() }),
    extras: { defer_loading: true },
  }
);
```

资料来源：[libs/providers/langchain-openai/src/tools/index.ts:1-30]()

## Built-in Tools

### SQL Tool

The SQL Tool enables LLM agents to query relational databases using natural language. It accepts raw SQL queries and returns structured results.

| Property | Type | Description |
|----------|------|-------------|
| `db` | Database connection | Database instance to query |
| `customQueryFunction` | Function | Optional custom query handler |
| `ignoreInsertOnly` | boolean | Skip INSERT operations |

资料来源：[libs/langchain-classic/src/tools/sql.ts:1-80]()

### Web Browser Tool

The Web Browser Tool allows agents to navigate websites, extract content, and interact with web resources. It simulates browser operations including page navigation, content extraction, and element interaction.

```mermaid
graph LR
    A[User Query] --> B[WebBrowserTool]
    B --> C{Action Type}
    C -->|navigate| D[Load URL]
    C -->|extract| E[Parse Content]
    C -->|interact| F[Click/Type]
    D --> G[HTML Response]
    E --> H[Extracted Data]
    F --> I[Interaction Result]
```

资料来源：[libs/langchain-classic/src/tools/webbrowser.ts:1-100]()

## Toolkits

### SQL Toolkit

The SQL Toolkit bundles tools for database operations, including query execution, table inspection, and schema understanding. Located at `libs/langchain-classic/src/agents/toolkits/sql/sql.ts`, it provides:

| Tool | Purpose |
|------|---------|
| `info` | Describe table schema |
| `tables` | List available tables |
| `execute` | Run SQL queries |
| `query_checker` | Validate SQL syntax |

资料来源：[libs/langchain-classic/src/agents/toolkits/sql/sql.ts:1-150]()

### OpenAPI Toolkit

The OpenAPI Toolkit enables agents to interact with REST APIs defined in OpenAPI specifications. It parses OpenAPI documents and generates corresponding tools.

```mermaid
graph TD
    A[OpenAPI Spec] --> B[OpenAPIToolkit]
    B --> C[HTTP GET Tool]
    B --> D[HTTP POST Tool]
    B --> E[HTTP PUT Tool]
    B --> F[HTTP DELETE Tool]
    C --> G[API Response]
    D --> G
    E --> G
    F --> G
```

资料来源：[libs/langchain-classic/src/agents/toolkits/openapi/openapi.ts:1-120]()

### Vector Store Toolkit

The Vector Store Toolkit provides tools for similarity search and document retrieval from vector databases. This is commonly used in Retrieval Augmented Generation (RAG) pipelines.

| Tool | Function |
|------|----------|
| ` similarity_search` | Find similar documents |
| ` similarity_search_with_score` | Search with relevance scores |
| ` similarity_search_by_vector` | Search using embedding vectors |

资料来源：[libs/langchain-classic/src/agents/toolkits/vectorstore/vectorstore.ts:1-100]()

## Integration with Agents

Tools and Toolkits are primarily consumed by LangChain agents. The agent runtime coordinates tool selection, execution, and result handling.

```mermaid
graph TD
    A[User Input] --> B[Agent]
    B --> C[LLM Reasoning]
    C --> D{Tool Call?}
    D -->|Yes| E[Select Tool]
    E --> F[Execute Tool]
    F --> G[Return Result]
    G --> C
    D -->|No| H[Final Response]
```

### Tool Calling Configuration

Agents can be configured to use tools with various parameters:

```typescript
const config: InterruptOnConfig = {
  allowedDecisions: ["approve", "edit"],
  description: formatToolDescription
};
```

资料来源：[libs/langchain/src/agents/middleware/hitl.ts:1-50]()

## Best Practices

### Tool Design Guidelines

1. **Clear Descriptions**: Write unambiguous tool descriptions that help the LLM understand when to invoke the tool
2. **Strict Schemas**: Use Zod schemas to validate inputs and prevent errors
3. **Error Handling**: Return meaningful error messages that guide the agent toward recovery
4. **Idempotency**: Design tools to be safely re-callable when needed
5. **Resource Management**: Properly dispose of connections and resources after tool execution

### Toolkit Organization

| Consideration | Recommendation |
|---------------|----------------|
| Scope | Bundle tools that share authentication or context |
| Cohesion | Tools in a toolkit should serve a common goal |
| Size | Avoid overly large toolkits; prefer composition |
| Documentation | Document tool dependencies and initialization |

## Conclusion

Tools and Toolkits in LangChain.js provide a powerful abstraction for extending LLM capabilities beyond text generation. By leveraging the standardized tool interface, developers can create reusable components that integrate seamlessly with agent workflows, enabling sophisticated AI applications that interact with databases, APIs, web resources, and more.

---

<a id='vector-stores'></a>

## Vector Stores

### 相关页面

相关主题：[Embeddings Integration](#embeddings)

<details>
<summary>相关源码文件</summary>

以下源码文件用于生成本页说明：

- [libs/providers/langchain-pinecone/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-pinecone/src/vectorstores.ts)
- [libs/providers/langchain-qdrant/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-qdrant/src/vectorstores.ts)
- [libs/providers/langchain-weaviate/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-weaviate/src/vectorstores.ts)
- [libs/providers/langchain-redis/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-redis/src/vectorstores.ts)
- [libs/providers/langchain-mongodb/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-mongodb/src/vectorstores.ts)
- [libs/providers/langchain-neo4j/src/vectorstores/neo4j_vector.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-neo4j/src/vectorstores/neo4j_vector.ts)
- [libs/providers/langchain-pgvector/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-pgvector/src/vectorstores.ts)
- [libs/langchain-classic/src/vectorstores/memory.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/vectorstores/memory.ts)
</details>

# Vector Stores

Vector stores are a core component in LangChain.js that enable efficient similarity search over embedded data. They serve as the persistence layer for vector embeddings in retrieval-augmented generation (RAG) pipelines, allowing developers to store, index, and query high-dimensional vectors representing documents, text chunks, or other data.

## Overview

LangChain.js provides a unified interface for interacting with multiple vector database backends through the `@langchain/core` package. This abstraction enables developers to switch between different vector store implementations without changing their application code.

```mermaid
graph TD
    A[Documents] --> B[Text Embedding Models]
    B --> C[Vector Embeddings]
    C --> D[Vector Store]
    E[Query] --> F[Embed Query]
    F --> G[Similarity Search]
    G --> H[Retrieved Documents]
    
    D --> I[Pinecone]
    D --> J[Qdrant]
    D --> K[Weaviate]
    D --> L[Redis]
    D --> M[MongoDB]
    D --> N[Neo4j]
    D --> O[PGVector]
    D --> P[Memory]
```

## Supported Vector Stores

LangChain.js integrates with the following vector database providers:

| Provider | Package | Key Features |
|----------|---------|--------------|
| Pinecone | `@langchain/pinecone` | Managed cloud service, serverless indexes |
| Qdrant | `@langchain/qdrant` | Open-source, hybrid filtering |
| Weaviate | `@langchain/weaviate` | GraphQL API, modular architecture |
| Redis | `@langchain/redis` | In-memory, pub/sub capabilities |
| MongoDB | `@langchain/mongodb` | Document-based, Atlas vector search |
| Neo4j | `@langchain/neo4j` | Graph database, knowledge graphs |
| PGVector | `@langchain/pgvector` | PostgreSQL extension, SQL compatibility |
| Memory | `@langchain/classic` | In-memory, no external dependencies |

资料来源：[libs/providers/langchain-pinecone/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-pinecone/README.md)
资料来源：[libs/providers/langchain-qdrant/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-qdrant/README.md)

## Core Interface

All vector stores in LangChain.js implement a common interface defined in `@langchain/core/vectorstores`. This interface ensures consistency across different implementations while allowing provider-specific features.

### Key Methods

| Method | Description |
|--------|-------------|
| `addDocuments()` | Add documents with embeddings to the store |
| `similaritySearch()` | Find similar documents using cosine similarity |
| `similaritySearchVectorWithScore()` | Search with relevance scores |
| `delete()` | Remove documents by IDs |
| `fromTexts()` | Static factory for text-based initialization |

资料来源：[libs/langchain-classic/src/vectorstores/memory.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/vectorstores/memory.ts)

## Usage Patterns

### Basic Similarity Search

The following example demonstrates the typical workflow for performing similarity search using the `MemoryVectorStore`:

```typescript
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { OpenAIEmbeddings } from '@langchain/openai';

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
});

const vectorStore = new MemoryVectorStore(embeddings);

// Add documents
const document1 = { pageContent: "foo", metadata: { baz: "bar" } };
const document2 = { pageContent: "thud", metadata: { bar: "baz" } };
const documents = [document1, document2];

await vectorStore.addDocuments(documents);

// Perform similarity search
const results = await vectorStore.similaritySearch("thud", 1);

for (const doc of results) {
  console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
```

资料来源：[libs/langchain-classic/src/vectorstores/memory.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-classic/src/vectorstores/memory.ts)

### Installation

Each vector store package has specific installation requirements:

#### Pinecone

```bash
npm install @langchain/pinecone @langchain/core @pinecone-database/pinecone
```

资料来源：[libs/providers/langchain-pinecone/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-pinecone/README.md)

#### Qdrant

```bash
npm install @langchain/qdrant
```

资料来源：[libs/providers/langchain-qdrant/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-qdrant/README.md)

#### PGVector

The PostgreSQL vector extension requires a running PostgreSQL instance with `pgvector` installed:

```bash
npm install @langchain/pgvector pg
```

## Architecture

### Document Model

LangChain.js uses a standardized `Document` interface for representing content:

```typescript
interface Document {
  pageContent: string;    // The text content
  metadata: Record<string, any>;  // Associated metadata
}
```

Documents are processed through embedding models to generate vector representations before storage.

### Embedding Integration

Vector stores work in conjunction with embedding models from `@langchain/core` or provider-specific embedding implementations. The embedding model is passed during vector store initialization and is used to:

1. Generate vectors for incoming documents during `addDocuments()`
2. Generate vectors for query strings during search operations

```mermaid
sequenceDiagram
    participant App as Application
    participant VS as Vector Store
    participant EM as Embedding Model
    participant DB as Database
    
    App->>VS: addDocuments(documents)
    VS->>EM: embedDocuments(documents)
    EM-->>VS: embeddings
    VS->>DB: store(embeddings + docs)
    
    App->>VS: similaritySearch(query)
    VS->>EM: embedQuery(query)
    EM-->>VS: queryVector
    VS->>DB: findSimilar(queryVector)
    DB-->>VS: results
    VS-->>App: documents
```

## Provider-Specific Implementations

### Neo4j Vector Store

The Neo4j integration leverages graph database capabilities for vector storage, enabling hybrid queries that combine vector similarity with graph traversal:

```typescript
import { Neo4jVectorStore } from '@langchain/neo4j';
```

资料来源：[libs/providers/langchain-neo4j/src/vectorstores/neo4j_vector.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-neo4j/src/vectorstores/neo4j_vector.ts)

### Weaviate

Weaviate provides hybrid search capabilities combining vector similarity with keyword matching:

```typescript
import { WeaviateVectorStore } from '@langchain/weaviate';
```

资料来源：[libs/providers/langchain-weaviate/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-weaviate/src/vectorstores.ts)

### Redis

Redis vector store offers high-performance in-memory vector operations with built-in pub/sub capabilities:

```typescript
import { RedisVectorStore } from '@langchain/redis';
```

资料来源：[libs/providers/langchain-redis/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-redis/src/vectorstores.ts)

### MongoDB Atlas

MongoDB Atlas provides vector search through its Atlas Search feature, integrated via the official MongoDB driver:

```typescript
import { MongoDBAtlasVectorSearch } from '@langchain/mongodb';
```

资料来源：[libs/providers/langchain-mongodb/src/vectorstores.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/providers/langchain-mongodb/src/vectorstores.ts)

## Development Guidelines

### Adding New Vector Stores

LangChain.js provides a template for creating new vector store integrations. See the official integration template for the required structure:

资料来源：[libs/create-langchain-integration/template/README.md](https://github.com/langchain-ai/langchainjs/blob/main/libs/create-langchain-integration/template/README.md)

### Package Structure

Each vector store package should follow the LangChain.js monorepo conventions:

| Directory/File | Purpose |
|---------------|---------|
| `src/index.ts` | Main exports |
| `src/vectorstores.ts` | Vector store implementation |
| `tests/` | Unit and integration tests |
| `README.md` | Documentation and usage examples |

### Testing

Vector store implementations require both unit tests (`.test.ts`) and integration tests (`.int.test.ts`):

```bash
pnpm test
pnpm test:int
```

## Common Configuration Options

While specific options vary by provider, most vector stores support these common parameters:

| Parameter | Type | Description |
|-----------|------|-------------|
| `embeddings` | `Embeddings` | The embedding model to use |
| `textKey` | `string` | Field name for text content |
| `embeddingKey` | `string` | Field name for embeddings |
| `namespace` | `string` | Optional namespace isolation |
| `filter` | `Record<string, any>` | Metadata filtering criteria |

## Best Practices

1. **Embedding Model Consistency**: Always use the same embedding model for indexing and querying to ensure vector compatibility.

2. **Metadata Indexing**: Include relevant metadata during document storage to enable filtered searches.

3. **Batch Operations**: Use batch operations (`addDocuments`) rather than single document additions for better performance.

4. **Connection Management**: Follow provider-specific connection pooling and timeout configurations in production environments.

5. **Dimension Matching**: Ensure your embedding model dimensions match the vector store's expected configuration.

## Related Components

Vector stores are typically used in combination with:

- **Document Loaders**: Load documents from various sources
- **Text Splitters**: Split large documents into searchable chunks
- **Retrieval Chains**: Combine vector stores with language models for RAG
- **Memory Systems**: Persist conversation context using vector storage

---

<a id='memory-system'></a>

## Memory Systems

### 相关页面

相关主题：[Agent Framework](#agent-framework)

I cannot generate an accurate technical wiki page about Memory Systems because the provided repository context does not contain the actual source files for memory implementations.

The requested source files are not present in the retrieved context:

- `libs/langchain-classic/src/memory/buffer_memory.ts`
- `libs/langchain-classic/src/memory/buffer_window_memory.ts`
- `libs/langchain-classic/src/memory/summary_buffer.ts`
- `libs/langchain-classic/src/memory/entity_memory.ts`
- `libs/langchain-classic/src/memory/vector_store.ts`
- `libs/langchain-core/src/memory.ts`
- `libs/langchain-core/src/chat_history.ts`

The context only contains files related to:

- HTML text splitting
- Agent HITL middleware
- Anthropic/OpenAI chat models
- SHA256 hashing utilities
- Vector store memory (partial)
- Computer use tools
- Language model utilities
- Agent streaming

To generate an accurate wiki page, I would need access to the actual memory module source files.

---

---

## Doramagic 踩坑日志

项目：langchain-ai/langchainjs

摘要：发现 10 个潜在踩坑项，其中 1 个为 high/blocking；最高优先级：配置坑 - 来源证据：[Feature request] React Native support。

## 1. 配置坑 · 来源证据：[Feature request] React Native support

- 严重度：high
- 证据强度：source_linked
- 发现：GitHub 社区证据显示该项目存在一个配置相关的待验证问题：[Feature request] React Native support
- 对用户的影响：可能增加新用户试用和生产接入成本。
- 建议检查：来源问题仍为 open，Pack Agent 需要复核是否仍影响当前版本。
- 防护动作：不得脱离来源链接放大为确定性结论；需要标注适用版本和复核状态。
- 证据：community_evidence:github | cevd_39933028ef894033b30ff784e81f185f | https://github.com/langchain-ai/langchainjs/issues/4239 | 来源类型 github_issue 暴露的待验证使用条件。

## 2. 安装坑 · 来源证据：@langchain/core@1.1.46

- 严重度：medium
- 证据强度：source_linked
- 发现：GitHub 社区证据显示该项目存在一个安装相关的待验证问题：@langchain/core@1.1.46
- 对用户的影响：可能增加新用户试用和生产接入成本。
- 建议检查：来源显示可能已有修复、规避或版本变化，说明书中必须标注适用版本。
- 防护动作：不得脱离来源链接放大为确定性结论；需要标注适用版本和复核状态。
- 证据：community_evidence:github | cevd_36a7a58d5cd84bda8dde7918402a6f8a | https://github.com/langchain-ai/langchainjs/releases/tag/%40langchain/core%401.1.46 | 来源讨论提到 npm 相关条件，需在安装/试用前复核。

## 3. 配置坑 · 来源证据：Must pass in at least 1 record to upsert.

- 严重度：medium
- 证据强度：source_linked
- 发现：GitHub 社区证据显示该项目存在一个配置相关的待验证问题：Must pass in at least 1 record to upsert.
- 对用户的影响：可能增加新用户试用和生产接入成本。
- 建议检查：来源问题仍为 open，Pack Agent 需要复核是否仍影响当前版本。
- 防护动作：不得脱离来源链接放大为确定性结论；需要标注适用版本和复核状态。
- 证据：community_evidence:github | cevd_26c3acaad9e14ed3953206f25870c0b0 | https://github.com/langchain-ai/langchainjs/issues/10890 | 来源讨论提到 node 相关条件，需在安装/试用前复核。

## 4. 能力坑 · 能力判断依赖假设

- 严重度：medium
- 证据强度：source_linked
- 发现：README/documentation is current enough for a first validation pass.
- 对用户的影响：假设不成立时，用户拿不到承诺的能力。
- 建议检查：将假设转成下游验证清单。
- 防护动作：假设必须转成验证项；没有验证结果前不能写成事实。
- 证据：capability.assumptions | github_repo:598342280 | https://github.com/langchain-ai/langchainjs | README/documentation is current enough for a first validation pass.

## 5. 维护坑 · 维护活跃度未知

- 严重度：medium
- 证据强度：source_linked
- 发现：未记录 last_activity_observed。
- 对用户的影响：新项目、停更项目和活跃项目会被混在一起，推荐信任度下降。
- 建议检查：补 GitHub 最近 commit、release、issue/PR 响应信号。
- 防护动作：维护活跃度未知时，推荐强度不能标为高信任。
- 证据：evidence.maintainer_signals | github_repo:598342280 | https://github.com/langchain-ai/langchainjs | last_activity_observed missing

## 6. 安全/权限坑 · 下游验证发现风险项

- 严重度：medium
- 证据强度：source_linked
- 发现：no_demo
- 对用户的影响：下游已经要求复核，不能在页面中弱化。
- 建议检查：进入安全/权限治理复核队列。
- 防护动作：下游风险存在时必须保持 review/recommendation 降级。
- 证据：downstream_validation.risk_items | github_repo:598342280 | https://github.com/langchain-ai/langchainjs | no_demo; severity=medium

## 7. 安全/权限坑 · 存在评分风险

- 严重度：medium
- 证据强度：source_linked
- 发现：no_demo
- 对用户的影响：风险会影响是否适合普通用户安装。
- 建议检查：把风险写入边界卡，并确认是否需要人工复核。
- 防护动作：评分风险必须进入边界卡，不能只作为内部分数。
- 证据：risks.scoring_risks | github_repo:598342280 | https://github.com/langchain-ai/langchainjs | no_demo; severity=medium

## 8. 安全/权限坑 · 来源证据：bug(@langchain/openai): Bare JSON.parse in Responses API converter crashes on structured output with trailing characters

- 严重度：medium
- 证据强度：source_linked
- 发现：GitHub 社区证据显示该项目存在一个安全/权限相关的待验证问题：bug(@langchain/openai): Bare JSON.parse in Responses API converter crashes on structured output with trailing characters
- 对用户的影响：可能阻塞安装或首次运行。
- 建议检查：来源问题仍为 open，Pack Agent 需要复核是否仍影响当前版本。
- 防护动作：不得脱离来源链接放大为确定性结论；需要标注适用版本和复核状态。
- 证据：community_evidence:github | cevd_4d2a6bed33284a3cbd2f3319321d9e4c | https://github.com/langchain-ai/langchainjs/issues/10894 | 来源讨论提到 node 相关条件，需在安装/试用前复核。

## 9. 维护坑 · issue/PR 响应质量未知

- 严重度：low
- 证据强度：source_linked
- 发现：issue_or_pr_quality=unknown。
- 对用户的影响：用户无法判断遇到问题后是否有人维护。
- 建议检查：抽样最近 issue/PR，判断是否长期无人处理。
- 防护动作：issue/PR 响应未知时，必须提示维护风险。
- 证据：evidence.maintainer_signals | github_repo:598342280 | https://github.com/langchain-ai/langchainjs | issue_or_pr_quality=unknown

## 10. 维护坑 · 发布节奏不明确

- 严重度：low
- 证据强度：source_linked
- 发现：release_recency=unknown。
- 对用户的影响：安装命令和文档可能落后于代码，用户踩坑概率升高。
- 建议检查：确认最近 release/tag 和 README 安装命令是否一致。
- 防护动作：发布节奏未知或过期时，安装说明必须标注可能漂移。
- 证据：evidence.maintainer_signals | github_repo:598342280 | https://github.com/langchain-ai/langchainjs | release_recency=unknown

<!-- canonical_name: langchain-ai/langchainjs; human_manual_source: deepwiki_human_wiki -->
