Doramagic Project Pack · Human Manual

langchain

LangChain is a comprehensive framework designed for building agents and LLM-powered applications. It enables developers to chain together interoperable components and third-party integrati...

Introduction to LangChain

Related topics: Runnable and Execution Model, Getting Started with LangChain

Section Related Pages

Continue reading this section for the full explanation and source context.

Section langchain-core

Continue reading this section for the full explanation and source context.

Section langchain-classic

Continue reading this section for the full explanation and source context.

Section LLM Provider Packages

Continue reading this section for the full explanation and source context.

Related topics: Runnable and Execution Model, Getting Started with LangChain

Introduction to LangChain

Overview

LangChain is a comprehensive framework designed for building agents and LLM-powered applications. It enables developers to chain together interoperable components and third-party integrations to simplify AI application development, while maintaining flexibility as underlying technologies evolve.

LangChain follows a modular architecture where core functionality is separated into specialized packages. The main langchain package serves as the primary interface for most use cases, with partner packages providing dedicated integrations for specific providers.

Architecture Overview

LangChain's architecture consists of several key layers working together to provide a seamless development experience:

graph TD
    A[Application Layer] --> B[langchain Package]
    B --> C[langchain-classic]
    B --> D[langchain-core]
    C --> E[Chains & Components]
    C --> F[Community Re-exports]
    D --> G[Core Abstractions]
    H[Partner Packages] --> B
    H --> I[OpenAI, Anthropic, HuggingFace, etc.]

Core Packages

langchain-core

The foundational package containing core abstractions and interfaces that all other LangChain packages depend on. This includes base classes for language models, prompts, callbacks, and other fundamental components.

langchain-classic

The classic LangChain package containing chains, langchain-community re-exports, indexing API, and deprecated functionality. In most cases, developers should use the main langchain package instead.

Partner Integrations

LangChain provides dedicated partner packages for various LLM providers and services. Each package is independently maintained and optimized for its specific provider.

LLM Provider Packages

PackageProviderPurposeInstall Command
langchain-openaiOpenAIOpenAI SDK integrationpip install langchain-openai
langchain-anthropicAnthropicClaude models integrationpip install langchain-anthropic
langchain-huggingfaceHugging FaceTransformers & embeddingspip install langchain-huggingface
langchain-ollamaOllamaLocal model inferencepip install langchain-ollama
langchain-deepseekDeepSeekDeepSeek API integrationpip install langchain-deepseek
langchain-groqGroqGroq API integrationpip install langchain-groq
langchain-fireworksFireworks.aiFireworks API integrationpip install langchain-fireworks
langchain-perplexityPerplexityPerplexity API integrationpip install langchain-perplexity
langchain-xaixAIxAI API integrationpip install langchain-xai
langchain-openrouterOpenRouterUnified multi-provider APIpip install langchain-openrouter

Vector Store & Search Packages

PackageProviderPurposeInstall Command
langchain-chromaChromaVector database integrationpip install langchain-chroma
langchain-exaExaWeb search API for AIpip install langchain-exa
langchain-nomicNomicEmbedding & visualizationpip install langchain-nomic

Quick Start

The simplest way to get started with LangChain is using the init_chat_model function for initializing chat models:

from langchain.chat_models import init_chat_model

model = init_chat_model("openai:gpt-5.4")
result = model.invoke("Hello, world!")

For more advanced customization or agent orchestration, LangChain recommends using LangGraph, their framework for building controllable agent workflows.

Installation Options

LangChain offers flexible installation options to suit different needs:

# Base installation
pip install langchain

# With specific integrations
pip install langchain-huggingface[full]

The [full] extra for packages like langchain-huggingface includes additional dependencies such as sentence-transformers>=5.2.0 and transformers>=5.0.0 for local inference capabilities.

Key Features

Interoperability

LangChain emphasizes interoperability through standardized interfaces. Components can be easily swapped and combined, allowing developers to:

  • Switch between different LLM providers without code changes
  • Combine multiple integrations in a single application
  • Future-proof applications against technology changes

Flexible Abstraction Layers

LangChain provides multiple levels of abstraction:

  • High-level chains: Quick starts for common patterns
  • Low-level components: Fine-grained control for advanced use cases

The framework grows with your application's complexity, allowing you to start simple and add sophistication as needed.

Security Considerations

When building applications with LangChain, security should be a primary concern. The framework includes built-in protections for components like API chains:

Security Note: The API chain uses the requests toolkit to make GET, POST, PATCH, PUT, and DELETE requests to an API. Exercise care in who is allowed to use this chain. Control access to who can submit requests and what network access it has.

Additional Resources

LangChain provides comprehensive documentation and support:

ResourceURL
Full Documentationdocs.langchain.com
API Referencereference.langchain.com/python
Community Forumforum.langchain.com
Chat with Docschat.langchain.com
LangChain Academyacademy.langchain.com
Contributing Guidedocs.langchain.com/contributing

For specialized use cases, LangChain offers additional projects:

  • LangGraph: Framework for building controllable agent workflows
  • Deep Agents: Higher-level package with built-in capabilities for planning, subagents, and file system usage
  • LangSmith: Platform for developing, debugging, and deploying AI agents
  • LangChain.js: JavaScript/TypeScript equivalent library

Ecosystem Diagram

graph LR
    A[Developers] --> B[langchain Package]
    B --> C[Core Abstractions]
    B --> D[Partner Integrations]
    D --> E[LLM Providers]
    D --> F[Vector Stores]
    D --> G[Search APIs]
    B --> H[LangGraph]
    B --> I[LangSmith]
    C --> J[Chat Models]
    C --> K[Embeddings]
    C --> L[Tools]
    C --> M[Memory]

LangChain continues to evolve with an active open-source community. Contributions are welcome in the form of new features, improved infrastructure, or better documentation.

Source: https://github.com/langchain-ai/langchain / Human Manual

Getting Started with LangChain

Related topics: Introduction to LangChain, Runnable and Execution Model

Section Related Pages

Continue reading this section for the full explanation and source context.

Section Base Installation

Continue reading this section for the full explanation and source context.

Section Integration Packages

Continue reading this section for the full explanation and source context.

Section Full Installation with Dependencies

Continue reading this section for the full explanation and source context.

Related topics: Introduction to LangChain, Runnable and Execution Model

Getting Started with LangChain

LangChain is a framework for building agents and LLM-powered applications. It enables developers to chain together interoperable components and third-party integrations to simplify AI application development while future-proofing decisions as the underlying technology evolves. Sources: README.md

Installation

Base Installation

The core LangChain package can be installed via pip or uv:

pip install langchain
# or
uv add langchain

Sources: README.md

Integration Packages

LangChain uses a modular architecture with partner-specific packages. Each integration is distributed as a separate package:

PackagePurposeInstall Command
langchain-openaiOpenAI models (GPT-4, GPT-5, etc.)pip install langchain-openai
langchain-anthropicAnthropic models (Claude)pip install langchain-anthropic
langchain-huggingfaceHugging Face modelspip install langchain-huggingface
langchain-ollamaOllama local modelspip install langchain-ollama
langchain-deepseekDeepSeek modelspip install langchain-deepseek
langchain-groqGroq modelspip install langchain-groq

Sources: libs/partners/openai/README.md, libs/partners/anthropic/README.md, libs/partners/ollama/README.md, libs/partners/deepseek/README.md, libs/partners/groq/README.md

Full Installation with Dependencies

For packages requiring additional dependencies like sentence-transformers or transformers:

pip install langchain-huggingface[full]
Note: The [full] extra includes sentence-transformers>=5.2.0 and transformers>=5.0.0. Sources: libs/partners/huggingface/README.md

Quick Start

Initializing a Chat Model

LangChain provides a unified interface for initializing chat models using init_chat_model:

from langchain.chat_models import init_chat_model

model = init_chat_model("openai:gpt-5.4")
result = model.invoke("Hello, world!")

The init_chat_model function supports model providers through a prefixed model string format (e.g., openai:gpt-5.4, anthropic:claude-3-opus). Sources: README.md

Supported Providers

LangChain integrates with multiple model providers:

Architecture Overview

LangChain provides flexible abstraction layers allowing developers to work at the level of abstraction that suits their needs:

