LangChain + EXAIV: Switch Model Providers with One Line (Python/JS)
Point LangChain's ChatOpenAI at EXAIV's OpenAI-compatible endpoint: full Python and TypeScript examples, streaming, tool calling, and common errors.
LangChain + EXAIV: Switch Model Providers with One Line
LangChain's ChatOpenAI class natively supports a custom base_url — point it at EXAIV's OpenAI-compatible endpoint and your chains, agents, and RAG pipelines run on EXAIV's model pool with zero structural changes.
Python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="qwen3.8-flash",
api_key="YOUR_EXAIV_API_KEY",
base_url="https://exaiv.com/api/ai/v1",
)
# Chains / agents are written exactly as with official OpenAI
result = llm.invoke("Explain EXAIV in one sentence")
print(result.content)
Install: pip install langchain-openai
JavaScript / TypeScript
const llm = new ChatOpenAI({
model: "qwen3.8-flash",
apiKey: "YOUR_EXAIV_API_KEY",
configuration: { baseURL: "https://exaiv.com/api/ai/v1" },
});
const result = await llm.invoke("Explain EXAIV in one sentence");
console.log(result.content);
Install: npm install @langchain/openai
Streaming
for chunk in llm.stream("Tell a two-sentence joke"):
print(chunk.content, end="", flush=True)
LangChain's streaming abstraction works unchanged with a custom base_url — events pass through per the OpenAI-compatible protocol.
Agents and tool calling
High-level wrappers like create_agent / create_react_agent take this same llm instance. Tool calls follow the standard OpenAI function-calling protocol, forwarded faithfully by the EXAIV endpoint.
Switching models
Switching models = changing the model string. Available models are the endpoint's list (see the models page) — e.g. deepseek-v4-flash, glm-5.3, claude-opus-4-8-fd — all usable with the same key.
Common issues
| Symptom | Fix |
|---|---|
| NotFoundError | base_url is missing /v1; the EXAIV standard is https://exaiv.com/api/ai/v1 |
| AuthenticationError | Key pasted completely? Not the official OpenAI key by mistake? |
| Timeouts | Complex agent turns can take longer — set a longer client timeout |
Advanced: RAG and agents
RAG: the retriever → llm pipeline needs no extra configuration — base_url applies to the whole chain. Swap the retriever for your own vector store (FAISS/Milvus/etc.); EXAIV handles the generation stage.
Structured output: .with_structured_output(PydanticModel) works over the compatible endpoint per OpenAI semantics — ideal for extracting fields from unstructured text.
Multi-model collaboration: instantiate two ChatOpenAI objects in one agent — a fast model for planning, a flagship for execution — each billed independently. This is the common pattern for keeping agent costs under control.
Observability: LangSmith and other tracing tools use standard callbacks and don't conflict with a custom base_url, making it easy to find which step burned more tokens than expected.
Where EXAIV fits a LangChain stack
A typical LLM application has four layers: orchestration (LangChain/LangGraph), retrieval (your vector store), generation (the model), and observability. EXAIV replaces only the generation layer's provider wiring — every other layer keeps its code, its data, and its tooling.
That separation is what makes the switch low-risk: keep your chains and prompts exactly as they are, change the model string per environment, and A/B models by pointing one deployment at a different model ID. Teams commonly run production on a stable model while a staging environment experiments with newer ones — same key, same billing, independent rate limits per call volume.
If you later add an agent framework (LangGraph, CrewAI) or an evaluation harness, they all consume the same ChatOpenAI instance, so the provider change propagates everywhere at once.
FAQ
Migrating from another provider
If your codebase already points ChatOpenAI at another gateway or the official API, the migration is a three-line diff: swap the base_url, swap the key, and confirm the model IDs exist on the new endpoint (rename in one place if your config centralizes them). Run your existing test prompts afterward — LangChain's abstractions mean no chain or agent code changes.
One habit worth adopting during migration: log the model ID alongside every response (LangChain makes this available on the result metadata). When you later compare providers or models, you will want this field in your logs rather than in your memory.