Introduction
LangChain is an open-source framework for building applications and agents on top of large language models. Thelangchain-apimart package provides ChatAPIMart, a LangChain chat model that connects to APIMart’s OpenAI-compatible API, so you can use GPT, Claude, DeepSeek, Qwen, GLM, Grok and other models through one API key and switch between them by changing the model name.
ChatAPIMart supports the standard LangChain chat model interface: invoke, stream, batch, async methods, tool calling and structured output.
Prerequisites
Before you begin, make sure you have:- Python 3.10 or newer
-
An APIMart API key
Log in to the APIMart Console to get your API key (starts with
sk-)
Tip: If you don’t have an APIMart account yet, please register at APIMart first and obtain your API key.
Step 1: Install the package
langchain-apimart together with langchain-core and langchain-openai.
If your terminal reports
command not found: pip (common on macOS), use pip3 install -U langchain-apimart or python3 -m pip install -U langchain-apimart instead.Step 2: Set your API key
ChatAPIMart reads the key from the APIMART_API_KEY environment variable.
- macOS / Linux
- Windows (PowerShell)
ChatAPIMart(api_key="sk-..."), but keep keys out of source code you share.
Step 3: Send your first request
model value, for example gpt-5.5, claude-sonnet-5-5, qwen3.7-plus, glm-5.3 or grok-4.7. See the model list for all available model IDs.
Streaming
usage_metadata on the combined message matches the token counts reported by the API.
Tool calling
Describe a tool with a Pydantic model (or a function decorated with@tool) and bind it to the model:
[{'name': 'GetWeather', 'args': {'city': 'Paris'}, ...}]. Your code runs the tool and sends the result back as a ToolMessage.
Structured output
Ask the model to return data that matches a schema:with_structured_output uses tool calling by default, which works with all APIMart chat models that support tools. Some models also support OpenAI’s json_schema response format; pass method="json_schema" only when you know the model supports it.Build an agent
ChatAPIMart works with LangChain agents. This example gives the model one tool and lets the agent decide when to call it:
pip install -U langchain.
Configuration reference
All other
ChatOpenAI arguments, such as temperature, max_tokens, timeout and max_retries, work as usual.
Using ChatOpenAI directly
If you already uselangchain-openai, you can point ChatOpenAI at APIMart instead of installing a new package:
ChatAPIMart adds a few APIMart-specific defaults on top of this: it requests token usage when streaming and counts it once, uses tool calling for structured output, and always uses the Chat Completions API.
FAQ
Which base URL should I use?
Which base URL should I use?
Use
https://api.apimart.ai/api/v1, which is the default in ChatAPIMart. It follows the standard OpenAI behavior and returns a single JSON response unless you ask for streaming.I get an error about a missing API key
I get an error about a missing API key
Make sure
APIMART_API_KEY is set in the same terminal session that runs your code, or pass api_key when creating ChatAPIMart.Structured output returns plain text
Structured output returns plain text
Remove
method="json_schema" so that the default tool-calling method is used. Not every model supports the json_schema response format.