Suanlimao
← Back to all tutorials

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

terminal
$ pip install -U langchain-openai$ export API_KEY=<YOUR_API_KEY>
The full key is shown only once, at creation. Missed it? Revoke it and create a new one.

Create a key →

Step 2 · STEP 0x02

Set the address, key and model

python
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)
terminal
$ python demo.pyHello! I'm doing well, thank you …
Use 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.
Don't guess the model name: open the model plaza, click “Copy model id” on a model card and paste it. A wrong model name is the most common error.

Step 3 · STEP 0x03

Use environment variables instead (optional)

terminal
$ export OPENAI_BASE_URL=https://api.suanlimao.shop/v1$ export OPENAI_API_KEY=<YOUR_API_KEY>
python
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.

LangChain states that 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.invoke returns 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).