graph TD
    A[LangChain Application] --> B[High-Level Chains]
    A --> C[Low-Level Components]
    B --> D[Agents]
    B --> E[Chains]
    C --> F[LLMs]
    C --> G[Prompts]
    C --> H[Tools]
    F --> I[Provider Integrations]
    I --> J[OpenAI]
    I --> K[Anthropic]
    I --> L[HuggingFace]
    I --> M[Ollama]
  • High-level chains - Quick starts for common patterns
  • Low-level components - Fine-grained control for custom implementations

Sources: README.md

Ecosystem

LangChain consists of multiple interconnected packages:

graph LR
    A[Core LangChain] --> B[langchain-core]
    A --> C[Partner Packages]
    A --> D[langchain-community]
    B --> E[Base abstractions]
    C --> F[Provider-specific]
    D --> G[Community integrations]

Package Structure

PackageDescription
langchain-coreCore abstractions and interfaces
langchainMain framework (chains, agents, core functionality)
langchain-communityCommunity-contributed integrations
Partner packagesProvider-specific integrations (openai, anthropic, etc.)

Documentation Resources

For comprehensive learning and reference:

ResourceURLPurpose
Main Docsdocs.langchain.comConceptual guides, overviews, tutorials
API Referencereference.langchain.com/pythonComplete API documentation
Chat LangChainchat.langchain.comInteractive documentation chat
LangChain Forumforum.langchain.comCommunity discussions
LangChain Academyacademy.langchain.comFree courses on LangChain

Sources: README.md

Advanced Usage

Building Agents

For advanced customization and agent orchestration, LangChain recommends using LangGraph, a framework for building controllable agent workflows. Sources: README.md

Development and Debugging

For developing, debugging, and deploying AI agents and LLM applications, LangSmith provides comprehensive tooling. Sources: README.md

Contributing

LangChain is an open-source project welcoming contributions in the form of new features, improved infrastructure, or better documentation. See the Contributing Guide for details on how to get involved. Sources: README.md, libs/partners/openai/README.md

