Connect your tools
LangChain
Point langchain-openai's ChatOpenAI at our address.
About 5 minFor:Developers building with LangChain
Before you start
- A Python environment
- A key in an environment variable
Step 1 · STEP 0x01
Install
$ pip install -U langchain-openai$ export API_KEY=<YOUR_API_KEY>
Step 2 · STEP 0x02
Set the address, key and model
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="<MODEL_NAME>",
base_url="https://api.suanlimao.shop/v1",
api_key=os.environ["API_KEY"],
)
response = llm.invoke("Hello, how are you?")
print(response.content)$ python demo.pyHello! I'm doing well, thank you …
https://api.suanlimao.shop/v1 as the API address (with /v1). If the tool says 404, check whether /v1 is missing or doubled, then read the FAQ below.Step 3 · STEP 0x03
Use environment variables instead (optional)
$ export OPENAI_BASE_URL=https://api.suanlimao.shop/v1$ export OPENAI_API_KEY=<YOUR_API_KEY>
llm = ChatOpenAI(model="<MODEL_NAME>")LangChain reads OPENAI_API_BASE and the OpenAI SDK reads OPENAI_BASE_URL; if both are set the first wins. With OPENAI_BASE_URL set, LangChain leaves stream_usage off by default because many non-OpenAI endpoints do not support streaming usage.
ChatOpenAI targets the official OpenAI spec only: non-standard fields from third-party providers are not extracted. Use the OpenAI SDK directly if you need them.✓ How to tell it worked
llm.invokereturns an answer and the Usage page shows the request.
FAQ
What about LangChain's Anthropic integration?
Claude models also work through the OpenAI-compatible API with the code above. For Anthropic-native features use the Anthropic SDK with the root address; see the SDK tutorial.
404?
Check that base_url has /v1 and the model name.
Related tutorials
Still stuck?
Read the troubleshooting tutorials first. If that does not help, open a ticket with the request_id, time, model and the exact error (never paste your key).