LangChain integration

LangChain reaches Modellane through its OpenAI chat model class, which calls the Chat Completions API (POST /v1/chat/completions). Set the base URL and key once, then use the model in chains, tools and agents.

Install#

Python

pip install langchain-openai

Node.js

npm install @langchain/openai @langchain/core

Create the chat model#

Python

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="deepseek-v4.1-flash",
    api_key=os.environ["MODELLANE_API_KEY"],
    base_url="https://usemodellane.com/v1",
    stream_usage=True,
)

print(llm.invoke("Explain recursion in one sentence.").content)

Node.js

import { ChatOpenAI } from "@langchain/openai"

const llm = new ChatOpenAI({
  model: "deepseek-v4.1-flash",
  apiKey: process.env.MODELLANE_API_KEY,
  configuration: { baseURL: "https://usemodellane.com/v1" },
  streamUsage: true,
})

const reply = await llm.invoke("Explain recursion in one sentence.")
console.log(reply.content)

stream_usage (Python) and streamUsage (Node.js) ask for the usage chunk on streamed calls, so LangChain callbacks and tracing see real token counts.

Streaming#

Python

for chunk in llm.stream("Write a short poem about rain."):
    print(chunk.content, end="", flush=True)

Node.js

for await (const chunk of await llm.stream("Write a short poem about rain.")) {
  process.stdout.write(String(chunk.content))
}

Tool calling#

On models that list Tool calls in Models & pricing, bind_tools works as with any OpenAI model.

Python

from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    """Return the current weather for a city."""
    return f"Sunny in {city}"

reply = llm.bind_tools([get_weather]).invoke("What is the weather in Paris?")
print(reply.tool_calls)

Your code runs the tool and sends the result back in a tool message; see Tool calls.

Things to keep in mind#

  • Stay on Chat Completions. Leave use_responses_api (Python) and useResponsesApi (Node.js) off, and do not bind OpenAI's built-in tools such as web search or file search: they run on OpenAI's servers, and LangChain can switch to the Responses API when they are used.
  • Reasoning text is not kept. LangChain's OpenAI class does not keep non-standard response fields such as reasoning_content, so a model's reasoning does not show up in the message. The answer and the token usage are not affected.
  • Context length. Trim long histories so they fit the context length shown in Models & pricing.

Verify#

Run the invoke example above. A printed reply means the base URL, key and model id are right; an authentication error points at the key, and model_not_found at the model id.

Images#