Skip to content

Connect LangChain to TrustedRails

LangChain is a framework for building applications on top of language models. Its OpenAI chat-model integration accepts a custom base URL, so you can point it at TrustedRails and build on open-source models — the only change from a standard OpenAI setup is the base URL, the key, and the model id.

The integration is the same in Python and JavaScript/TypeScript; only the package name and the constructor differ. Pick your language below.

Install the OpenAI integration package:

Terminal window
pip install langchain-openai

Point ChatOpenAI at TrustedRails:

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="moonshotai/Kimi-K2.6",
base_url="https://proxy.trustedrails.com/v1",
api_key="tr-prx-your-api-key",
)
response = llm.invoke("Say hello from TrustedRails.")
print(response.content)

The model must match a TrustedRails-supported id exactly — for example moonshotai/Kimi-K2.6 or MiniMaxAI/MiniMax-M2.7 (see Supported Models). Once the model is configured, use it anywhere a LangChain chat model goes — chains, agents, and LCEL pipelines all work unchanged.

Run the snippet above. A printed reply confirms LangChain is reaching TrustedRails through your key.

  • 401 / invalid API key — wrong or paused key. Create a fresh one from Create a TrustedRails API Key.
  • Model not found / unsupported — the model must match a model TrustedRails serves exactly (see Supported Models).
  • Connection errors — confirm the base URL is exactly https://proxy.trustedrails.com/v1.
  • Visible <think> reasoning in the output — reasoning models such as MiniMaxAI/MiniMax-M2.7 emit their thinking inline, which LangChain returns verbatim in content (wrapped in <think>…</think>). The example uses moonshotai/Kimi-K2.6, which returns clean text; strip the <think> block yourself if you prefer a reasoning model.