Skip to main content

LangChain connector

LangChain is a popular Python toolkit for building apps on top of AI models. You join small pieces (a prompt, a model, a parser) into a chain.

ChatNuRoute is the piece that plugs NuRoute in as the model. It comes in the official package langchain-nuroute. It is a normal LangChain BaseChatModel, so it works with everything else in LangChain (prompts, output parsers, tools, LCEL pipes) exactly like ChatOpenAI or ChatAnthropic would.

Your tool uses the NuRoute connector, which talks to your NuRoute gateway

Before you start​

  • Python 3.9 or newer.
  • Your NuRoute gateway address.
  • An API key that starts with aicp-.

Step 1: Install​

pip install langchain-nuroute

Step 2: Say hello​

from langchain_nuroute import ChatNuRoute
from langchain_core.messages import HumanMessage

llm = ChatNuRoute(
nuroute_api_key="aicp-...",
nuroute_base_url="https://your-nuroute-gateway",
model="auto", # NuRoute's routing engine picks the best model — see Concepts > Routing
)

response = llm.invoke([HumanMessage(content="Hello!")])
print(response.content)

With model="auto", Artemis picks the model for each request.

Use it in a chain​

Because ChatNuRoute is a normal chat model, it works in a chain like any other:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("Summarize this in one sentence:\n\n{text}")
chain = prompt | llm

print(chain.invoke({"text": "..."}).content)

Streaming​

Streaming shows the answer as it is written, piece by piece, instead of waiting for all of it.

for chunk in llm.stream([HumanMessage(content="Tell me a short story")]):
print(chunk.content, end="", flush=True)

Forcing a provider or model​

Give a specific model and a provider to fix both. With model="auto" and a provider, no router runs: the provider serves the first model it lists.

llm = ChatNuRoute(
nuroute_api_key="aicp-...",
nuroute_base_url="https://your-nuroute-gateway",
model="claude-sonnet-4-6",
provider="anthropic",
temperature=0.2,
max_tokens=1024,
)

Reading which model answered​

Every response says which model NuRoute actually used, and how many tokens the call took. When you call generate directly, find them in llm_output:

result = llm.generate([[HumanMessage(content="Hello!")]])
print(result.llm_output["model"]) # e.g. "gpt-4o-mini"
print(result.llm_output["usage"]) # {"prompt_tokens": ..., "completion_tokens": ..., "total_tokens": ...}

Error handling​

When the gateway returns an error, ChatNuRoute raises nuroute.NuRouteError, the same exception the SDK raises. It has status, error_type and code fields. It is not an OpenAI-shaped exception.

from nuroute import NuRouteError

try:
llm.invoke([HumanMessage(content="Hello!")])
except NuRouteError as e:
print(e.status, e.error_type, e.message)

Why use ChatNuRoute and not ChatOpenAI?​

ChatNuRoute calls your gateway through the nuroute Python SDK, not through an OpenAI-lookalike layer.

Pointing LangChain's own ChatOpenAI at the gateway's OpenAI-compatible /v1/chat/completions address also works, because the gateway accepts OpenAI-shaped requests. But that squeezes NuRoute's special features into OpenAI's answer and error shapes. ChatNuRoute keeps them:

  • the "auto" routing model,
  • provider overrides, to pin one upstream provider,
  • gateway errors arriving as NuRouteError.

What is not supported​

ChatNuRoute does not accept a stop setting. The gateway's /v1/chat/completions endpoint has no stop-sequence support today, so passing one raises NotImplementedError instead of being quietly ignored.