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:
create_agent(...)— the high-level entry point for building tool-using agents on top oflanggraph. Source:libs/langchain_v1/langchain/agents/factory.py.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.- 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.typedKey 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 aBaseChatModelinstance) - A list of tools (callables or
BaseToolinstances) - An ordered list of
AgentMiddlewareinstances - An optional
response_formatfor 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:
- Parses an optional
<provider>:<model>prefix or accepts an explicitmodel_provider. - Looks up the entry, calls
importlib.import_module(module_path)lazily. - 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-corefor everything (messages, runnables, tools, callbacks). - Depends on
langgraphforStateGraph,ToolNode,Command,Send,Runtime. - Depends on
langsmith(transitively, vialangchain-core) fortraceabledecorators 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 frommiddleware/__init__.py. Use thebefore_*/wrap_*/after_*decorators to wire hooks. - To add a new provider to
init_chat_model's registry, add an entry to_BUILTIN_PROVIDERSinlibs/langchain_v1/langchain/chat_models/base.py. The actual implementation lives in the partner package. - To extend the agent state schema, define a
TypedDictthat extendsAgentStateand pass it asstate_schema=tocreate_agent.
Related
- features/agents
- features/middleware
- features/structured-output
- packages/core — the abstractions this package builds on
- partners/index — the providers
init_chat_modeldispatches to
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.