APIrove

Home / Blog / LangChain custom Base URL

LANGCHAIN · API INTEGRATION · 6 MIN READ

Use LangChain ChatOpenAI with a Custom Base URL

Point LangChain's ChatOpenAI wrapper at an OpenAI-compatible chat endpoint by setting base_url, an API key, and a model ID your account can use. Compatibility still depends on the endpoint and model.

By APIrove · October 15, 2026

Before you start

You need a LangChain OpenAI integration package, an API key for the destination service, and the exact model ID enabled for your account. A custom base URL changes where the SDK sends requests; it does not grant model access or make every OpenAI feature available.

For APIrove, the documented base URL is https://apirove.com/v1. Check the model list and confirm account access and pricing after signing in. Use the API documentation to review the currently documented endpoints.

Configure LangChain ChatOpenAI in Python

Install the integration:

python -m pip install -U langchain-openai

Store your key and model ID in your server environment. Do not put a real key in a notebook you share, browser code, or a public repository.

export APIROVE_API_KEY="your-api-key"
export APIROVE_MODEL_ID="your-enabled-model-id"

Then configure ChatOpenAI to use the compatible Chat Completions endpoint:

import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model=os.environ["APIROVE_MODEL_ID"],
    api_key=os.environ["APIROVE_API_KEY"],
    base_url="https://apirove.com/v1",
    use_responses_api=False,
)

reply = model.invoke("Reply with: connection test passed.")
print(reply.content)

The example deliberately uses a placeholder model ID. Replace it with a model shown as available to your account, and confirm that the chosen model supports Chat Completions. use_responses_api=False keeps this LangChain call on the /v1/chat/completions request format; it does not establish that every model accepts the same parameters.

Try streaming only if your app needs it

After a normal request succeeds, test streaming separately:

for chunk in model.stream("Count from one to three."):
    if chunk.content:
        print(chunk.content, end="", flush=True)

Streaming behavior and returned metadata can differ across models and compatible services. Confirm the result shape your application depends on instead of assuming that every provider-specific field is preserved.

Verify the exact integration

  1. Confirm the Base URL includes /v1 and the API key belongs to the destination service.
  2. Check that the model ID is enabled for your account and supports the selected endpoint.
  3. Start with a short prompt and a small output limit; inspect the returned content and model.
  4. If the application needs streaming, tools, JSON output, or multimodal input, test each capability independently with the exact model.
  5. Review account usage and pricing after the request before increasing traffic.

LangChain documents base_url as a client setting and notes that ChatOpenAI targets the standard OpenAI API specification. Non-standard response fields from third-party services may not be extracted or preserved. Check the LangChain ChatOpenAI reference for current package behavior.

Common setup checks

  • 401: confirm the key is present, copied correctly, and issued by the service named in base_url.
  • 403: check account permissions and whether the selected model is enabled.
  • 400 or a not-found response: verify the endpoint path, model ID, and supported request format.
  • 429: review account limits and the response guidance before retrying.
  • 5xx or timeout: capture the request ID if provided; avoid rapid retries that can multiply requests.
This guide explains the configuration pattern. It does not claim that every LangChain version, model, parameter, or APIrove account has been live-tested. Confirm current model access and behavior in your own account. For the general concept, see What an OpenAI-compatible API means.