---
id: 18799
lang: en
title: "LangChain 1.5 connects any OpenAI-compatible LLM through a single base_url"
published_at: "2026-12-01 08:30:31"
source_name: "Habr AI"
source_url: "https://habr.com/ru/companies/bothub/articles/1114408/?utm_campaign=1114408&utm_source=habrahabr&utm_medium=rss"
canonical_url: "https://news.hamidun.com/en/news/18799"
categories: ["llm", "autonomous"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

# LangChain 1.5 connects any OpenAI-compatible LLM through a single base_url

> **Краткая суть (BLUF):** Almost all AI providers copy the OpenAI request format, so in LangChain 1.5 you switch models by replacing one server address: the base_url parameter. For example, when moving from OpenAI to a local vLLM server, the code stays the same and only the address and key change. But "OpenAI compatibility" has no single specification, and gateways behave differently. Before going to production, check streaming, tool calling, and structured output on your own gateway.  • stream_usage is disabled for non-OpenAI endpoints: enable token accounting manually • with_structured_output silently drops the agent's bound tools • Non-standard gateway fields (LM Studio's ttl) are passed through extra_body

Imagine you have built an AI agent on OpenAI, and tomorrow you need to move it to a local model in vLLM or to a cheap cloud gateway. You don't want to rewrite all the code. In LangChain 1.5 (the current version of the popular AI agent framework), this is solved by changing one parameter: base_url, the address of the server where requests are sent. This works because nearly the entire industry copies the OpenAI Chat Completions format, and such endpoints are called "OpenAI-compatible." But this compatibility has pitfalls, and you should know about them before going to production.

## How to connect a third-party gateway

In autumn 2025, LangChain 1.0 shipped: the team rewrote the core, moved legacy code into the langchain-classic package, and promised a stable API. The current branch is 1.5, with middleware and the create_agent function. The framework's idea remains the same: the model should be swapped with almost no code changes. In practice, each provider has its own package (langchain-anthropic, langchain-deepseek, langchain-google-genai), its own key, and its own environment variables, and some providers have no official integration at all.

The workaround: ChatOpenAI is a wrapper over the official openai client. It makes a single POST request to {base_url}/chat/completions, which contains the model name, messages, temperature, and the list of tools. If you substitute the address of a third-party gateway, the request goes there. Installation: `pip install -U langchain langchain-openai`, Python 3.10+ required. It's best to keep the address and key in environment variables. The lookup order for the address is: first the base_url parameter, then OPENAI_API_BASE, and last OPENAI_BASE_URL.

If the model should come from a config or be chosen per task (a cheap one for classification, an expensive one for complex queries), the init_chat_model factory is more convenient: a string like "openai:model-name" with the same base_url will create the same ChatOpenAI. If there is one provider and it is fixed, it's simpler to write ChatOpenAI directly: fewer layers, easier debugging. For well-known providers such as DeepSeek or OpenRouter, LangChain recommends dedicated packages, for example ChatDeepSeek: they extract non-standard fields, in particular reasoning content, which ChatOpenAI ignores according to the documentation.

The author uses the BotHub aggregator, with 250+ models and a referral bonus of 300,000 CAPS, as the gateway. This is a sponsored mention, but the techniques apply to any compatible endpoint: vLLM, LM Studio, Ollama, or a cloud gateway.

## Where "compatibility" ends

The term "OpenAI-compatible" has no specification, versions, or test suite. Any service that accepts POST /v1/chat/completions and returns JSON with a choices field may call itself that. So you should check three levels: request paths and structure (there are almost no problems here), response format, and behavior. Responses sometimes hold surprises, such as a missing finish_reason in the middle of a stream. Behavior diverges even more: parameters are silently dropped, temperature ceilings differ, and error handling and retry rules are all custom.

Specific nuances:
- For non-OpenAI endpoints, stream_usage is disabled by default: many gateways return token counts incorrectly during streaming or don't return them at all. If you need usage statistics, enable stream_options={"include_usage": True}; otherwise, cost is calculated blind.
- LangChain passes unknown parameters (for example, frequency_penalty) into the request as they are. For fields that are not in the OpenAI spec, there is extra_body: for vLLM this is use_beam_search, for LM Studio it is ttl.
- Streaming over SSE works on almost all endpoints. Images and audio usually pass through, but you need to verify this with your own provider.
- with_structured_output (a response schema based on Pydantic) conflicts with tool calling: a model with a schema silently loses its bound tools, and vice versa. This is a long-standing problem with an open issue in the LangChain repository. Inside an agent with tools, use this kind of output selectively and test it; in a plain ChatOpenAI without tools, it works normally.

## The create_agent agent and the price of abstraction

In 1.x, an agent is assembled with a single create_agent function on top of LangGraph. It doesn't care which model is underneath, as long as tool calling is supported. In the author's example, the agent gets two tools (body mass index and daily calorie requirement using the Mifflin–St Jeor formula) and the prompt "calculate using the tools, don't guess." After that comes the standard ReAct loop: the model calls one tool, receives a number, calls the second, and assembles the answer.

Middleware sits on top of the model: SummarizationMiddleware automatically compresses long conversation history (you can use a cheap model from the same gateway), and HumanInTheLoopMiddleware asks for confirmation before dangerous calls. Loop states are streamed via agent.stream(..., stream_mode="values"). Beyond agents, the framework includes a RAG pipeline (document loaders, chunking, init_embeddings, Chroma, Qdrant, pgvector), LangSmith tracing with tokens, latency, and cost for each step, and a JS/TS version.

The cost of all this is the depth of abstraction. When something breaks inside an agent, the stack trace leads through LangChain's code, not yours: this is a long-standing complaint from engineers at Octomind. For a small project, it is more sensible to use the provider's plain SDK.

The practical takeaway: connecting a third-party gateway is easy, but before production, run your own set of checks against it: streaming with usage, tool calling, structured output, and errors. The author also notes a broader shift: competition is moving away from the APIs themselves toward price, speed, regional availability, and privacy, so the gateway and model should be chosen for the specific task.

---
- **Первоисточник:** [Habr AI](https://habr.com/ru/companies/bothub/articles/1114408/?utm_campaign=1114408&utm_source=habrahabr&utm_medium=rss)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/18799
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
