Open-Source Wikis

/

LangChain

/

Packages

/

langchain (v1)

langchain-ai/langchain

langchain (v1)

The actively-maintained langchain package. Source lives in libs/langchain_v1/langchain/. PyPI distribution is named langchain, currently at 1.2.16.

Purpose

Three things:

  1. create_agent(...) — the high-level entry point for building tool-using agents on top of langgraph. Source: libs/langchain_v1/langchain/agents/factory.py.
  2. init_chat_model(...) — a provider-agnostic factory that imports the right partner package and returns a configured chat model. Source: libs/langchain_v1/langchain/chat_models/base.py.
  3. The middleware library — a set of composable agent middleware (HITL, summarization, PII redaction, model fallbacks, tool retries, shell tool, todo list, …). Source: libs/langchain_v1/langchain/agents/middleware/.

The package is small (~33 source files) and depends on only langchain-core>=1.3.2, langgraph>=1.1.10, and pydantic>=2.7.4. Provider SDKs are imported lazily — installing langchain alone does not pull in openai, anthropic, or any other partner.

Directory layout

libs/langchain_v1/langchain/
├── __init__.py            # __version__ = "1.2.16"
├── agents/
│   ├── __init__.py        # Re-exports `create_agent`, `AgentState`
│   ├── factory.py         # The agent factory (~1,875 lines)
│   ├── structured_output.py
│   └── middleware/        # Built-in middleware (16 modules)
├── chat_models/
│   └── base.py            # `init_chat_model` and the provider registry
├── embeddings/
│   └── base.py            # `init_embeddings`
├── messages/
│   └── __init__.py        # Re-exports from langchain_core.messages
├── rate_limiters/
│   └── __init__.py        # Re-exports from langchain_core
├── tools/
│   ├── __init__.py
│   └── tool_node.py       # `ToolNode` re-export from langgraph.prebuilt
└── py.typed

Key abstractions

Symbol File Description
create_agent libs/langchain_v1/langchain/agents/factory.py Build a langgraph graph from model + tools + middleware
AgentState libs/langchain_v1/langchain/agents/middleware/types.py The default TypedDict state schema (messages, structured_response, …)
AgentMiddleware libs/langchain_v1/langchain/agents/middleware/types.py Base class for middleware; ~1,700 lines including hook decorators
before_agent, before_model, wrap_model_call, wrap_tool_call, after_model, after_agent libs/langchain_v1/langchain/agents/middleware/types.py Hook decorators
ToolStrategy, ProviderStrategy, AutoStrategy, ResponseFormat libs/langchain_v1/langchain/agents/structured_output.py Structured-output strategies
init_chat_model libs/langchain_v1/langchain/chat_models/base.py Factory dispatching to a partner package
init_embeddings libs/langchain_v1/langchain/embeddings/base.py Embedding-model factory

How create_agent works

graph LR
    A[create_agent args]
    A --> B[Build StateGraph]
    B --> C[before_agent middleware]
    C --> D[before_model middleware]
    D --> E[Model call wrapped by wrap_model_call]
    E --> F[after_model middleware]
    F --> G{tool_calls?}
    G -->|yes| H[ToolNode wrapped by wrap_tool_call]
    H --> D
    G -->|no| I[after_agent middleware]
    I --> J[Final state]

create_agent accepts:

  • A model (string "openai:gpt-5" or a BaseChatModel instance)
  • A list of tools (callables or BaseTool instances)
  • An ordered list of AgentMiddleware instances
  • An optional response_format for structured output
  • An optional state_schema, context_schema, checkpointer, store, cache, pre_model_hook, post_model_hook

It returns a compiled CompiledStateGraph. Every call to agent.invoke({"messages": [...]}) runs the graph until the model produces an AIMessage with no further tool calls.

How init_chat_model works

libs/langchain_v1/langchain/chat_models/base.py keeps a _BUILTIN_PROVIDERS registry mapping provider keys (openai, anthropic, bedrock, groq, ollama, mistralai, google_genai, huggingface, nvidia, cohere, litellm, xai, together, …) to a (module_path, class_name, ctor) tuple. The function:

  1. Parses an optional <provider>:<model> prefix or accepts an explicit model_provider.
  2. Looks up the entry, calls importlib.import_module(module_path) lazily.
  3. Calls the registered constructor with model=<name> plus any extra kwargs.

Most partners use the default _call(cls, **kwargs) constructor; a few (huggingface, ibm) need lambdas because their classes use a different parameter name (model_id instead of model).

Integration points

  • Depends on langchain-core for everything (messages, runnables, tools, callbacks).
  • Depends on langgraph for StateGraph, ToolNode, Command, Send, Runtime.
  • Depends on langsmith (transitively, via langchain-core) for traceable decorators on the agent factory.
  • Lazy-imports every partner package; users who only need OpenAI never load Anthropic.

Entry points for modification

  • To add a built-in middleware, create a new file under libs/langchain_v1/langchain/agents/middleware/ and re-export from middleware/__init__.py. Use the before_*/wrap_*/after_* decorators to wire hooks.
  • To add a new provider to init_chat_model's registry, add an entry to _BUILTIN_PROVIDERS in libs/langchain_v1/langchain/chat_models/base.py. The actual implementation lives in the partner package.
  • To extend the agent state schema, define a TypedDict that extends AgentState and pass it as state_schema= to create_agent.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

langchain (v1) – LangChain wiki | Factory