本页还没有译文
本页 34 段还没译成日本語,下面显示的是英文原文。ChatXAI integration
还没翻译译自英文(docs.langchain.com)本页 34 段都还没译
Warning
This page makes reference to Grok models provided by xAI - not to be confused with Groq, a separate AI hardware and software company. See the Groq provider page.
xAI offers an API to interact with Grok models.
Tip
API Reference
For detailed documentation of all features and configuration options, head to the ChatXAI API reference.
Overview
Integration details
| Class | Package | Serializable | JS support | Downloads | Version |
|---|---|---|---|---|---|
ChatXAI | langchain-xai | beta | ✅ |
Model features
| Tool calling | Structured output | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ |
Setup
To access xAI models, you'll need to create an xAI account, get an API key, and install the langchain-xai integration package.
Credentials
Head to this page to sign up for xAI and generate an API key. Once you've done this, set the XAI_API_KEY environment variable:
import getpass
import os
if "XAI_API_KEY" not in os.environ:
os.environ["XAI_API_KEY"] = getpass.getpass("Enter your xAI API key: ")To enable automated tracing of your model calls, set your LangSmith API key:
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"Installation
The LangChain xAI integration lives in the langchain-xai package:
pip install -qU langchain-xaiInstantiation
Now we can instantiate our model object and generate chat completions:
from langchain_xai import ChatXAI
llm = ChatXAI(
model="grok-beta",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
# other params...
)Invocation
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msgAIMessage(content="J'adore programmer.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 30, 'total_tokens': 36, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'grok-beta', 'system_fingerprint': 'fp_14b89b2dfc', 'finish_reason': 'stop', 'logprobs': None}, id='run-adffb7a3-e48a-4f52-b694-340d85abe5c3-0', usage_metadata={'input_tokens': 30, 'output_tokens': 6, 'total_tokens': 36, 'input_token_details': {}, 'output_token_details': {}})print(ai_msg.content)J'adore programmer.Tool calling
ChatXAI has a tool calling (we use "tool calling" and "function calling" interchangeably here) API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool. Tool-calling is extremely useful for building tool-using chains and agents, and for getting structured outputs from models more generally.
ChatXAI.bind_tools()
With ChatXAI.bind_tools, we can easily pass in Pydantic classes, dict schemas, LangChain tools, or even functions as tools to the model. Under the hood, these are converted to an OpenAI tool schema, which looks like:
{
"name": "...",
"description": "...",
"parameters": {...} # JSONSchema
}
and passed in every model invocation.
from pydantic import BaseModel, Field
class GetWeather(BaseModel):
"""Get the current weather in a given location"""
location: str = Field(description="The city and state, e.g. San Francisco, CA")
llm_with_tools = llm.bind_tools([GetWeather])ai_msg = llm_with_tools.invoke(
"what is the weather like in San Francisco",
)
ai_msgAIMessage(content='I am retrieving the current weather for San Francisco.', additional_kwargs={'tool_calls': [{'id': '0', 'function': {'arguments': '{"location":"San Francisco, CA"}', 'name': 'GetWeather'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 151, 'total_tokens': 162, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'grok-beta', 'system_fingerprint': 'fp_14b89b2dfc', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-73707da7-afec-4a52-bee1-a176b0ab8585-0', tool_calls=[{'name': 'GetWeather', 'args': {'location': 'San Francisco, CA'}, 'id': '0', 'type': 'tool_call'}], usage_metadata={'input_tokens': 151, 'output_tokens': 11, 'total_tokens': 162, 'input_token_details': {}, 'output_token_details': {}})Web search
xAI's previous Live Search configuration used the search_parameters constructor
argument, which is now deprecated. To ground Grok responses with current web
results, bind xAI's built-in web_search tool instead:
from langchain_xai import ChatXAI
llm = ChatXAI(model="grok-4", temperature=0).bind_tools([{"type": "web_search"}])
response = llm.invoke("Provide me a digest of world news in the last 24 hours.")
response.contentSee xAI's tools documentation for the current set of built-in tools, including Web Search and X Search.
Reasoning effort
Certain xAI models support the standard reasoning_effort parameter, which controls the amount of reasoning the model does. It can be set at model construction or per invocation:
from langchain_xai import ChatXAI
model = ChatXAI(model="grok-4.3")
response = model.invoke(
"Analyze the trade-offs between microservices and monolithic architectures",
reasoning_effort="high",
)Note
reasoning_effort as a standard parameter requires langchain-xai>=1.3.0. ChatXAI sends it nested under extra_body.reasoning_effort.
Check a model's profile for the effort levels it supports:
model.profile["reasoning_effort_levels"] # e.g. ['low', 'medium', 'high']API reference
For detailed documentation of all ChatXAI features and configurations, head to the API reference.