Skip to contentNewKimi K3.1 Pro is now availableTry it now
Docs / LangChain

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:

bash
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);

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));
}

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);

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" },
  },
});

Verified model IDs

Model IDContext windowTool callingModel page
deepseek-v4.1-flash-10171,040,000YesDeepSeek V4.1 Flash
glm-5.31,048,576YesGLM 5.3
glm-5.3-flash1,048,576YesGLM 5.3 Flash
moonshotai/kimi-k3.1-pro1,048,576YesKimi K3.1 Pro
inference-net/schematron-v2-turbo128,000NoSchematron V2 Turbo

Optional: US-only routing and BYOK passthrough

  • US-only endpoint: Replace https://api.inference.net/v1 with https://us.api.inference.net/v1 for models displaying the US badge 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-provider and x-inference-provider-api-key in default_headers (Python) or configuration.defaultHeaders (TypeScript). See API quickstart.

Troubleshooting

  • TypeScript baseURL ignored: In @langchain/openai, pass baseURL inside configuration: { baseURL: "https://api.inference.net/v1" } rather than as a top-level option.
  • 401 Unauthorized: Check that apiKey / api_key uses INFERENCE_API_KEY, not OPENAI_API_KEY.
  • 404 or HTML response: Verify the base URL is https://api.inference.net/v1 (with /v1), not https://inference.net/v1.
  • Model not found: Copy the exact id from GET https://api.inference.net/v1/models, including owner prefixes such as moonshotai/kimi-k3.1-pro.

Explore all supported editors, gateways, and SDKs in the Integrations hub.