Sources: [README.md](https://github.com/langchain-ai/langchain/blob/main/README.md)

Runnable and Execution Model

Related topics: Introduction to LangChain, Callbacks and Tracing Infrastructure, Agents Framework

Section Related Pages

Continue reading this section for the full explanation and source context.

Related topics: Introduction to LangChain, Callbacks and Tracing Infrastructure, Agents Framework

Runnable and Execution Model

Overview

The Runnable interface is the foundational abstraction in LangChain for creating composable, executable units of work. Every component in LangChain—including chains, models, tools, and utilities—implements the Runnable protocol, enabling uniform composition, execution, and error handling across the entire framework.

The execution model provides both synchronous and asynchronous invocation patterns, retry mechanisms, configurable runtime parameters, and conditional branching capabilities. This design allows developers to build complex AI applications by composing simple, reusable components into sophisticated pipelines.

Sources: libs/core/langchain_core/runnables/base.py

Sources: [libs/core/langchain_core/runnables/base.py](https://github.com/langchain-ai/langchain/blob/main/libs/core/langchain_core/runnables/base.py)

Messages and Prompt System

Related topics: Chat Models and Embeddings

Section Related Pages

Continue reading this section for the full explanation and source context.

Section Core Components

Continue reading this section for the full explanation and source context.

Section BaseCallbackHandler

Continue reading this section for the full explanation and source context.

Section CallbackManager

Continue reading this section for the full explanation and source context.

Related topics: Chat Models and Embeddings

I cannot generate an accurate technical wiki page about the "Messages and Prompt System" because the context provided does not contain the actual source code files listed in the `

Callbacks and Tracing Infrastructure

Overview

The LangChain callbacks and tracing infrastructure provides a unified system for observability, monitoring, and event propagation across LangChain components. This system enables developers to track LLM invocations, chain executions, tool usage, and custom events throughout the application lifecycle.

LangChain follows a monorepo structure where core primitives and abstractions are located in libs/core/langchain_core/, with callback and tracer implementations residing in dedicated modules under langchain_core.callbacks and langchain_core.tracers respectively. Sources: libs/README.md

Architecture Overview

graph TD
    A[Application Code] --> B[CallbackManager]
    B --> C[BaseCallbackHandler]
    C --> D[Tracer Implementations]
    C --> E[Custom Handlers]
    D --> F[LangSmith]
    D --> G[Other Exporters]
    B --> H[AsyncCallbackManager]
    H --> C
    style A fill:#e1f5fe
    style F fill:#c8e6c9
    style G fill:#c8e6c9

Callback System

Core Components

The callback system in LangChain is built around the following key abstractions:

ComponentFile LocationPurpose
BaseCallbackHandlerlibs/core/langchain_core/callbacks/base.pyAbstract base class for all callback handlers
CallbackManagerlibs/core/langchain_core/callbacks/manager.pyManages synchronous callback execution
AsyncCallbackManagerlibs/core/langchain_core/callbacks/manager.pyManages asynchronous callback execution
Callbackslibs/core/langchain_core/callbacks/__init__.pyPublic API exports

BaseCallbackHandler

The BaseCallbackHandler class defines the contract for all callback implementations. It provides methods for intercepting various events during chain execution:

# Conceptual structure
class BaseCallbackHandler:
    """Base class for callback handlers."""
    
    def on_llm_start(self, ...):
        """Called when LLM starts processing."""
        pass
    
    def on_llm_end(self, ...):
        """Called when LLM finishes processing."""
        pass
    
    def on_chain_start(self, ...):
        """Called when a chain starts execution."""
        pass
    
    def on_chain_end(self, ...):
        """Called when a chain finishes execution."""
        pass
    
    def on_tool_start(self, ...):
        """Called when a tool starts execution."""
        pass
    
    def on_tool_end(self, ...):
        """Called when a tool finishes execution."""
        pass

Sources: libs/core/langchain_core/callbacks/base.py

CallbackManager

The CallbackManager coordinates multiple callback handlers and ensures proper event propagation:

sequenceDiagram
    participant App as Application
    participant CM as CallbackManager
    participant H1 as Handler 1
    participant H2 as Handler 2
    participant LC as LangChain Component

    App->>CM: add_handler(handler)
    App->>LC: invoke(inputs, callbacks=CM)
    LC->>CM: on_chain_start(...)
    CM->>H1: on_chain_start(...)
    CM->>H2: on_chain_start(...)
    LC->>CM: on_llm_start(...)
    CM->>H1: on_llm_start(...)
    CM->>H2: on_llm_start(...)
    LC->>CM: on_llm_end(...)
    CM->>H1: on_llm_end(...)
    CM->>H2: on_llm_end(...)
    LC->>CM: on_chain_end(...)
    CM->>H1: on_chain_end(...)
    CM->>H2: on_chain_end(...)

Key features of CallbackManager:

  • Manages a list of registered handlers
  • Supports handler persistence via copy() method
  • Provides context management for nested callbacks
  • Handles both sync and async handlers transparently

Sources: libs/core/langchain_core/callbacks/manager.py

Callback Configuration Options

ParameterTypeDescription
tagslist[str]Tags for filtering callbacks
inheritableboolWhether callbacks propagate to child runs
metadatadictAdditional context metadata

Tracing Infrastructure

Overview

The tracing subsystem extends the callback system to provide persistent, exportable traces of LangChain executions. Tracers are specialized callback handlers that serialize execution data for external systems like LangSmith.

graph LR
    A[LangChain Execution] --> B[Tracer Base]
    B --> C[Core Tracer]
    B --> D[Event Stream Tracer]
    C --> E[LangChain Tracers]
    D --> F[Async Event Streaming]
    E --> G[LangSmith]
    E --> H[Other Exporters]

Sources: libs/core/langchain_core/tracers/__init__.py

Tracer Base Class

Located in libs/core/langchain_core/tracers/base.py, the base tracer provides:

FeatureDescription
Run trackingMaintains state for all active and completed runs
Event serializationConverts callback events to exportable format
Parent-child relationshipsManages nested execution traces
Persistence layerAbstract interface for storing traces

Event Stream Tracer

The EventStreamTracer provides an async-native interface for streaming trace events:

# Conceptual implementation
class EventStreamTracer:
    """Tracer that supports async event streaming."""
    
    async def astream_events(self, ...):
        """Async generator for trace events."""
        while True:
            event = await self._get_next_event()
            if event is None:
                break
            yield event

This tracer enables real-time event streaming capabilities for applications requiring live observability.

Sources: libs/core/langchain_core/tracers/event_stream.py

Integration Patterns

Passing Callbacks to Components

Callbacks can be passed to LangChain components at initialization or invocation time:

from langchain_core.callbacks import CallbackManager, BaseCallbackHandler

class MyHandler(BaseCallbackHandler):
    def on_chain_start(self, serialized, inputs, **kwargs):
        print(f"Chain starting with inputs: {inputs}")

handler = MyHandler()
callback_manager = CallbackManager(handlers=[handler])

# Pass to model
response = model.invoke("Hello", config={"callbacks": callback_manager})

# Pass to chain
chain.invoke(input, config={"callbacks": callback_manager})

Using Tracers for Persistence

from langchain_core.tracers import LangChainTracer

tracer = LangChainTracer(project_name="my-project")

chain.invoke(
    input,
    config={"callbacks": [tracer]}
)

Data Flow

graph TD
    subgraph Execution
        A[User Code] --> B[Chain/Model]
        B --> C[CallbackManager]
    end
    
    subgraph EventPropagation
        C --> D[on_chain_start]
        D --> E[on_llm_start]
        E --> F[LLM Execution]
        F --> G[on_llm_end]
        G --> H[on_chain_end]
    end
    
    subgraph Observability
        H --> I[Tracer]
        I --> J[Run Tree]
        J --> K[Export/Store]
        K --> L[LangSmith/Other]
    end
    
    style L fill:#fff3e0
    style K fill:#fff3e0

Best Practices

  1. Handler Isolation: Create separate callback handlers for different concerns (monitoring, tracing, debugging)
  1. Performance Consideration: Avoid expensive operations in synchronous callback handlers; prefer async handlers for I/O-bound operations
  1. Memory Management: Use copy() on CallbackManager when spawning child executions to prevent handler leakage
  1. Tag-Based Filtering: Use tags to selectively enable/disable callbacks in different environments
  1. Tracer Configuration: Configure tracers with appropriate project names and metadata for proper organization in observability platforms

Package Structure

The callbacks and tracing infrastructure is maintained in the core library:

libs/core/langchain_core/
├── callbacks/
│   ├── __init__.py      # Public exports
│   ├── base.py          # BaseCallbackHandler
│   └── manager.py       # CallbackManager, AsyncCallbackManager
└── tracers/
    ├── __init__.py      # Public exports
    ├── base.py          # Base tracer implementations
    └── event_stream.py  # Async event streaming support

Sources: libs/README.md

Sources: [libs/core/langchain_core/callbacks/base.py](libs/core/langchain_core/callbacks/base.py)

Chat Models and Embeddings

Related topics: Introduction to LangChain, Messages and Prompt System

Section Related Pages

Continue reading this section for the full explanation and source context.

Section High-Level Component Hierarchy

Continue reading this section for the full explanation and source context.

Section Chat Model Initialization Pattern

Continue reading this section for the full explanation and source context.

Section BaseChatModel Interface

Continue reading this section for the full explanation and source context.

Related topics: Introduction to LangChain, Messages and Prompt System

Chat Models and Embeddings

Overview

Chat Models and Embeddings are foundational components within the LangChain ecosystem that enable interaction with large language models (LLMs) for conversational and vector-based AI applications. These abstractions provide standardized interfaces for working with different model providers while maintaining flexibility for customization.

LangChain provides two primary categories of language model interfaces:

CategoryPurposeKey Classes
Chat ModelsHandle conversational interactions with message-based APIsBaseChatModel, BaseMessage
EmbeddingsConvert text into vector representations for similarity searchEmbeddings, BaseEmbeddings
LLMsTraditional text-in, text-out language model interfacesBaseLLM, LLM

Sources: libs/core/langchain_core/language_models/chat_models.py

Architecture

High-Level Component Hierarchy

graph TD
    A[LangChain Core] --> B[Language Models]
    A --> C[Embeddings]
    B --> D[BaseChatModel]
    B --> E[BaseLLM]
    D --> F[Provider Implementations]
    E --> G[Provider Implementations]
    C --> H[Embeddings Interface]
    H --> I[Embedding Providers]
    
    F --> J[OpenAI Chat]
    F --> K[Anthropic Chat]
    F --> L[Ollama Chat]
    
    I --> M[OpenAI Embeddings]
    I --> N[HuggingFace Embeddings]
    I --> O[Ollama Embeddings]

Chat Model Initialization Pattern

The preferred method for initializing chat models in LangChain is through the init_chat_model function, which provides a unified initialization interface across providers.

from langchain.chat_models import init_chat_model

# Initialize with provider prefix
model = init_chat_model("openai:gpt-4o-mini", temperature=0)

# Invoke the model
result = model.invoke("Hello, world!")

Sources: README.md

Chat Models

BaseChatModel Interface

The BaseChatModel class serves as the foundation for all chat model implementations in LangChain. It extends BaseLanguageModel and provides standardized methods for generating responses from conversation histories.

#### Key Methods

MethodPurpose
invoke()Synchronous invocation with input processing
ainvoke()Asynchronous invocation
bind_tools()Attach tools/functions to the model for tool calling
with_structured_output()Constrain output to follow a schema

The bind_tools method enables models to call external tools, which is fundamental for building agentic applications:

model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
tools = toolkit.get_tools()
api_request_chain = API_URL_PROMPT.partial(api_docs=api_spec) | model.bind_tools(tools, tool_choice="any")

Sources: libs/langchain/langchain_classic/chains/api/base.py

Message Types

Chat models in LangChain work with structured message objects that represent different roles in a conversation:

Message TypeRoleTypical Use
SystemMessageSystemInstructions and context
HumanMessageUserUser input
AIMessageAssistantModel responses
ToolMessageToolTool execution results

State Management in Chat Applications

When building chat applications with stateful interactions, LangChain uses typed state dictionaries with LangGraph:

class ChainState(TypedDict):
    """LangGraph state."""
    messages: Annotated[Sequence[BaseMessage], add_messages]

async def acall_model(state: ChainState, config: RunnableConfig):
    response = await model.ainvoke(state["messages"], config)
    return {"messages": [response]}

Sources: libs/langchain/langchain_classic/chains/api/base.py

Embeddings

Embeddings Interface

The Embeddings interface provides a standardized way to convert text into numerical vector representations. These vectors enable semantic similarity calculations, retrieval-augmented generation (RAG), and vector database operations.

#### Core Methods

MethodDescription
embed_query()Embed a single query string
embed_documents()Embed multiple document strings
embed_query_async()Async version of embed_query
embed_documents_async()Async version of embed_documents

Sources: libs/core/langchain_core/embeddings/embeddings.py

Integration with Vector Stores

Embeddings work seamlessly with LangChain's vector store integrations. The HuggingFace partner package provides specific implementations:

# Install with full dependencies for local inference
pip install langchain-huggingface[full]

The [full] extra includes sentence-transformers>=5.2.0 and transformers>=5.0.0, which are required for HuggingFaceEmbeddings.

Sources: libs/partners/huggingface/README.md

Model Providers

LangChain provides partner packages for various LLM providers, each following consistent patterns for installation and usage:

ProviderPackageInstall Command
OpenAIlangchain-openaipip install langchain-openai
Anthropiclangchain-anthropicpip install langchain-anthropic
Ollamalangchain-ollamapip install langchain-ollama
HuggingFacelangchain-huggingfacepip install langchain-huggingface
Groqlangchain-groqpip install langchain-groq
DeepSeeklangchain-deepseekpip install langchain-deepseek
Perplexitylangchain-perplexitypip install langchain-perplexity
xAIlangchain-xaipip install langchain-xai
Chromalangchain-chromapip install langchain-chroma
Exalangchain-exapip install langchain-exa
OpenRouterlangchain-openrouterpip install langchain-openrouter

Each provider package contains integration classes that implement the base interfaces described above.

Sources: libs/partners/openai/README.md, libs/partners/anthropic/README.md, libs/partners/ollama/README.md

Testing Chat Models

LangChain provides standardized test infrastructure through the langchain-tests package to ensure consistent behavior across implementations:

from typing import Type
import pytest
from langchain_core.language_models import BaseChatModel
from langchain_tests.unit_tests import ChatModelUnitTests

from langchain_parrot_chain import ChatParrotChain

class TestParrotChainStandard(ChatModelUnitTests):
    @pytest.fixture
    def chat_model_class(self) -> Type[BaseChatModel]:
        return ChatParrotChain

#### Configurable Test Fixtures

FixtureRequiredDefaultDescription
chat_model_classYesThe chat model class to test
chat_model_paramsNo{}Constructor keyword arguments
chat_model_has_tool_callingNohasattr(class, 'bind_tools')Tool calling capability
chat_model_has_structured_outputNohasattr(class, 'with_structured_output')Structured output support

Sources: libs/standard-tests/README.md

Deprecated Patterns

The langchain-community package previously contained community-contributed integrations, but this pattern is being deprecated in favor of official partner packages:

@deprecated(
    since="0.2.13",
    removal="2.0.0",
    alternative="langchain.agents.create_agent",
)
class APIChain(Chain):
    """Chain that makes API calls and summarizes the responses."""

New integrations should be implemented as standalone partner packages following the patterns established in libs/partners/*.

Sources: libs/langchain/langchain_classic/chains/api/base.py

Quick Reference

Initialization

from langchain.chat_models import init_chat_model

# Basic initialization
model = init_chat_model("provider:model-name")

# With parameters
model = init_chat_model("openai:gpt-4o-mini", temperature=0)

Invocation

# Synchronous
response = model.invoke("Your message here")

# Asynchronous
response = await model.ainvoke("Your message here")

# With chat history
response = model.invoke([SystemMessage(content="You are helpful"), 
                         HumanMessage(content="Hello")])

Tool Binding

model_with_tools = model.bind_tools(tools)
response = model_with_tools.invoke("Use tool X to find information")

Further Reading

Sources: [libs/core/langchain_core/language_models/chat_models.py](https://github.com/langchain-ai/langchain/blob/main/libs/core/langchain_core/language_models/chat_models.py)

Agents Framework

Related topics: Tools and Toolkits, Runnable and Execution Model

Section Related Pages

Continue reading this section for the full explanation and source context.

Section Base Agent Interface

Continue reading this section for the full explanation and source context.

Section Agent Types

Continue reading this section for the full explanation and source context.

Section Tool Structure

Continue reading this section for the full explanation and source context.

Related topics: Tools and Toolkits, Runnable and Execution Model

Agents Framework

The LangChain Agents Framework is a core component of the LangChain ecosystem that enables language models to reason about and take actions through a systematic loop of observation, thought, and execution. The framework provides abstractions for building autonomous agents that can use tools, make decisions, and complete complex multi-step tasks.

Architecture Overview

LangChain's agent architecture follows a cyclical pattern where the agent receives inputs, decides on actions using an LLM, executes those actions via tools, and processes the results to determine the next step.

graph TD
    A[User Input] --> B[Agent]
    B --> C[LLM Decision]
    C --> D{Action Type}
    D -->|Tool Call| E[Tool Execution]
    D -->|Final Answer| F[Output]
    E --> G[Observation]
    G --> B
    F --> H[User]
    
    style B fill:#e1f5fe
    style C fill:#fff3e0
    style E fill:#f3e5f5

Core Agent Classes

The agent system is built on a base Agent class that defines the interface for all agent implementations. LangChain provides several pre-built agent types for different use cases.

Base Agent Interface

All agents inherit from the base Agent class which provides:

  • Chain integration for LLM calls
  • Tool binding and management
  • Output parsing for action extraction
  • State management across agent steps

Agent Types

Agent TypeDescriptionUse Case
ReActReasoning + Acting agentGeneral purpose tool use
Structured ChatJSON-structured tool callsStructured tool interfaces
OpenAI FunctionsOpenAI function calling formatOpenAI function calling
ToolkitsPre-configured tool bundlesDomain-specific tasks

Sources: libs/langchain/langchain_classic/agents/__init__.py

Tool Integration

Agents interact with external systems through tools. Tools are abstractions that wrap functionality and expose it to the LLM in a structured format.

Tool Structure

Each tool typically consists of:

  • Name: Unique identifier for the tool
  • Description: Natural language description for LLM understanding
  • Parameters: Schema defining required and optional inputs
  • Execute Function: The actual implementation

Toolkits

Toolkits provide pre-configured bundles of related tools for specific domains:

ToolkitPurpose
RequestsToolkitHTTP requests for API interactions
SQLDatabaseToolkitDatabase queries
Custom ToolkitsDomain-specific tool collections

Sources: libs/langchain/langchain_classic/agents/agent_toolkits/__init__.py

Agent Execution Loop

The agent execution follows a ReAct-style (Reasoning + Acting) pattern:

sequenceDiagram
    participant User
    participant Agent
    participant LLM
    participant Tools
    
    User->>Agent: Input Query
    Agent->>LLM: Prompt + Tools
    LLM-->>Agent: Thought + Action
    Agent->>Tools: Execute Tool Call
    Tools-->>Agent: Observation
    Agent->>LLM: Observation + Next Decision
    loop Until Final Answer
        LLM-->>Agent: Next Action
        Agent->>Tools: Execute Tool Call
        Tools-->>Agent: Observation
    end
    Agent-->>User: Final Response

Agent Creation API

The recommended way to create agents is through the create_agent factory function:

from langchain.agents import create_agent

# Create an agent with specified model and tools
agent = create_agent(
    model="openai:gpt-4",
    tools=[...],
    system_message="You are a helpful assistant."
)

Sources: libs/langchain/langchain_classic/agents/agent.py

Agent Types Detail

ReAct Agent

The ReAct (Reasoning + Acting) agent implements a reasoning loop where the model thinks through each step before taking action.

Characteristics:

  • Explicit thought process before each action
  • Observation processing after tool execution
  • Flexible tool usage without strict schema requirements

Sources: libs/langchain/langchain_classic/agents/react/__init__.py

Structured Chat Agent

Designed for agents that interact with tools using structured JSON inputs, this agent type excels when working with tools that have complex parameter schemas.

Characteristics:

  • JSON-structured tool inputs
  • Strong typing for tool parameters
  • Schema validation for tool calls

Sources: libs/langchain/langchain_classic/agents/structured_chat/__init__.py

OpenAI Functions Agent

Optimized for OpenAI's function calling API, this agent type maps LangChain tools to OpenAI function specifications.

Characteristics:

  • Direct mapping to OpenAI function calling format
  • Tool choice controls (auto, any, none)
  • Streaming support for responses

Sources: libs/langchain/langchain_classic/agents/openai_functions_agent/__init__.py

Integration with LangGraph

For complex agent workflows, LangChain Agents integrate seamlessly with LangGraph for building stateful, controllable agent applications:

from langgraph.prebuilt import ToolNode
from langgraph.graph import StateGraph

class AgentState(TypedDict):
    messages: Annotated[Sequence[BaseMessage], add_messages]

graph_builder = StateGraph(AgentState)
graph_builder.add_node("call_tool", some_agent_node)
graph_builder.add_node("execute_tool", ToolNode(tools))
graph_builder.add_edge("call_tool", "execute_tool")

Configuration Options

ParameterTypeDescription
modelstr or BaseLanguageModelLLM to power the agent
toolsList[BaseTool]Available tools for the agent
system_messagestrInstructions for agent behavior
max_iterationsintMaximum agent steps before termination
timeoutfloatTimeout for agent execution

Deprecated Patterns

The APIChain class has been deprecated in favor of the new agent system:

@deprecated(
    since="0.2.13",
    removal="2.0.0",
    alternative="langchain.agents.create_agent",
)
class APIChain(Chain):
    """Chain that makes API calls and summarizes responses."""

Migration to the new agent system is recommended for all use cases.

Security Considerations

When deploying agents:

  • Input Validation: Validate all user inputs before processing
  • Tool Permissions: Control which tools are available to agents
  • Network Access: Limit agent network capabilities appropriately
  • Rate Limiting: Implement rate limiting to prevent abuse

Summary

The LangChain Agents Framework provides a flexible, extensible system for building LLM-powered agents that can:

  1. Reason about user queries
  2. Select appropriate tools for task completion
  3. Execute actions and process results
  4. Iterate until goals are achieved

The framework supports multiple agent types optimized for different use cases, with seamless integration into the broader LangChain ecosystem including LangGraph for complex workflows.

Sources: [libs/langchain/langchain_classic/agents/__init__.py](https://github.com/langchain-ai/langchain/blob/main/libs/langchain/langchain_classic/agents/__init__.py)

Tools and Toolkits

Related topics: Agents Framework

Section Related Pages

Continue reading this section for the full explanation and source context.

Section BaseTool

Continue reading this section for the full explanation and source context.

Section StructuredTool

Continue reading this section for the full explanation and source context.

Section Toolkit Structure

Continue reading this section for the full explanation and source context.

Related topics: Agents Framework

Tools and Toolkits

LangChain provides a flexible and extensible system for defining, managing, and executing tools that enable Large Language Models (LLMs) to interact with external systems, APIs, and data sources. The Tools and Toolkits module forms the foundational building block for agent-based architectures, allowing models to perform actions and retrieve information beyond their training data.

Overview

Tools in LangChain serve as the interface between LLMs and external capabilities. They provide a standardized way to:

  • Define callable functions with clear input/output schemas
  • Control what operations an agent can perform
  • Handle serialization, deserialization, and tool calling protocols
  • Integrate with various third-party services and APIs

The system is designed around the principle of structured tool definitions, where each tool specifies its name, description, and parameter schema, enabling LLMs to understand when and how to invoke specific tools.

Architecture

graph TD
    A[Agent / LLM] -->|Tool Calls| B[Tool Executor]
    B --> C[BaseTool]
    C --> D[StructuredTool]
    C --> E[ToolKit]
    D --> F[Function Definition]
    E --> G[Tool Collection]
    B -->|Results| A
    
    style A fill:#e1f5ff
    style B fill:#fff3e0
    style C fill:#e8f5e9

Core Components

BaseTool

The BaseTool class serves as the foundation for all tools in LangChain. It provides the essential interface that all tool implementations must follow.

AttributeTypeDescription
namestrUnique identifier for the tool
descriptionstrHuman-readable description for LLM understanding
args_schemaType[BaseModel]Pydantic model defining input parameters
return_schemaOptional[Type[BaseModel]]Schema for return values
tagsOptional[List[str]]Categorization tags
metadataOptional[Dict[str, Any]]Additional metadata

Key Methods:

def invoke(self, input: Union[str, Dict], config: Optional[CallbackManager] = None) -> Any
async def ainvoke(self, input: Union[str, Dict], config: Optional[AsyncCallbackManager] = None) -> Any

Sources: libs/core/langchain_core/tools/base.py

StructuredTool

StructuredTool extends BaseTool with enhanced parameter handling capabilities. It supports complex input schemas and provides better type safety for tool definitions.

Key Features:

  • Pydantic-based input validation
  • JSON schema generation for LLM tool calling protocols
  • Support for nested objects and arrays
  • Custom validation logic via model validators

Sources: libs/core/langchain_core/tools/structured.py

Tool Definition Pattern

from langchain_core.tools import StructuredTool
from pydantic import BaseModel, Field

class SearchInput(BaseModel):
    query: str = Field(description="The search query to look up")
    limit: int = Field(default=10, description="Maximum number of results")

def search_function(query: str, limit: int = 10) -> str:
    """Perform a web search and return results."""
    # Implementation here
    pass

search_tool = StructuredTool(
    name="web_search",
    description="Search the web for information about a given topic",
    func=search_function,
    args_schema=SearchInput,
)

Toolkits

Toolkits provide a convenient way to group related tools together. They bundle multiple tools that are commonly used together for specific use cases.

Toolkit Structure

ComponentDescription
toolsList of BaseTool instances
nameToolkit identifier
descriptionSummary of toolkit purpose

Creating Custom Toolkits

from langchain_core.tools import BaseTool, StructuredTool
from langchain_core.tools import Toolkit

class CustomToolkit(Toolkit):
    def __init__(self) -> None:
        super().__init__()
        self.tools = [
            tool_a,
            tool_b,
            tool_c,
        ]
    
    def get_tools(self) -> list[BaseTool]:
        return self.tools

Sources: libs/langchain/langchain_classic/tools/base.py

Tool Rendering

Tool rendering converts tool definitions into formats consumable by different LLM providers and protocols.

graph LR
    A[BaseTool] -->|convert| B[OpenAI Format]
    A -->|convert| C[Anthropic Format]
    A -->|convert| D[JSON Schema]
    A -->|convert| E[Custom Format]

Render Modes

ModeDescription
tool_callRenders tools for OpenAI-style tool calling
json_schemaOutputs JSON Schema representation
anthropicFormat compatible with Anthropic Claude models

Sources: libs/core/langchain_core/tools/render.py

Tool Execution Flow

sequenceDiagram
    participant Agent
    participant Executor
    participant Tool
    participant External
    
    Agent->>Executor: invoke(tool_name, parameters)
    Executor->>Tool: validate_and_bind_params(input)
    Tool->>External: execute_action()
    External-->>Tool: result
    Tool-->>Executor: validated_result
    Executor-->>Agent: ToolOutput

Integration with Agents

Tools are bound to agents through the agent construction process. The binding specifies which tools an agent can access and how they should be invoked.

Common Patterns:

  1. Direct Binding: Single tool passed to agent
  2. ToolKit Binding: Multiple related tools from a toolkit
  3. Dynamic Binding: Tools selected at runtime based on context
from langchain.agents import create_agent
from langchain_core.tools import StructuredTool

agent = create_agent(
    llm=model,
    tools=[search_tool, calculator_tool],
    prompt=system_prompt,
)

Built-in Tool Categories

Core Tools

Located in libs/langchain/langchain_classic/tools/:

ToolPurpose
SearchWeb search capabilities
WikipediaWikipedia API integration
CalculatorMathematical operations
FileSystemLocal file operations

API Integration Tools

LangChain provides specialized tools for API interactions:

from langchain_classic.chains.api.base import APIChain

# APIChain is deprecated in favor of agent-based approaches
# but demonstrates the pattern of API tool integration

Sources: libs/langchain/langchain_classic/chains/api/base.py

Configuration Options

Tool Configuration Parameters

ParameterTypeDefaultDescription
handle_parsing_errorsboolTrueHandle LLM output parsing errors
return_directboolFalseReturn tool output directly to LLM
verboseboolFalseEnable verbose logging
max_retriesint3Maximum retry attempts
timeoutOptional[float]NoneExecution timeout in seconds

Error Handling

Tools implement comprehensive error handling to manage failures gracefully:

try:
    result = tool.invoke(input_dict)
except ValidationError as e:
    # Handle input validation failures
    return ErrorOutput(error=str(e))
except ExecutionError as e:
    # Handle execution failures
    return ErrorOutput(error=str(e))
except TimeoutError:
    # Handle timeout
    return ErrorOutput(error="Tool execution timed out")

Best Practices

1. Clear Tool Descriptions

Write descriptions that help the LLM understand when and how to use each tool:

description = """
    Search for information on the web.
    
    Use this tool when:
    - User asks about current events or facts
    - User requests information not in training data
    - User asks about specific entities that need verification
    
    Parameters:
    - query: The search query (required)
    - limit: Maximum results to return (optional, default: 5)
    """

2. Comprehensive Schemas

Define complete input schemas with descriptions:

class QueryInput(BaseModel):
    query: str = Field(
        description="The search query string. Be specific and include relevant keywords.",
        min_length=1,
        max_length=500,
    )
    source: Literal["web", "news", "scholar"] = Field(
        default="web",
        description="The type of search to perform",
    )

3. Proper Error Messages

Return meaningful error messages that help the agent recover:

def execute_tool(self, params: Dict) -> str:
    try:
        return self._do_execution(params)
    except ValueError as e:
        return f"Invalid input: {str(e)}. Please check your parameters and try again."
    except ConnectionError:
        return "Unable to connect to the service. Please check your network connection."

Testing Tools

LangChain provides testing utilities for tool implementations:

from langchain_core.tools import tool, StructuredTool

def test_tool_invoke():
    """Test tool invocation with valid input."""
    result = my_tool.invoke({"param1": "value1"})
    assert result is not None

def test_tool_validation():
    """Test input validation."""
    from pydantic import ValidationError
    try:
        my_tool.invoke({"invalid_param": "value"})
        assert False, "Should have raised ValidationError"
    except ValidationError:
        pass  # Expected behavior

Package Structure

langchain/
├── libs/
│   ├── core/
│   │   └── langchain_core/
│   │       └── tools/
│   │           ├── __init__.py       # Public API exports
│   │           ├── base.py           # BaseTool implementation
│   │           ├── structured.py     # StructuredTool implementation
│   │           └── render.py         # Tool rendering utilities
│   └── langchain/
│       └── langchain_classic/
│           └── tools/
│               ├── __init__.py       # Built-in tools
│               └── base.py           # Toolkit base classes

See Also

Sources: [libs/core/langchain_core/tools/base.py](https://github.com/langchain-ai/langchain/blob/main/libs/core/langchain_core/tools/base.py)

Document Loaders and Text Processing

Related topics: Vector Stores and Retrievers, Document Loaders and Text Processing

Section Related Pages

Continue reading this section for the full explanation and source context.

Section Document Class Structure

Continue reading this section for the full explanation and source context.

Section Core Loading Methods

Continue reading this section for the full explanation and source context.

Section Character Text Splitter

Continue reading this section for the full explanation and source context.

Related topics: Vector Stores and Retrievers, Document Loaders and Text Processing

Document Loaders and Text Processing

Document Loaders and Text Processing are fundamental components in the LangChain ecosystem that enable the ingestion, transformation, and preparation of various document types for downstream AI applications. These components form the entry point for most LLM-powered workflows, converting raw content from diverse sources into structured Document objects that can be processed by chains, agents, and retrieval systems.

The document processing pipeline in LangChain follows a two-stage approach: first, documents are loaded from external sources using specialized loaders; second, the loaded content is optionally split into smaller, semantically coherent chunks using text splitters. This separation of concerns allows for flexible composition and reuse across different application scenarios.

Architecture Overview

LangChain's document processing architecture is distributed across multiple packages within the monorepo, with core abstractions defined in langchain-core and legacy implementations preserved in langchain-classic. Text splitting functionality is isolated in a dedicated langchain-text-splitters package to maintain separation of concerns and enable independent versioning.

The architecture employs a base class pattern where BaseLoader defines the interface contract that all concrete loader implementations must satisfy. This design enables polymorphic usage across different document sources while maintaining consistent behavior. The BaseTransformer abstraction extends this pattern for document transformation, and the TextSplitter hierarchy provides pluggable text chunking strategies.

graph TD
    A[External Sources] --> B[Document Loaders]
    B --> C[Document Objects]
    C --> D[Text Splitters]
    D --> E[Chunked Documents]
    E --> F[Vector Stores]
    E --> G[LLM Chains]
    E --> H[Agents]
    
    B1[BaseLoader] --> B2[File Loaders]
    B1 --> B3[Web Loaders]
    B1 --> B4[Database Loaders]
    B1 --> B5[Cloud Storage Loaders]
    
    D1[TextSplitter] --> D2[CharacterTextSplitter]
    D1 --> D3[RecursiveCharacterTextSplitter]
    D1 --> D4[LanguageSpecificSplitter]

Document Object Model

At the heart of LangChain's document processing lies the Document data class, which provides a standardized representation for loaded content. Each Document instance encapsulates the actual text content along with associated metadata that describes the source, provenance, and any auxiliary information relevant to the content.

The Document class is defined in langchain-core and serves as the universal currency for document data throughout the LangChain ecosystem. This unified representation enables seamless interoperability between loaders, splitters, and consumers such as vector stores and language models.

Document Class Structure

AttributeTypeDescription
page_contentstrThe actual text content of the document
metadatadict[str, Any]Associated metadata including source, page number, etc.
typestrDocument type identifier (optional)

The metadata dictionary provides flexibility for storing arbitrary key-value pairs that describe the document's origin and characteristics. Common metadata fields include the source file path, URL, creation date, author, and any domain-specific attributes that may be useful for filtering or attribution.

Base Loader Interface

The BaseLoader abstract class in langchain_core.document_loaders.base establishes the foundational interface that all document loaders must implement. This interface ensures consistency across the loader ecosystem while allowing each loader to handle the specifics of its target data source.

graph LR
    A[load] --> B[Document List]
    A1[aload] --> B
    C[lazy_load] --> B
    D[get_lazy_loaders] --> E[Generator]
    
    style A fill:#90EE90
    style A1 fill:#90EE90
    style C fill:#90EE90
    style D fill:#90EE90

Core Loading Methods

MethodReturn TypeDescription
load()list[Document]Load all documents synchronously
aload()list[Document]Load all documents asynchronously
lazy_load()Iterator[Document]Lazily load documents one at a time
get_lazy_loaders()Generator[BaseLoader]Generate child loaders for partitioning

The lazy_load() method provides a memory-efficient approach for processing large document collections by yielding documents one at a time rather than loading everything into memory simultaneously. This is particularly valuable when working with large file systems or extensive web crawls.

Sources: libs/core/langchain_core/document_loaders/base.py

Base Transformer Interface

The BaseTransformer abstract class extends the loading framework to support document transformation and enrichment. Transformers operate on already-loaded documents, applying processing operations such as filtering, annotation, or format conversion.

This abstraction is particularly useful for implementing cross-cutting concerns like content cleaning, metadata extraction, and format standardization that should be applied consistently across multiple loader types.

graph TD
    A[Raw Documents] --> B[Transformer Pipeline]
    B --> C[Transformed Documents]
    
    B1[Metadata Enricher] --> B
    B2[Content Cleaner] --> B
    B3[Format Converter] --> B

Text Splitters

Text splitters address the practical requirement of dividing large documents into smaller, manageable chunks that satisfy token limitations and enable efficient retrieval. The TextSplitter class hierarchy in langchain-text-splitters provides multiple strategies for document segmentation.

The splitting process operates by first breaking documents into character-level chunks, then optionally applying a secondary splitting step based on the specific text structure (such as markdown headers, code blocks, or natural language sentences). This hierarchical approach ensures that chunks respect semantic boundaries while maintaining the desired size constraints.

Character Text Splitter

The CharacterTextSplitter provides the foundational character-based splitting implementation. It divides text at character boundaries, with configurable chunk size and overlap parameters to control the granularity and continuity between chunks.

Sources: libs/text-splitters/langchain_text_splitters/character.py

#### Key Configuration Parameters

ParameterTypeDefaultDescription
separatorstr"\n\n"Character sequence used for splitting
chunk_sizeint4000Maximum characters per chunk
chunk_overlapint200Characters of overlap between chunks
length_functionCallablelenFunction to calculate text length
is_separator_regexboolFalseWhether separator is a regex pattern

The chunk_overlap parameter is particularly important for maintaining context continuity across chunk boundaries. By including overlapping content between adjacent chunks, downstream systems can retrieve relevant information that might span a boundary without losing critical context.

graph LR
    A[Full Document] --> B[Chunk 1<br/>chars 0-4000]
    B --> C[Chunk 2<br/>chars 3800-7800<br/>200 char overlap]
    C --> D[Chunk 3<br/>chars 7600-11600<br/>200 char overlap]
    
    style B fill:#87CEEB
    style C fill:#87CEEB
    style D fill:#87CEEB

Recursive Character Text Splitter

The RecursiveCharacterTextSplitter extends the basic character splitter with a multi-pass approach that attempts to split text at increasingly smaller delimiters if the initial split results in chunks exceeding the target size. This strategy preserves semantic coherence by preferring natural text boundaries such as paragraphs and sentences over arbitrary character cuts.

The splitter maintains an ordered list of separators that it attempts in sequence: double newlines for paragraphs, single newlines for line breaks, spaces for mid-sentence breaks, and finally individual characters as a last resort. This ordered approach ensures that chunks align with linguistic structures whenever possible.

Splitting Methods

MethodParametersReturn TypeDescription
split_documents()documents: list[Document]list[Document]Split a list of Document objects
split_text()text: strlist[str]Split raw text into chunks
create_documents()texts: list[str], metadatas?: list[dict]list[Document]Create Document objects from texts

Sources: libs/text-splitters/langchain_text_splitters/__init__.py

Document Loader Categories

The LangChain ecosystem encompasses a diverse collection of document loaders organized by their target data sources. These loaders are distributed across multiple packages, with community-contributed loaders maintained in the langchain-community package and select integrations maintained by the LangChain team in the partners/ directory.

File System Loaders

File system loaders handle documents stored locally or accessible through file protocols. Support extends to common formats including plain text, CSV, JSON, XML, Markdown, and various word processing formats. Binary formats such as PDFs and Office documents require additional dependencies that are installed as optional extras.

Loader CategoryTypical ExtensionsDependencies
Plain Text.txt, .textNone
Structured Data.csv, .json, .xmlBuilt-in libraries
Documents.pdf, .docx, .pptxOptional extras
Code.py, .js, .java, etc.Language-specific parsers

Web Loaders

Web loaders enable extraction of content from internet and intranet resources. These loaders handle HTTP requests, HTML parsing, and content extraction to produce clean document objects free of navigation elements and advertising content.

The WebBaseLoader, PyMuPDFLoader, and PlaywrightLoader represent common approaches to web content extraction, each offering different tradeoffs between speed, accuracy, and JavaScript rendering capabilities.

Database Loaders

Database loaders connect to SQL and NoSQL databases to extract structured data and present it as documents. These loaders are essential for enterprise workflows that require accessing content stored in data warehouses, document databases, or traditional relational systems.

Cloud Storage Loaders

Integration with major cloud storage providers enables loading documents from Amazon S3, Google Cloud Storage, and Azure Blob Storage. These loaders handle authentication, bucket navigation, and file streaming to support large-scale document ingestion pipelines.

Integration with Retrieval Systems

Document loaders and text splitters form the foundation of Retrieval-Augmented Generation (RAG) pipelines. After documents are loaded and chunked, they are typically embedded using embedding models and stored in vector databases for similarity search.

graph TD
    A[Documents] --> B[Loader]
    B --> C[Document Objects]
    C --> D[Text Splitter]
    D --> E[Chunks]
    E --> F[Embedding Model]
    F --> G[Vector Store]
    G --> H[Retrieval]
    H --> I[LLM Response]

The chunk size and overlap parameters directly impact retrieval quality. Smaller chunks capture fine-grained concepts but may lack sufficient context, while larger chunks provide more context but may dilute the relevance of retrieved content. Tuning these parameters requires balancing the specific requirements of the target application.

Legacy and Deprecated Components

The langchain-classic package preserves legacy loader implementations that were present in earlier versions of LangChain. These components are maintained for backward compatibility but may lack features present in the newer langchain-core implementations.

Sources: libs/langchain/langchain_classic/document_loaders/__init__.py

The deprecation warnings in langchain-classic indicate a gradual migration path toward the consolidated abstractions in langchain-core. New projects should prefer the core implementations, while existing projects can continue using classic loaders until suitable migration paths are available.

Best Practices

When working with document loaders and text splitters, several practices help ensure optimal results in production environments. First, always consider the downstream consumer when selecting chunk sizes—smaller chunks work well for question answering, while larger chunks preserve more context for summarization tasks. Second, include comprehensive metadata during loading to enable filtering and attribution in later stages.

For large-scale document processing, prefer lazy loading strategies to manage memory consumption effectively. When dealing with heterogeneous document collections, consider creating custom transformer pipelines that apply format-specific preprocessing before the generic splitting logic.

The separator configuration should align with the natural structure of the document format. Markdown documents benefit from separator patterns that respect heading hierarchies, while plain text may work better with paragraph-based or sentence-based splitting strategies.

Package Organization Summary

PackagePurposeKey Classes
langchain-coreCore abstractionsBaseLoader, BaseTransformer, Document
langchain-classicLegacy implementationsCSVLoader, PyPDFLoader, etc.
langchain-text-splittersText chunkingTextSplitter, CharacterTextSplitter, RecursiveCharacterTextSplitter

Sources: libs/core/langchain_core/document_loaders/__init__.py

The separation of langchain-text-splitters into its own package reflects the independent applicability of text splitting beyond document loading. This design enables reuse of splitting logic in contexts where documents are generated programmatically or obtained through means other than loaders.

Sources: [libs/core/langchain_core/document_loaders/base.py](https://github.com/langchain-ai/langchain/blob/main/libs/core/langchain_core/document_loaders/base.py)

Vector Stores and Retrievers

Related topics: Document Loaders and Text Processing, Chat Models and Embeddings, Tools and Toolkits

Section Related Pages

Continue reading this section for the full explanation and source context.

Section Core Interface

Continue reading this section for the full explanation and source context.

Section Embeddings Integration

Continue reading this section for the full explanation and source context.

Section Deletion and Prefix Operations

Continue reading this section for the full explanation and source context.

Related topics: Document Loaders and Text Processing, Chat Models and Embeddings, Tools and Toolkits

Vector Stores and Retrievers

Overview

Vector Stores and Retrievers are fundamental components in LangChain that enable efficient similarity search and retrieval of documents based on semantic meaning. Vector stores handle the storage and indexing of embedded data, while retrievers provide the interface for querying and fetching relevant information from these stores.

LangChain provides a layered architecture where core abstractions are defined in langchain_core and concrete implementations are available through partner packages and langchain_classic.

Architecture

The Vector Stores and Retrievers system follows a clean separation of concerns with interfaces defined at the core level and implementations distributed across the ecosystem.

graph TD
    A[User Query] --> B[Embedding Model]
    B --> C[Vector Query]
    C --> D[Vector Store]
    D --> E[Retriever]
    E --> F[Retrieved Documents]
    
    G[Documents] --> H[Document Loader]
    H --> I[Text Splitter]
    I --> J[Embedding Model]
    J --> K[Indexed Vectors]
    K --> D
    
    L[langchain_core] --> M[BaseVectorStore Interface]
    L --> N[BaseRetriever Interface]
    O[langchain_classic] --> P[Additional VectorStore Implementations]
    O --> Q[Retrievers]
    R[Partner Packages] --> S[Chroma, FAISS, Pinecone, etc.]

Vector Stores

Core Interface

The base vector store interface in langchain_core provides the foundational methods that all vector store implementations must support. The interface is designed to be provider-agnostic while enabling specific features through optional methods.

#### Key Methods

MethodPurposeParametersReturn Type
add_documentsAdd documents to the storedocuments: List[Document], ids: Optional[List[str]]List[str]
add_textsAdd raw texts to the storetexts: List[str], metadatas: Optional[List[dict]], ids: Optional[List[str]]List[str]
similarity_searchFind similar documentsquery: str, k: int, filter: Optional[dict]List[Document]
similarity_search_by_vectorSearch by embeddingembedding: List[float], k: int, filter: Optional[dict]List[Document]
similarity_search_with_scoreSearch with relevance scoresquery: str, k: int, filter: Optional[dict]List[Tuple[Document, float]]
deleteRemove documents by IDids: Optional[List[str]]None
get_by_prefixRetrieve by ID prefixprefix: strList[Document]

Embeddings Integration

Vector stores work in conjunction with embedding models to convert text into vector representations. The HuggingFace integration provides HuggingFaceEmbeddings for generating embeddings locally:

# From libs/partners/huggingface/README.md
from langchain_huggingface import HuggingFaceEmbeddings

# Requires sentence-transformers>=5.2.0
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

The embedding models are decoupled from vector stores, allowing flexibility in choosing both components independently.

Deletion and Prefix Operations

Vector stores support flexible document management through deletion and prefix-based retrieval operations:

OperationDescription
delete(ids)Removes specific documents by their IDs
get_by_prefix(prefix)Retrieves all documents whose IDs start with the given prefix

These operations enable efficient document lifecycle management within the vector store.

Retrievers

Retrievers provide a standardized interface for fetching relevant documents from various sources. They abstract away the underlying storage mechanism and provide additional capabilities like filtering and reranking.

Base Retriever Interface

The retriever interface in langchain_classic defines the contract for all retrieval implementations:

graph LR
    A[Query] --> B[Retriever]
    B --> C{Filtering}
    C -->|Apply| D[Metadata Filter]
    C -->|Apply| E[Time-based Filter]
    B --> F[Document Pool]
    F --> G[Top-K Results]

Retriever Types

LangChain provides several specialized retrievers:

Retriever TypePurposeUse Case
VectorStoreRetrieverStandard similarity searchGeneral-purpose retrieval
MultiVectorRetrieverMultiple embedding per documentTables, images, sub-documents
MultiQueryRetrieverGenerate multiple query variationsImprove recall
ContextualCompressionRetrieverCompress retrieved contextReduce token usage
TimeWeightedVectorStoreRetrieverPrioritize recent documentsRecency-aware search

Configuration Options

Retrievers support various configuration parameters:

ParameterTypeDefaultDescription
search_typestr"similarity"Search algorithm: similarity, mmr
kint4Number of documents to retrieve
fetch_kint20Initial pool size for MMR
lambda_multfloat0.5MMR diversity parameter
filterdictNoneMetadata filter conditions

Partner Integrations

Chroma Vector Store

The langchain-chroma package provides integration with Chroma, an open-source embedding database:

pip install langchain-chroma

Chroma is designed specifically for AI applications and provides efficient storage and retrieval of embeddings with metadata filtering capabilities.

HuggingFace Embeddings

The langchain-huggingface package offers multiple embedding options:

  • HuggingFaceEmbeddings - For local inference using sentence-transformers
  • HuggingFacePipeline - For local LLM inference

For full functionality including embeddings:

pip install langchain-huggingface[full]
Note: The [full] extra requires sentence-transformers>=5.2.0 and transformers>=5.0.0. Projects migrating from langchain-community may need to upgrade sentence-transformers to >=5.2.0.

Package Structure

Core Layer (`langchain_core`)

The langchain_core.vectorstores module provides base abstractions that define the contract for all vector store implementations. This layer is dependency-light and focuses on interface definitions.

Classic Layer (`langchain_classic`)

The langchain_classic.vectorstores module extends the core abstractions with additional functionality:

  • Additional vector store implementations
  • Utility functions for common operations
  • Integration with other LangChain components

Partner Packages

Partner packages provide maintained integrations with specific vector store providers:

libs/partners/
├── chroma/       # Chroma vector store
├── weaviate/     # Weaviate vector database
├── pinecone/     # Pinecone managed vector database
├── qdrant/       # Qdrant vector search engine
└── ...           # Additional providers

Workflow: Document Indexing and Retrieval

sequenceDiagram
    participant User
    participant Loader
    participant Splitter
    participant Embedder
    participant VectorStore
    participant Retriever
    
    User->>Loader: Load documents
    Loader-->>User: Raw documents
    User->>Splitter: Split documents
    Splitter-->>User: Chunked documents
    User->>Embedder: Generate embeddings
    Embedder-->>User: Vector embeddings
    User->>VectorStore: Add documents + embeddings
    VectorStore-->>User: Document IDs
    
    User->>Retriever: Query
    Retriever->>Embedder: Embed query
    Embedder-->>Retriever: Query vector
    Retriever->>VectorStore: Similarity search
    VectorStore-->>Retriever: Top-K documents
    Retriever-->>User: Retrieved results

Best Practices

Indexing Recommendations

  1. Chunk Size Selection: Choose appropriate chunk sizes based on your use case—smaller chunks for precise retrieval, larger chunks for more context
  2. Metadata Enrichment: Include relevant metadata during indexing to enable filtered retrieval
  3. Embedding Selection: Match embedding models to your data type and language requirements

Retrieval Optimization

  1. Use MMR for Diversity: When using MMR (Maximal Marginal Relevance), tune lambda_mult to balance relevance and diversity
  2. Implement Filtering: Use metadata filtering to narrow results to relevant subsets
  3. Consider Compression: For long contexts, use contextual compression to reduce token usage

Security Considerations

When implementing vector stores and retrievers in production:

  • Data Isolation: Ensure vector store data is properly isolated based on access requirements
  • Input Sanitization: Validate and sanitize query inputs to prevent injection attacks
  • Access Control: Implement appropriate access controls for sensitive document retrieval

See Also

Source: https://github.com/langchain-ai/langchain / Human Manual

Doramagic Pitfall Log

Source-linked risks stay visible on the manual page so the preview does not read like a recommendation.

medium README/documentation is current enough for a first validation pass.

The project should not be treated as fully validated until this signal is reviewed.

medium Maintainer activity is unknown

Users cannot judge support quality until recent activity, releases, and issue response are checked.

medium no_demo

The project may affect permissions, credentials, data exposure, or host boundaries.

medium no_demo

The project may affect permissions, credentials, data exposure, or host boundaries.

Doramagic Pitfall Log

Doramagic extracted 6 source-linked risk signals. Review them before installing or handing real data to the project.

1. Capability assumption: README/documentation is current enough for a first validation pass.

  • Severity: medium
  • Finding: README/documentation is current enough for a first validation pass.
  • User impact: The project should not be treated as fully validated until this signal is reviewed.
  • Recommended check: Open the linked source, confirm whether it still applies to the current version, and keep the first run isolated.
  • Evidence: capability.assumptions | github_repo:552661142 | https://github.com/langchain-ai/langchain | README/documentation is current enough for a first validation pass.

2. Maintenance risk: Maintainer activity is unknown

  • Severity: medium
  • Finding: Maintenance risk is backed by a source signal: Maintainer activity is unknown. Treat it as a review item until the current version is checked.
  • User impact: Users cannot judge support quality until recent activity, releases, and issue response are checked.
  • Recommended check: Open the linked source, confirm whether it still applies to the current version, and keep the first run isolated.
  • Evidence: evidence.maintainer_signals | github_repo:552661142 | https://github.com/langchain-ai/langchain | last_activity_observed missing

3. Security or permission risk: no_demo

  • Severity: medium
  • Finding: no_demo
  • User impact: The project may affect permissions, credentials, data exposure, or host boundaries.
  • Recommended check: Open the linked source, confirm whether it still applies to the current version, and keep the first run isolated.
  • Evidence: downstream_validation.risk_items | github_repo:552661142 | https://github.com/langchain-ai/langchain | no_demo; severity=medium

4. Security or permission risk: no_demo

  • Severity: medium
  • Finding: no_demo
  • User impact: The project may affect permissions, credentials, data exposure, or host boundaries.
  • Recommended check: Open the linked source, confirm whether it still applies to the current version, and keep the first run isolated.
  • Evidence: risks.scoring_risks | github_repo:552661142 | https://github.com/langchain-ai/langchain | no_demo; severity=medium

5. Maintenance risk: issue_or_pr_quality=unknown

  • Severity: low
  • Finding: issue_or_pr_quality=unknown。
  • User impact: Users cannot judge support quality until recent activity, releases, and issue response are checked.
  • Recommended check: Open the linked source, confirm whether it still applies to the current version, and keep the first run isolated.
  • Evidence: evidence.maintainer_signals | github_repo:552661142 | https://github.com/langchain-ai/langchain | issue_or_pr_quality=unknown

6. Maintenance risk: release_recency=unknown

  • Severity: low
  • Finding: release_recency=unknown。
  • User impact: Users cannot judge support quality until recent activity, releases, and issue response are checked.
  • Recommended check: Open the linked source, confirm whether it still applies to the current version, and keep the first run isolated.
  • Evidence: evidence.maintainer_signals | github_repo:552661142 | https://github.com/langchain-ai/langchain | release_recency=unknown

Source: Doramagic discovery, validation, and Project Pack records

Community Discussion Evidence

These external discussion links are review inputs, not standalone proof that the project is production-ready.

Sources 12

Count of project-level external discussion links exposed on this manual page.

Use Review before install

Open the linked issues or discussions before treating the pack as ready for your environment.

Community Discussion Evidence

Doramagic exposes project-level community discussion separately from official documentation. Review these links before using langchain with real data or production workflows.

Source: Project Pack community evidence and pitfall evidence