> For the complete documentation index, see [llms.txt](https://docs.tensorx.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorx.ai/developer-sdks/langchain.md).

# LangChain

Use TensorX models with [LangChain](https://python.langchain.com/), the popular framework for building LLM-powered applications.

## Installation

```bash
pip install -U langchain-openai
```

## Quick Start

```python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="z-ai/glm-5.1",
    base_url="https://api.tensorx.ai/v1",
    api_key="your-tensorx-api-key",
)

response = llm.invoke("Hello, how are you?")
print(response.content)
```

## Configuration

LangChain's `ChatOpenAI` class supports OpenAI-compatible APIs through the `base_url` parameter.

| Parameter  | Value                       |
| ---------- | --------------------------- |
| `base_url` | `https://api.tensorx.ai/v1` |
| `api_key`  | Your TensorX API key        |
| `model`    | Any TensorX model ID        |

## Examples

### Basic Chat

```python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="z-ai/glm-5.1",
    base_url="https://api.tensorx.ai/v1",
    api_key="your-tensorx-api-key",
    temperature=0.7,
)

messages = [
    ("system", "You are a helpful assistant."),
    ("human", "What is the capital of France?"),
]

response = llm.invoke(messages)
print(response.content)
```

### Streaming

```python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="minimax/minimax-m2",
    base_url="https://api.tensorx.ai/v1",
    api_key="your-tensorx-api-key",
)

for chunk in llm.stream("Tell me a short story"):
    print(chunk.content, end="", flush=True)
```

### Tool Calling

```python
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

class GetWeather(BaseModel):
    """Get current weather for a location"""
    location: str = Field(description="City and state, e.g. San Francisco, CA")

llm = ChatOpenAI(
    model="z-ai/glm-5.1",
    base_url="https://api.tensorx.ai/v1",
    api_key="your-tensorx-api-key",
)

llm_with_tools = llm.bind_tools([GetWeather])
response = llm_with_tools.invoke("What's the weather in Tokyo?")
print(response.tool_calls)
```

### Structured Output

```python
from langchain_openai import ChatOpenAI
from pydantic import BaseModel

class Joke(BaseModel):
    setup: str
    punchline: str

llm = ChatOpenAI(
    model="minimax/minimax-m2",
    base_url="https://api.tensorx.ai/v1",
    api_key="your-tensorx-api-key",
)

structured_llm = llm.with_structured_output(Joke)
result = structured_llm.invoke("Tell me a programming joke")
print(f"Setup: {result.setup}")
print(f"Punchline: {result.punchline}")
```

### Async Usage

```python
import asyncio
from langchain_openai import ChatOpenAI

async def main():
    llm = ChatOpenAI(
        model="z-ai/glm-5.1",
        base_url="https://api.tensorx.ai/v1",
        api_key="your-tensorx-api-key",
    )
    
    response = await llm.ainvoke("Hello!")
    print(response.content)

asyncio.run(main())
```

## Available Models

| Model        | ID                     | Best For                        |
| ------------ | ---------------------- | ------------------------------- |
| GLM-5.1 ⭐    | `z-ai/glm-5.1`         | Coding, reasoning, functions    |
| MiniMax-M2.5 | `minimax/minimax-m2.5` | Reasoning, functions            |
| Kimi-K2.5    | `moonshotai/kimi-k2.5` | Vision, functions, long context |
| MiniMax-M2   | `minimax/minimax-m2`   | Coding, fast responses          |

## Tips

* Use `z-ai/glm-5.1` for applications that require tool/function calling
* Use `minimax/minimax-m2` for reasoning tasks and long conversations
* Set `temperature=0` for deterministic outputs

## Resources

* [LangChain ChatOpenAI Docs](https://python.langchain.com/docs/integrations/chat/openai/)
* [TensorX API Reference](/api-reference/overview.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tensorx.ai/developer-sdks/langchain.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
