Metadata-Version: 2.5
Name: langchain-keln
Version: 0.1.0
Summary: LangChain integration for Keln, an inference platform for open models
Project-URL: Homepage, https://keln.ai
Project-URL: Documentation, https://docs.keln.ai
Project-URL: Repository, https://github.com/kris-holy/langchain-keln
Project-URL: Issues, https://github.com/kris-holy/langchain-keln/issues
Author-email: kris-holy <kris@keln.ai>
License-Expression: MIT
License-File: LICENSE
Keywords: keln,langchain,llm,open models,openai-compatible
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: <4.0,>=3.10
Requires-Dist: langchain-core<2.0.0,>=1.0.0
Requires-Dist: langchain-openai<2.0.0,>=1.0.0
Description-Content-Type: text/markdown

# langchain-keln

LangChain chat model for [Keln](https://keln.ai), an inference platform for open models.

`ChatKeln` works like `ChatOpenAI`: `invoke`, `stream`, async calls, tool calling, structured output and agents. It also handles 2 Keln details for you. It reads the reasoning trace that reasoning models return, and it sends that trace back on assistant messages in a tool call loop.

## Install

```bash
pip install -U langchain-keln
```

## Get an API key

1. Sign up at [app.keln.ai/signup](https://app.keln.ai/signup).
2. Go to **Keys** → **Create key**.
3. Set the key as an environment variable:

```bash
export KELN_API_KEY="your-api-key"
```

On Windows PowerShell:

```powershell
$env:KELN_API_KEY = "your-api-key"
```

## Chat

```python
from langchain_keln import ChatKeln

llm = ChatKeln(model="zai-org/glm-5.3", reasoning_effort="low")

print(llm.invoke("Explain prompt caching in 1 sentence.").content)

for chunk in llm.stream("Write a haiku about open models."):
    print(chunk.text, end="", flush=True)
```

The model list with context lengths and reasoning levels is at [docs.keln.ai/models-pricing](https://docs.keln.ai/models-pricing/). The same list is available as JSON at `GET https://api.keln.ai/v1/models`, with no key needed.

## Tool calling and structured output

```python
from langchain_core.tools import tool
from pydantic import BaseModel


@tool
def get_weather(city: str) -> str:
    """Return the current weather for a city."""
    return "sunny, 21 C"


llm_with_tools = llm.bind_tools([get_weather])
print(llm_with_tools.invoke("What is the weather in Paris?").tool_calls)


class Summary(BaseModel):
    title: str
    bullets: list[str]


print(
    llm.with_structured_output(Summary).invoke(
        "Summarize what an inference platform does."
    )
)
```

## Agents

With the `langchain` package installed (`pip install -U langchain`):

```python
from langchain.agents import create_agent

agent = create_agent(ChatKeln(model="moonshotai/kimi-k3"), tools=[get_weather])
agent.invoke(
    {"messages": [{"role": "user", "content": "What is the weather in Paris?"}]}
)
```

## Reasoning

Most Keln models can think before they answer. Set the level with `reasoning_effort`, for example `"none"`, `"low"`, `"high"` or `"max"`. Each model lists the levels it accepts in `/v1/models`. If you leave it out, each model uses the default level listed in `/v1/models`.

The trace is in `message.additional_kwargs["reasoning_content"]`, in `invoke` and in `stream`. When you pass the conversation back, `ChatKeln` sends each assistant message's trace with it, so the model keeps its train of thought across tool calls.

## Configuration

| Argument | Env var | Default |
|---|---|---|
| `api_key` | `KELN_API_KEY` | required |
| `base_url` | `KELN_API_BASE` | `https://api.keln.ai/v1` |
| `model` | | `zai-org/glm-5.3` |

Every other `ChatOpenAI` argument (`temperature`, `max_tokens`, `timeout`, `max_retries`, `model_kwargs` and others) works the same way. `max_tokens` covers reasoning and answer together.

`ChatKeln` uses the `/v1/chat/completions` endpoint. It sends the `system` role and `max_tokens`, and leaves out the `store` field.

## Docs

- Keln docs: [docs.keln.ai](https://docs.keln.ai)
- Models and prices: [docs.keln.ai/models-pricing](https://docs.keln.ai/models-pricing/)

## Development

```bash
pip install -e . --group test --group lint --group typing
pytest tests/unit_tests --disable-socket --allow-unix-socket
KELN_API_KEY=... pytest tests/integration_tests
ruff check . && ruff format --check . && mypy langchain_keln tests
```

## License

MIT, Keln.
