LangChain
Connect LangChain ChatOpenAI in Python and TypeScript to Inference.net for streaming, tool calling, and structured outputs.
Last updated
Connect LangChain to Inference.net using ChatOpenAI in Python (langchain-openai) or TypeScript (@langchain/openai) by pointing the base URL at https://api.inference.net/v1 and authenticating with your INFERENCE_API_KEY. No downstream provider key is required for models hosted on Inference.net.
Quick start
Create an account at /register, generate a project key in API keys, and export it:
export INFERENCE_API_KEY="inf_sk_..."Install the LangChain OpenAI package and run your first completion:
// npm install @langchain/openai @langchain/core
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "deepseek-v4.1-flash-1017",
apiKey: process.env.INFERENCE_API_KEY,
configuration: {
baseURL: "https://api.inference.net/v1",
},
});
const response = await model.invoke(
"Explain KV cache reuse in two sentences.",
);
console.log(response.content);# pip install langchain-openai
import os
from langchain_openai import ChatOpenAI
model = ChatOpenAI(
model="deepseek-v4.1-flash-1017",
base_url="https://api.inference.net/v1",
api_key=os.environ["INFERENCE_API_KEY"],
)
response = model.invoke("Explain KV cache reuse in two sentences.")
print(response.content)Stream responses
Both Python and TypeScript ChatOpenAI clients stream tokens directly from https://api.inference.net/v1:
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "glm-5.3",
apiKey: process.env.INFERENCE_API_KEY,
configuration: {
baseURL: "https://api.inference.net/v1",
},
});
const stream = await model.stream("List three steps to optimize LLM latency.");
for await (const chunk of stream) {
process.stdout.write(String(chunk.content));
}import os
from langchain_openai import ChatOpenAI
model = ChatOpenAI(
model="glm-5.3",
base_url="https://api.inference.net/v1",
api_key=os.environ["INFERENCE_API_KEY"],
)
for chunk in model.stream("List three steps to optimize LLM latency."):
print(chunk.content, end="", flush=True)Tool calling and structured output
Use any model with supports_tools: true in GET https://api.inference.net/v1/models (such as deepseek-v4.1-flash-1017, glm-5.3, or moonshotai/kimi-k3.1-pro) with bindTools / bind_tools or withStructuredOutput / with_structured_output.
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";
const model = new ChatOpenAI({
model: "deepseek-v4.1-flash-1017",
apiKey: process.env.INFERENCE_API_KEY,
configuration: {
baseURL: "https://api.inference.net/v1",
},
});
const TicketSummary = z.object({
category: z.enum(["billing", "technical", "account"]),
summary: z.string(),
});
const structured = model.withStructuredOutput(TicketSummary);
const result = await structured.invoke(
"Customer reports 401 errors after rotating their API key.",
);
console.log(result);import os
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field
class TicketSummary(BaseModel):
category: str = Field(description="One of billing, technical, or account")
summary: str
model = ChatOpenAI(
model="deepseek-v4.1-flash-1017",
base_url="https://api.inference.net/v1",
api_key=os.environ["INFERENCE_API_KEY"],
)
structured = model.with_structured_output(TicketSummary)
result = structured.invoke(
"Customer reports 401 errors after rotating their API key."
)
print(result)See Function calling and Structured outputs for full parameter support.
Tag requests with task and environment headers
Attach x-inference-task-id and x-inference-environment at client creation time or per call to filter metrics in Usage. Note that LangChain's per-call header option differs between TypeScript and Python:
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "deepseek-v4.1-flash-1017",
apiKey: process.env.INFERENCE_API_KEY,
configuration: {
baseURL: "https://api.inference.net/v1",
defaultHeaders: {
"x-inference-environment": "production",
},
},
});
const response = await model.invoke("Summarize this release note.", {
options: {
headers: { "x-inference-task-id": "release-summary" },
},
});import os
from langchain_openai import ChatOpenAI
model = ChatOpenAI(
model="deepseek-v4.1-flash-1017",
base_url="https://api.inference.net/v1",
api_key=os.environ["INFERENCE_API_KEY"],
default_headers={
"x-inference-environment": "production",
},
)
response = model.invoke(
"Summarize this release note.",
extra_headers={"x-inference-task-id": "release-summary"},
)Verified model IDs
| Model ID | Context window | Tool calling | Model page |
|---|---|---|---|
deepseek-v4.1-flash-1017 | 1,040,000 | Yes | DeepSeek V4.1 Flash |
glm-5.3 | 1,048,576 | Yes | GLM 5.3 |
glm-5.3-flash | 1,048,576 | Yes | GLM 5.3 Flash |
moonshotai/kimi-k3.1-pro | 1,048,576 | Yes | Kimi K3.1 Pro |
inference-net/schematron-v2-turbo | 128,000 | No | Schematron V2 Turbo |
Optional: US-only routing and BYOK passthrough
- US-only endpoint: Replace
https://api.inference.net/v1withhttps://us.api.inference.net/v1for models displaying theUSbadge in the model catalog. - Bring your own provider key: To proxy requests to OpenAI or another provider through the Inference.net Gateway with a credit-billing API key, pass
x-inference-providerandx-inference-provider-api-keyindefault_headers(Python) orconfiguration.defaultHeaders(TypeScript). See API quickstart.
Troubleshooting
- TypeScript
baseURLignored: In@langchain/openai, passbaseURLinsideconfiguration: { baseURL: "https://api.inference.net/v1" }rather than as a top-level option. 401 Unauthorized: Check thatapiKey/api_keyusesINFERENCE_API_KEY, notOPENAI_API_KEY.404or HTML response: Verify the base URL ishttps://api.inference.net/v1(with/v1), nothttps://inference.net/v1.- Model not found: Copy the exact
idfromGET https://api.inference.net/v1/models, including owner prefixes such asmoonshotai/kimi-k3.1-pro.
Explore all supported editors, gateways, and SDKs in the Integrations hub.