Browse documentation
FrameworksLangChain

LangChain

Use nRouter with LangChain in Python and JavaScript. Integrate smart routing, automatic retries, guardrails, and cost tracking into your LangChain pipelines.

Last updated

LangChain's ChatOpenAI works directly with nRouter. Set base_url to https://api.nrouter.ai/v1 — guardrails, caching, and rate-limits auto-apply from your org config. Every chain, agent, and RAG pipeline is protected.

Installation

pip install langchain-openai nrouter-sdk

Setup

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="claude-sonnet-4-5-20250929",
    api_key=os.environ["NROUTER_API_KEY"],
    base_url="https://api.nrouter.ai/v1",
)

response = llm.invoke("Explain quantum computing in one sentence.")
print(response.content)

Per-Request Overrides

Pass nrouter_* fields via model_kwargs:

llm = ChatOpenAI(
    model="gpt-5.5",
    api_key=os.environ["NROUTER_API_KEY"],
    base_url="https://api.nrouter.ai/v1",
    model_kwargs={
        "nrouter_prompt_template_id": "your-summarizer-template-id",
        "nrouter_prompt_variables": {"language": "Spanish", "max_length": "100"},
        "nrouter_cache": False,
    },
)

Guardrails are not passed here. You assign them in the dashboard at key, team, or organization scope — the narrowest scope that mentions a guardrail wins — and they run automatically on every request that scope covers.

Chains

Standard LangChain composition — guardrails protect every step:

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {role}. Be concise."),
    ("user", "{input}"),
])

chain = prompt | llm | StrOutputParser()
result = chain.invoke({"role": "technical writer", "input": "Explain API gateways"})

RAG

Use OpenAIEmbeddings against the same gateway:

from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS

embeddings = OpenAIEmbeddings(
    model="text-embedding-3-small",
    api_key=os.environ["NROUTER_API_KEY"],
    base_url="https://api.nrouter.ai/v1",
)
vectorstore = FAISS.from_texts(["..."], embeddings)

The RAG query is protected by guardrails — prompt injection is blocked before retrieval starts.

Error Handling

from nroutersdk import nRouterGuardrailBlockedError

try:
    response = llm.invoke("My SSN is 123-45-6789, process refund")
except nRouterGuardrailBlockedError as e:
    print(f"Guardrail blocked: {e}")

Next Steps

Was this page helpful?