Skip to main content
Valar exposes an OpenAI-compatible API, so you can use LangChain’s OpenAI integration as-is and just repoint the base URL and key at Valar.

Set up

1

Install the packages

pip install langchain langchain-openai
2

Set your API key

Export your Valar key so the SDK can read it from the environment.
export VALAR_API_KEY="your-api-key"
3

Configure ChatOpenAI

Point ChatOpenAI at the Valar base URL and pass your key. Set model to a Valar model ID.
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    base_url="https://api.valarhq.ai/v1",
    api_key=os.environ["VALAR_API_KEY"],
    model="moonshotai/Kimi-K2.6",
)
4

Invoke the model

Send a message and print the response.
response = llm.invoke("Explain retrieval-augmented generation in two sentences.")
print(response.content)

Stream responses

Use .stream(...) to receive the response in chunks as they are generated.
for chunk in llm.stream("Write a haiku about distributed systems."):
    print(chunk.content, end="", flush=True)

Tool calling

Attach tools with .bind_tools([...]). LangChain emits the OpenAI tool-calling format that Valar supports, and any tool calls the model makes come back on the response’s tool_calls.
from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    """Return the current weather for a city."""
    return f"It is 18C and clear in {city}."

llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in Amsterdam?")
print(response.tool_calls)
To choose a completion window, pass it as request metadata through model_kwargs. The two windows are Now (asap) and Standard (standard):
llm = ChatOpenAI(
    base_url="https://api.valarhq.ai/v1",
    api_key=os.environ["VALAR_API_KEY"],
    model="zai-org/GLM-5.1-FP8",
    model_kwargs={"metadata": {"completion_window": "standard"}},
)
See Completion windows and Inference modes for details.
See Models for the full list of model IDs, and Structured outputs for returning typed, schema-validated responses.