Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain ChatXAI

From Leeroopedia
Revision as of 11:24, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Langchain_ai_Langchain_ChatXAI.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains LLM Integration, Chat Models, xAI
Last Updated 2026-02-11 00:00 GMT

Overview

LangChain chat model wrapper around xAI's Chat Completions API, providing access to Grok models with tool calling, structured output, reasoning, and streaming.

Description

ChatXAI is a class in the langchain-xai partner package that extends BaseChatOpenAI from langchain-openai. It provides a LangChain-compatible interface to xAI's Grok family of models via the OpenAI-compatible API at api.x.ai. The class supports tool calling, structured output (via function_calling, json_mode, or json_schema methods), streaming with usage metadata, reasoning content from compatible models (e.g., grok-3-mini), logprobs, and web search via tool bindings. It automatically routes between the Chat Completions API and the Responses API as appropriate.

Usage

Import ChatXAI when building LangChain applications that use xAI's Grok models for chat completions, tool calling, or structured output generation.

Code Reference

Source Location

Signature

class ChatXAI(BaseChatOpenAI):
    model_name: str = Field(default="grok-4", alias="model")
    xai_api_key: SecretStr | None = Field(
        alias="api_key",
        default_factory=secret_from_env("XAI_API_KEY", default=None),
    )
    xai_api_base: str = Field(default="https://api.x.ai/v1/")
    search_parameters: dict[str, Any] | None = None  # Deprecated
    openai_api_key: SecretStr | None = None
    openai_api_base: str | None = None

Import

from langchain_xai import ChatXAI

I/O Contract

Inputs

Name Type Required Description
model_name str No Model name to use. Defaults to "grok-4". Also accepts "model" alias.
xai_api_key SecretStr or None No xAI API key. Reads from XAI_API_KEY environment variable if not set.
xai_api_base str No Base URL for API requests. Defaults to "https://api.x.ai/v1/".
temperature float No Sampling temperature between 0 and 2. Inherited from BaseChatOpenAI.
max_tokens int or None No Maximum number of tokens to generate. Inherited from BaseChatOpenAI.
streaming bool No Whether to stream results. Inherited from BaseChatOpenAI.
max_retries int or None No Maximum retries on failure. Inherited from BaseChatOpenAI.
timeout float or None No Request timeout. Inherited from BaseChatOpenAI.
logprobs bool No Whether to return logprobs. Inherited from BaseChatOpenAI.
search_parameters dict or None No Deprecated. Use bind_tools with web_search tool definitions instead.

Outputs

Name Type Description
ChatResult ChatResult Contains AIMessage with content, usage_metadata, response_metadata (including model_provider: "xai"), and additional_kwargs (reasoning_content, citations when available).
ChatGenerationChunk ChatGenerationChunk When streaming, yields chunks with incremental content, reasoning_content in additional_kwargs, and citations.

Key Methods

Method Description
_stream(*args, **kwargs) Routes to Chat Completions or Responses API based on kwargs.
_astream(*args, **kwargs) Async routing to Chat Completions or Responses API.
_create_chat_result(response, generation_info) Extends parent to add reasoning_content, citations, and xAI-specific reasoning token accounting.
_convert_chunk_to_generation_chunk(chunk, ...) Extends parent to extract reasoning_content and citations from streaming chunks.
with_structured_output(schema, method, include_raw, strict, **kwargs) Returns a Runnable for structured output. Supports "function_calling", "json_mode", and "json_schema" methods.
validate_environment() Validates API key and initializes OpenAI clients pointing to xAI's endpoint.
_get_ls_params(stop, **kwargs) Returns LangSmith parameters with ls_provider set to "xai".

Usage Examples

Basic Usage

from langchain_xai import ChatXAI

model = ChatXAI(model="grok-4", temperature=0, max_retries=2)
messages = [
    ("system", "You are a helpful translator. Translate the user sentence to French."),
    ("human", "I love programming."),
]
response = model.invoke(messages)
print(response.content)

Tool Calling

from pydantic import BaseModel, Field
from langchain_xai import ChatXAI

model = ChatXAI(model="grok-4")

class GetWeather(BaseModel):
    """Get the current weather in a given location"""
    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")

model_with_tools = model.bind_tools([GetWeather])
ai_msg = model_with_tools.invoke("What's the weather in SF?")
print(ai_msg.tool_calls)

Structured Output

from pydantic import BaseModel, Field
from langchain_xai import ChatXAI

class Joke(BaseModel):
    """Joke to tell user."""
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: int | None = Field(description="How funny the joke is, from 1 to 10")

model = ChatXAI(model="grok-4")
structured_model = model.with_structured_output(Joke)
result = structured_model.invoke("Tell me a joke about cats")
print(result)

Reasoning Model

from langchain_xai import ChatXAI

model = ChatXAI(
    model="grok-3-mini",
    extra_body={"reasoning_effort": "high"},
)
response = model.invoke("Explain the Riemann hypothesis.")
# Reasoning content is in additional_kwargs
print(response.additional_kwargs.get("reasoning_content"))

xAI-Specific Behavior

  • Reasoning token accounting: Unlike OpenAI, xAI reports reasoning tokens as less than completion tokens. ChatXAI assumes reasoning tokens are not counted in output tokens and adds them to the output token count.
  • Model provider metadata: All responses include "model_provider": "xai" in response_metadata.
  • Deprecated search_parameters: The legacy Live Search feature via search_parameters has been deprecated by xAI. A DeprecationWarning is emitted if used, and the parameter is ignored.
  • LangChain serializable: The class is marked as LangChain-serializable with namespace ["langchain_xai", "chat_models"].

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment