# Connect LangChain

https://modelportal.ai/docs/integrations/langchain

> Python framework for LLM apps; point ChatOpenAI at the platform.

## Use cases

Good for Python projects that build chains, agents or retrieval flows with LangChain or LangGraph. ChatOpenAI speaks the OpenAI-compatible API, so base_url and api_key are all that change.

## Tips

Read the key from a server-side environment variable. Use llm.stream() for streaming and llm.bind_tools() for tool calling.

## If it cannot connect

1. Use `https://modelportal.ai/v1` as the API host, without `/chat/completions`.
2. The key starts with `mp-` (older keys: `key_`) and is not paused or revoked; `invalid_api_key` means the key cannot be used.
3. The model ID matches the [catalog](/catalog) and is in the key's allowed models.
4. `insufficient_balance` means the balance is too low; see [common errors](/docs/errors?lang=en) for more.

## Setup

1. Install the dependency: pip install langchain-openai.
2. Create ChatOpenAI with base_url set to the API host below, your own key as api_key, and the model ID as model.
3. Use the model object in chains, agents, or LangGraph as usual.

## Settings

- API host：`https://modelportal.ai/v1`
- API key：`YOUR_API_KEY`
- Model：`MODEL_ID`

```python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="MODEL_ID",
    base_url="https://modelportal.ai/v1",
    api_key="YOUR_API_KEY",
)
print(llm.invoke("你好").content)
```

## FAQ

### Can I use environment variables instead of arguments?

Yes. With OPENAI_API_KEY and OPENAI_BASE_URL set, ChatOpenAI picks them up when api_key and base_url are omitted.

