LangChain

Wire LangChain's ChatOpenAI to the gateway as a custom baseURL.

Integrations

LangChain's ChatOpenAI accepts a configuration.baseURL option. Wire the gateway in once and reuse the chain everywhere.

Config

ts
import { ChatOpenAI } from "@langchain/openai";const llm = new ChatOpenAI({modelName: "gpt-5.5",configuration: { baseURL: "https://api.toygate.store/v1" },apiKey: process.env.GATEWAY_KEY,});

Use it in a chain

import { ChatPromptTemplate } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromMessages([
  ["system", "You are a helpful assistant."],
  ["human", "{question}"],
]);

const chain = prompt.pipe(llm);
const answer = await chain.invoke({ question: "What is RAG?" });
console.log(answer.content);

Streaming

LangChain's standard streaming API forwards through to the gateway's SSE response:

const stream = await chain.stream({ question: "Stream a sentence." });
for await (const chunk of stream) {
  process.stdout.write(chunk.content as string);
}

Tools

ChatOpenAI.bindTools() works as long as the underlying model has capabilities.tools = true on the catalog page. The gateway translates tool definitions to the upstream format and reverses for the response.

Troubleshooting

  • Invalid request to OpenAI in LangChain logs — LangChain wraps the gateway's 400 with its own message. Read the embedded error.code (e.g. upstream_bad_request) for the real reason.
  • OpenAIChat is deprecated — make sure you're using @langchain/openai's ChatOpenAI, not the legacy OpenAIChat from older releases.

Gateway URL: https://api.toygate.store