LangChain Integration with DeepSeek
This chapter introduces how to integrate and use the DeepSeek chat model in LangChain.
Pass langchain-deepseek With the extension package, developers can quickly connect to the large language model services provided by DeepSeek.
DeepSeek models support both the official hosted API and local or third-party inference deployment through platforms such as Ollama, Fireworks, and Together.
In addition to the official API interface, we can alsoCoding Plan/Token Planpackage to directly access mainstream large models such as DeepSeek, Kimi, GLM, Doubao, MiniMax, without needing to purchase each API separately.
Introduction to DeepSeek
DeepSeek is an open-source large language model family that supports capabilities such as chat, reasoning, and code generation.
In LangChain, DeepSeek is mainly called through the ChatDeepSeek class.
Integration information:
| Projects | Description |
|---|---|
| Class Names | ChatDeepSeek |
| Install packages | langchain-deepseek |
| State | Beta (test version) |
| JavaScript supported | Is |
| Python supported | Is |
Model capability support:
| Function | Supported |
|---|---|
| Tool Calling | Supported |
| Structured Output | Supported |
| Image input | Not supported |
| Audio input | Not supported |
| Video input | Not supported |
| Token streaming output | Supported |
| Native async calls | Supported |
| Token usage statistics | Supported |
| Logprobs | Not supported |
The DeepSeek API uses a format compatible with OpenAI/Anthropic APIs. By modifying the configuration, you can use OpenAI/Anthropic SDKs to access the DeepSeek API, or use software compatible with the OpenAI/Anthropic APIs.
| Parameters | Value |
|---|---|
| base_url (OpenAI) | https://api.deepseek.com |
| base_url (Anthropic) | https://api.deepseek.com/anthropic |
| api_key | Click the link to apply API key |
| model* | deepseek-v4-flash |
`deepseek-v4-pro`
`deepseek-chat` (将于 2026/07/24 弃用)
`deepseek-reasoner` (将于 2026/07/24 弃用) |
Install langchain-deepseek
Before starting, you need to install DeepSeek's LangChain integration package:
pip install -qU langchain-deepseek
Configure DeepSeek API Key
You need to first register a DeepSeek account and create an API Key. If you don't have one, go to https://platform.deepseek.com/api_keys to create an API key.
It is usually recommended to use python-dotenv to read the .env configuration file in the current directory.
pip install python-dotenv
Create a in the current project directory .env File:
.env file configuration:
DEEPSEEK_API_KEY
=
"sk-xxxxxxxxxxxxxxxx"
OPENAI_API_KEY
=
"sk-xxxxxxxxxxxxxxxx"

