Skip to main content

Introduction

LangChain is an open-source framework for building applications and agents on top of large language models. The langchain-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:
  1. Python 3.10 or newer
  2. 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

This installs 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.
You can also pass the key directly with ChatAPIMart(api_key="sk-..."), but keep keys out of source code you share.

Step 3: Send your first request

To use another model, change the 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

Token usage is returned with streamed responses and is counted once per response, so 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:
The model returns a tool call such as [{'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:
Install the agent package first with 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 use langchain-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

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.
Make sure APIMART_API_KEY is set in the same terminal session that runs your code, or pass api_key when creating ChatAPIMart.
Remove method="json_schema" so that the default tool-calling method is used. Not every model supports the json_schema response format.
The package is open source: GitHub · PyPI.