Loading
Skip to content
RedRouterRedRouter
Dashboard ↗

LangChain

Use RedRouter as the chat model in a LangChain pipeline through ChatOpenAI. This is a custom endpoint recipe, not a native RedRouter integration or a live-tested result.

Install and run a pipeline

python -m pip install langchain-openai
export REDROUTER_API_KEY="your-redrouter-api-key"

Save as example.py:

import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from openai import APIConnectionError, APIStatusError

model = ChatOpenAI(
    model="red-cyber-1",
    api_key=os.environ["REDROUTER_API_KEY"],
    base_url="https://redrouter.org/v1",
    use_responses_api=False,
    max_tokens=512,
    timeout=120,
    max_retries=0,
)
prompt = ChatPromptTemplate.from_messages([
    ("system", "Explain technical concepts clearly in two sentences."),
    ("human", "Explain {topic}."),
])
chain = prompt | model | StrOutputParser()

try:
    print(chain.invoke({"topic": "optimistic locking"}))
except APIStatusError as error:
    raise SystemExit(f"RedRouter HTTP {error.status_code}; inspect the API error.")
except APIConnectionError:
    raise SystemExit("Connection failed; generation may already have started.")

Run python example.py. Expected result: a short explanation produced through Chat Completions. The SDK base includes /v1.

Preserve metadata when you need it

StrOutputParser deliberately returns only the text. To inspect truncation, usage, or tool calls, keep the AIMessage instead:

message = (prompt | model).invoke({"topic": "optimistic locking"})
print(message.content)
print(message.response_metadata.get("finish_reason"))
print(message.usage_metadata)

Treat finish_reason == "length" as incomplete. Usage may be absent when the API has not reported actual usage. For text streaming, iterate over chain.stream(...) inside the same error-handling boundary. A partially printed stream is not a completed result.

Add tools or structured output

Use model.bind_tools(...) with a tool-capable catalog model. Binding declares tools; your application or agent runtime executes them and sends the matching results. Validate arguments and preserve the call IDs; see tool calling.

The example model supports structured output. When using with_structured_output, choose a method consistent with the selected model and RedRouter's structured-output contract. Do not assume framework schemas make every model enforce JSON.

Keep use_responses_api=False for this recipe. Enabling provider-hosted search, files, conversation state, or other OpenAI features changes the request surface; those capabilities are not supplied by a custom base URL. RedRouter does not expose embeddings, so configure retrieval embeddings independently. Check compatibility, reasoning, and production guidance before adding framework defaults or retries.

Reference: LangChain ChatOpenAI documentation.

↑ ↓ to select · Enter to open · Esc to close