Reading the .env file in Python:
Complete example
import
os
from
dotenv
import
load_dotenv
Load the .env file in the current directory
load_dotenv
(
)
Get an API Key
api_key
=
os
.
getenv
(
"DEEPSEEK_API_KEY"
)
(
api_key
)
Specify the .env file path: If the .env file is not in the current directory, you can specify the path manually:
Example
from
dotenv
import
load_dotenv
load_dotenv
(
dotenv_path
=
"./config/.env"
)
Configure LangSmith (optional)
If you need to enable LangChain chain tracing and debugging, you can configure a LangSmith API Key:
Example
os
.
environ
[
"LANGSMITH_TRACING"
]
=
"true"
os
.
environ
[
"LANGSMITH_API_KEY"
]
=
getpass
.
getpass
(
"Enter your LangSmith API key: "
)
Create a ChatDeepSeek model
After installation, you can ChatDeepSeek initialize the model:
Example
import
os
from
dotenv
import
load_dotenv
from
langchain_deepseek
import
ChatDeepSeek
Load .env
load_dotenv
(
)
Get API KEY
api_key
=
os
.
getenv
(
"DEEPSEEK_API_KEY"
)
Create the model
llm
=
ChatDeepSeek
(
api_key
=
api_key
,
model
=
"deepseek-v4-flash"
,
temperature
=
0
,
max_tokens
=
None
,
timeout
=
None
,
max_retries
=
2
)
Call the model
response
=
llm.
invoke
(
"Hello, please introduce LangChain"
)
(
response.
content
)
Parameter description:
| Parameters | Description |
|---|---|
| api_key | Set the API key you applied for, e.g. "sk-xxx" |
| model | Specify the model name, e.g. deepseek-v4-flash |
| temperature | Control randomness; the lower it is, the more stable the results |
| max_tokens | Limit the maximum number of generated Tokens |
| timeout | Request timeout |
| max_retries | Maximum number of retries after failure |
You can also use init_chat_model() function to call:
Example
import
os
from
dotenv
import
load_dotenv
Load the .env file in the current directory
load_dotenv
(
)
from
langchain.
chat_models
import
init_chat_model
When model is specified, a fixed model is returned
model
=
init_chat_model
(
"deepseek:deepseek-v4-flash"
,
temperature
=
0.7
)
response
=
model.
invoke
(
"Introduce Runoob Tutorial RUNOOB"
)
(
response.
content
)
Call the DeepSeek model
The following example demonstrates how to send chat messages to the DeepSeek model:
Note that in my test here, I wrote the API Key in the test file:
llm = ChatDeepSeek( api_key="sk-xxx", # Set your DeepSeek API Key model="deepseek-v4-flash" )
In a real production environment, please set it in .env file.
Example
from
langchain_deepseek
import
ChatDeepSeek
llm
=
ChatDeepSeek
(
api_key
=
"sk-xxx"
,
Set your DeepSeek API Key
model
=
"deepseek-v4-flash"
,
temperature
=
0
,
max_tokens
=
None
,
timeout
=
None
,
max_retries
=
2
)
messages
=
[
(
"system"
,
"You are a helpful assistant that translates English to Chinese."
)
,
(
"human"
,
"I love programming."
)
,
]
ai_msg
=
llm.
invoke
(
messages
)
(
ai_msg.
content
)
The above code translates English into Chinese; after running, the model returns the translated result:
我喜欢编程。
Supported DeepSeek models:
| Model | Purpose | Feature |
|---|---|---|
| deepseek-v4-flash | General chat models | Support Tool Calling and structured output |
| deepseek-v4-pro | Reasoning models (deepseek-v4-pro) | Stronger reasoning ability, but no Tool Calling support |
LangChain + DeepSeek complete test example
The following demonstrates a complete, runnable LangChain + DeepSeek example.
Create the test file
Create a test.py file:
Complete example
from
langchain_deepseek
import
ChatDeepSeek
from
langchain_core.
messages
import
HumanMessage
,
SystemMessage
=========================
Configure DeepSeek API Key
=========================
apiKey
=
"sk-xxx"
Set your DeepSeek API Key
=========================
Create the DeepSeek model
=========================
llm
=
ChatDeepSeek
(
api_key
=
apiKey
,
model
=
"deepseek-v4-flash"
,
temperature
=
0.7
,
max_tokens
=
1024
,
timeout
=
60
,
max_retries
=
3
)
=========================
Construct chat messages
=========================
messages
=
[
SystemMessage
(
content
=
"You are a professional Python teacher."
)
,
HumanMessage
(
content
=
"Please explain what LangChain is and give a simple example."
)
]
=========================
Call the model
=========================
response
=
llm.
invoke
(
messages
)
=========================
Output result
=========================
(
"AI reply:"
)
(
response.
content
)
Execute in the terminal:
python test.py
The output is as follows:

Code explanation:
| Code | Purpose |
|---|---|
| ChatDeepSeek | LangChain's DeepSeek chat model class |
| SystemMessage | System prompt, used to set the AI's identity |
| HumanMessage | User input message |
| llm.invoke() | Call the DeepSeek model |
| response.content | Get the content returned by the AI |
Use the deepseek-v4-pro reasoning model
LangChain supports DeepSeek streaming output, which requires the deepseek-v4-pro reasoning model:
Example
from
langchain_deepseek
import
ChatDeepSeek
from
langchain_core.
messages
import
HumanMessage
,
SystemMessage
=========================
Configure DeepSeek API Key
=========================
apiKey
=
"sk-xxx"
Set your DeepSeek API Key
=========================
Create the DeepSeek model
=========================
llm
=
ChatDeepSeek
(
api_key
=
apiKey
,
model
=
"deepseek-v4-pro"
,
temperature
=
0.7
,
max_tokens
=
1024
,
timeout
=
60
,
max_retries
=
3
)
for
chunk
in
llm.
stream
(
"Please introduce Python"
)
:
(
chunk
.
content
,
end
=
""
,
flush
=
True
)
Using PromptTemplate
Dynamically generating a Prompt with PromptTemplate:
Example
from
langchain_deepseek
import
ChatDeepSeek
from
langchain_core.
prompts
import
ChatPromptTemplate
=========================
Configure DeepSeek API Key
=========================
apiKey
=
"sk-xxx"
Set your DeepSeek API Key
=========================
Create the DeepSeek model
=========================
llm
=
ChatDeepSeek
(
api_key
=
apiKey
,
model
=
"deepseek-v4-pro"
,
temperature
=
0.7
,
max_tokens
=
1024
,
timeout
=
60
,
max_retries
=
3
)
prompt
=
ChatPromptTemplate.
from_template
(
"Please explain in detail: {topic}"
)
chain
=
prompt | llm
response
=
chain.
invoke
(
{
"topic"
:
"Transformer"
}
)
(
response.
content
)
Common mistakes
| Error | Reason | Solution |
|---|---|---|
| 401 Unauthorized | API Key error | Check DEEPSEEK_API_KEY |
| ModuleNotFoundError | Dependency not installed | Re-run pip install |
| Rate Limit | Request rate too high | Reduce the request rate |
| Timeout Error | Request timeout | Increase the timeout parameter |
Recommended project structure
Project Structure
project
/
│
├── app.py
├── requirements.txt
├── .env
├── prompts
/
├── data
/
└── vector_db
/
requirements.txt example
Example
langchain
langchain-deepseek
python-dotenv
Reference Documentation
- LangChain DeepSeek documentation: https://python.langchain.com/docs/integrations/chat/deepseek/
- DeepSeek official website: https://www.deepseek.com/
- LangChain official website: https://www.langchain.com/
- LangSmith: https://smith.langchain.com/