LangChain xAI Integration
An integration package connecting xAI and LangChain (current version 1.2.2). It provides an interface to interact with xAI's Grok models, including features like chat completions, streaming, and Live Search. The library is part of the broader LangChain ecosystem, which generally follows a rapid release cycle with continuous updates and improvements to its partner integrations.
Warnings
- gotcha It's crucial to distinguish between `xAI` (which develops Grok models) and `Groq` (a separate AI hardware and software company). Ensure you are using the correct `langchain-xai` package and corresponding API endpoint for `xAI` services, not Groq.
- gotcha The `XAI_API_KEY` environment variable must be set for authentication with xAI's API. Alternatively, the API key can be passed directly via the `xai_api_key` parameter during `ChatXAI` instantiation. Failure to provide a valid key will result in authentication errors.
- gotcha As a LangChain partner package, `langchain-xai` relies on `langchain-core`. Ensure `langchain-core` is installed and up-to-date, especially after major LangChain framework updates, as API changes in `langchain-core` could impact `langchain-xai`.
- gotcha Be aware that while this registry entry is for the Python `langchain-xai` package, there is a separate JavaScript package `@langchain/xai`. Ensure you are using the correct package and import statements for your Python projects to avoid installation and import errors.
Install
-
pip install langchain-xai
Imports
- ChatXAI
from langchain_xai import ChatXAI
Quickstart
import os
from langchain_xai import ChatXAI
from langchain_core.messages import HumanMessage, SystemMessage
# Set your xAI API key as an environment variable (e.g., in your shell: export XAI_API_KEY="YOUR_API_KEY")
# Or pass it directly as xai_api_key="YOUR_API_KEY" to ChatXAI
api_key = os.environ.get("XAI_API_KEY", "YOUR_XAI_API_KEY_HERE") # Replace with actual key or ensure env var is set
if api_key == "YOUR_XAI_API_KEY_HERE":
print("Warning: XAI_API_KEY not found in environment variables. Please set it or pass it directly.")
print("Skipping example due to missing API key.")
else:
model = ChatXAI(
model="grok-4",
temperature=0,
xai_api_key=api_key # Pass the API key
)
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="What is the capital of France?"),
]
print("Invoking Grok model...")
response = model.invoke(messages)
print(response.content)
print("\nStreaming response:")
for chunk in model.stream(messages):
print(chunk.content, end="")
print()