YouTip LogoYouTip

Langchain Deepseek

This chapter introduces how to integrate and use the DeepSeek chat model in LangChain. Through the **langchain-deepseek** extension package, developers can quickly access the large language model services provided by DeepSeek. DeepSeek models support both official hosted APIs and local or third-party inference deployment through platforms like Ollama, Fireworks, and Together. > In addition to the official API interface, we can also directly access mainstream large models such as DeepSeek, Kimi, GLM, Doubao, and MiniMax through the [Coding Plan/Token Plan](#) package, without purchasing each vendor's API separately. ### DeepSeek Introduction DeepSeek is an open-source large language model series that supports chat, reasoning, code generation, and other capabilities. In LangChain, DeepSeek is primarily called through the **ChatDeepSeek** class. Integration Information: | Project | Description | | --- | --- | | Class Name | ChatDeepSeek | | Installation Package | langchain-deepseek | | Status | Beta | | Supports JavaScript | Yes | | Supports Python | Yes | Model Capability Support: | Feature | 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 Call | βœ… Supported | | Token Usage Statistics | βœ… Supported | | Logprobs | ❌ Not Supported | DeepSeek API uses OpenAI/Anthropic compatible API format. By modifying the configuration, you can use OpenAI/Anthropic SDK to access DeepSeek API, or use software compatible with OpenAI/Anthropic API. | Parameter | Value | | --- | --- | | base_url (OpenAI) | `https://api.deepseek.com` | | base_url (Anthropic) | `https://api.deepseek.com/anthropic` | | api_key | Click the link to apply for (https://platform.deepseek.com/api_keys) | | model* | `deepseek-v4-flash` `deepseek-v4-pro` `deepseek-chat` (will be deprecated on 2026/07/24) `deepseek-reasoner` (will be deprecated on 2026/07/24) | * * * ## Install langchain-deepseek Before starting to use, you need to install the DeepSeek LangChain integration package: pip install -qU langchain-deepseek * * * ## Configure DeepSeek API Key You need to register a DeepSeek account first and create an API Key. If you don't have one yet, go to [https://platform.deepseek.com/api_keys](https://platform.deepseek.com/api_keys) to create an API key. It is recommended to use **python-dotenv** to read the .env configuration file in the current directory. pip install python-dotenv Create a **.env** file in the current project directory: ## .env File Configuration: DEEPSEEK_API_KEY="sk-xxxxxxxxxxxxxxxx" OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxx" !(#) Python reads .env file: ## Complete Example import os from dotenv import load_dotenv # Load .env file in current directory load_dotenv() # Get API Key api_key =os.getenv("DEEPSEEK_API_KEY") print(api_key) **Specify .env file path:** If the .env file is not in the current directory, you can manually specify the path: ## Example from dotenv import load_dotenv load_dotenv(dotenv_path="./config/.env") ### Configure LangSmith (Optional) If you need to enable LangChain call chain tracking and debugging features, you can configure LangSmith API Key: ## Example os.environ="true" os.environ=getpass.getpass( "Enter your LangSmith API key: " ) ### Create ChatDeepSeek Model After installation, you can initialize the model through **ChatDeepSeek**: ## 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 model llm = ChatDeepSeek( api_key=api_key, model="deepseek-v4-flash", temperature=0, max_tokens=None, timeout=None, max_retries=2 ) # Call model response = llm.invoke("Hello, please introduce LangChain") print(response.content) Parameter Description: | Parameter | Description | | --- | --- | | api_key | Set your applied API key, e.g., "sk-xxx" | | model | Specify model name, e.g., deepseek-v4-flash | | temperature | Control randomness, lower values result in more stable outputs | | max_tokens | Limit the maximum number of Tokens to generate | | timeout | Request timeout | | max_retries | Maximum retry attempts after failure | You can also use the [init_chat_model()](#) function to call it: ## Example import os from dotenv import load_dotenv # Load .env file in current directory load_dotenv() from langchain.chat_models import init_chat_model # Specify model, returns fixed model model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0.7) response = model.invoke("Introduce TUTORIAL") print(response.content) * * * ## Call DeepSeek Model The following example demonstrates how to send chat messages to the DeepSeek model: > Note: I wrote the API Key in the test file here for testing: > > llm = ChatDeepSeek( api_key="sk-xxx", # Set your DeepSeek API Key model="deepseek-v4-flash") > In actual production environment, please set it in the .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) print(ai_msg.content) After running, the model will return the French translation result. I love programming. Supported DeepSeek Models: | Model | Usage | Features | | --- | --- | --- | | deepseek-v4-flash | General chat model | Supports Tool Calling and structured output | | deepseek-v4-pro | Reasoning model (deepseek-v4-pro) | Stronger reasoning capability, but does not support Tool Calling | ## LangChain + DeepSeek Complete Test Example Below demonstrates a complete runnable LangChain + DeepSeek example. ### Create Test File Create 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 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 instructor." ), HumanMessage( content="Please explain what LangChain is and provide a simple example." ) ] # ========================= # Call Model # ========================= response = llm.invoke(messages) # ========================= # Output Result # ========================= print("AI Reply:") print(response.content) Execute in terminal: python test.py Output is as follows: !(#) Code Description: | Code | Function | | --- | --- | | ChatDeepSeek | DeepSeek chat model class in LangChain | | SystemMessage | System prompt, used to set AI identity | | HumanMessage | User input message | | llm.invoke() | Call DeepSeek model | | response.content | Get AI returned content | ### Use deepseek-v4-pro Reasoning Model LangChain supports DeepSeek streaming output, you need to use the deepseek-v4-pro reasoning model
← Langchain Init_Chat_ModeLangchain Intro β†’