How to Use DeepSeek API with LangChain (Python Tutorial)

Step-by-step guide to integrating DeepSeek-v3.2 into your LangChain applications via NovAI

LangChain is the most popular framework for building LLM-powered applications. Since NovAI provides an OpenAI-compatible API, integrating DeepSeek into LangChain takes just a few lines of code.

Prerequisites

Basic Chat with DeepSeek via LangChain

from langchain_openai import ChatOpenAI

# Point LangChain to NovAI's endpoint
llm = ChatOpenAI(
    model="deepseek-v3.2",
    openai_api_key="your-novai-api-key",
    openai_api_base="https://aiapi-pro.com/v1",
    temperature=0.7,
)

response = llm.invoke("Explain the difference between RAG and fine-tuning")
print(response.content)

Using DeepSeek in a LangChain Chain

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(
    model="deepseek-v3.2",
    openai_api_key="your-novai-api-key",
    openai_api_base="https://aiapi-pro.com/v1",
)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expert {topic} tutor. Explain concepts clearly."),
    ("human", "{question}"),
])

chain = prompt | llm
result = chain.invoke({"topic": "Python", "question": "What are decorators?"})
print(result.content)

Streaming Responses

for chunk in llm.stream("Write a Python quicksort implementation"):
    print(chunk.content, end="", flush=True)

Switching Models On-the-Fly

Since NovAI hosts multiple models, you can easily switch between them:

# Use DeepSeek for code tasks
code_llm = ChatOpenAI(model="deepseek-v3.2", openai_api_key=KEY, openai_api_base=BASE)

# Use MiniMax for long-context tasks (1M tokens)
long_llm = ChatOpenAI(model="minimax-text-01", openai_api_key=KEY, openai_api_base=BASE)

# Use GLM Flash for free prototyping
free_llm = ChatOpenAI(model="glm-4.6v-flash", openai_api_key=KEY, openai_api_base=BASE)

Same API key, same endpoint, just change the model parameter.

DeepSeek from $0.20/1M tokens — 10x cheaper than GPT-4o
Compare all model pricing side by side
View Full Pricing →

Get Your NovAI API Key

Free signup. Works with LangChain, LlamaIndex, and any OpenAI-compatible tool.

Start Free →

Related Articles

DeepSeek Python Tutorial → DeepSeek vs Claude → AI Agent Pipelines → DeepSeek in Cursor IDE →
NovAI — AI API from $0.05/1M tokens Get Free API Key → View Pricing