Loading...

Docs · Integrations

LangChain with TopxAI: ChatTopxAI from the langchain-topxai package

Call TopxAI chat models from LangChain in Python with ChatTopxAI from langchain-topxai. ChatOpenAI with a base URL still works, also for the Responses API.

langchain-topxai is the LangChain package for TopxAI. Its ChatTopxAI class calls TopxAI's text models through Chat Completions and reads your key from TOPXAI_API_KEY. TopXEA, the operator of TopxAI, maintains it; the source is on GitHub and releases are on PyPI.

Install

pip install -U langchain-topxai
export TOPXAI_API_KEY="sk-..."

Create the key on the API keys page; Create an API key and choose a route explains the route each key is tied to.

Python

from langchain_topxai import ChatTopxAI

llm = ChatTopxAI(model="claude-sonnet-5-5")
print(llm.invoke("Say hello in one sentence.").content)

model takes the id of any text model on the price list except Jev, for example claude-sonnet-5-5, gpt-6.1-sol, grok-4.7, kimi-k3.1-pro, deepseek-flash-Abliterated or GLM-5.3-Abliterated. ChatTopxAI builds on BaseChatOpenAI from langchain-openai, so you write streaming, async calls, tool calling, structured output and token usage the same way as with ChatOpenAI.

Argument Environment variable Default
api_key TOPXAI_API_KEY none; required. OPENAI_API_KEY is never read.
base_url TOPXAI_API_BASE https://ai.topxea.com/v1

Because OPENAI_API_KEY is never read, an OpenAI key in the same environment is not sent to TopxAI by mistake.

Reasoning text

ChatTopxAI puts the reasoning of models that return it, for example deepseek-flash-Abliterated and GLM-5.3-Abliterated, in additional_kwargs["reasoning_content"], also when streaming. The adapter reads both reasoning and reasoning_content; see the model guides for GLM and deepseek-flash-Abliterated.

msg = ChatTopxAI(model="deepseek-flash-Abliterated").invoke("What is 17 * 3?")
print(msg.additional_kwargs.get("reasoning_content"))

ChatOpenAI does not extract this field, so with it the reasoning text is lost.

Structured output

with_structured_output sends a JSON Schema by default (method="json_schema"), as ChatOpenAI does. Keep the default for the Claude models (claude-sonnet-5-5, claude-opus-5-5, claude-fable-5-1): they do not accept a forced tool call, so method="function_calling" fails for them, and TopxAI answers it with a 400.

For Claude, TopxAI turns the json_schema response format into Anthropic's structured outputs, which accept a subset of JSON Schema. Number bounds such as ge and le and list lengths reach Claude as a hint in the field description instead of being enforced; LangChain still checks the answer against your Pydantic model. dict and Any fields, recursive models and tuples get a 400 that says which part of the schema Claude cannot take. With method="json_mode" there is no schema, so TopxAI asks Claude for a single JSON object in the system prompt.

ChatOpenAI and ChatAnthropic

ChatOpenAI from langchain-openai still works with a base URL. It is also the class for TopxAI's Responses API: ChatTopxAI calls Chat Completions only and refuses use_responses_api=True. The API reference lists the models /v1/responses serves.

import os

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-6.1-sol",
    base_url="https://ai.topxea.com/v1",
    api_key=os.environ["TOPXAI_API_KEY"],
)

responses_llm = ChatOpenAI(
    model="gpt-6.1-sol",
    base_url="https://ai.topxea.com/v1",
    api_key=os.environ["TOPXAI_API_KEY"],
    use_responses_api=True,
)

ChatAnthropic from langchain-anthropic reaches Claude through the Messages API, with prompt caching. For structured output, pass method="json_schema" to its with_structured_output. Its default, function_calling, cannot force the tool call on the three Claude models; langchain-anthropic 1.7.6 warns and raises OutputParserException whenever Claude answers without calling the tool.

import os

from langchain_anthropic import ChatAnthropic

claude = ChatAnthropic(
    model="claude-sonnet-5-5",
    base_url="https://ai.topxea.com",
    api_key=os.environ["TOPXAI_API_KEY"],
    max_tokens=1024,
)

Without base_url and api_key arguments, these two classes read OPENAI_BASE_URL / OPENAI_API_KEY and ANTHROPIC_BASE_URL / ANTHROPIC_API_KEY (OPENAI_API_BASE and ANTHROPIC_API_URL take precedence when set), so a deployment can switch endpoints without a code change.

JavaScript

langchain-topxai is a Python package. In LangChain.js, pass the base URL to ChatOpenAI or ChatAnthropic:

import { ChatOpenAI } from '@langchain/openai'
import { ChatAnthropic } from '@langchain/anthropic'

const llm = new ChatOpenAI({
  model: 'gpt-6.1-sol',
  apiKey: process.env.TOPXAI_API_KEY,
  configuration: { baseURL: 'https://ai.topxea.com/v1' },
})

const claude = new ChatAnthropic({
  model: 'claude-sonnet-5-5',
  apiKey: process.env.TOPXAI_API_KEY,
  anthropicApiUrl: 'https://ai.topxea.com',
})

Embeddings

The catalogue has no embedding model; keep OpenAIEmbeddings on the provider you use today.

If it fails

Models: claude-sonnet-5-5, gpt-6.1-sol, deepseek-flash-Abliterated · Models & pricing

Related

Previous: ChatBox with TopxAI: a custom provider · Next: Vercel AI SDK with TopxAI: createOpenAI, createAnthropic and openai-compatible

Other languages: 简体中文