Home / Class/ ChatXAI Class — langchain Architecture

ChatXAI Class — langchain Architecture

Architecture documentation for the ChatXAI class in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  44814818_ed14_7dba_0cd5_a8f2cd67fb61["ChatXAI"]
  2a683305_667b_3567_cab9_9f77e29d4afa["BaseChatOpenAI"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|extends| 2a683305_667b_3567_cab9_9f77e29d4afa
  0998183a_ee20_cc02_d37b_948998ae74b7["AIMessageChunk"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|extends| 0998183a_ee20_cc02_d37b_948998ae74b7
  e9ffe984_e0f5_66ff_caa3_cb6d150e56ab["chat_models.py"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|defined in| e9ffe984_e0f5_66ff_caa3_cb6d150e56ab
  3486ae17_e9fe_4fbf_d40a_fbd69671f0bf["lc_secrets()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| 3486ae17_e9fe_4fbf_d40a_fbd69671f0bf
  30a550ee_5158_6abf_055d_727d3c7a4410["get_lc_namespace()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| 30a550ee_5158_6abf_055d_727d3c7a4410
  3b6fdb3e_74f7_1907_4998_33c47c423d32["lc_attributes()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| 3b6fdb3e_74f7_1907_4998_33c47c423d32
  bb31dcd4_234f_61bc_0ac5_6996bd3a4315["is_lc_serializable()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| bb31dcd4_234f_61bc_0ac5_6996bd3a4315
  43d5302c_b85d_a5fd_a03f_9265b6c59ecb["_llm_type()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| 43d5302c_b85d_a5fd_a03f_9265b6c59ecb
  7f6e9039_ac5d_6c73_d456_dc1049d7d30e["_get_ls_params()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| 7f6e9039_ac5d_6c73_d456_dc1049d7d30e
  6b4c5a93_f8d3_047a_9128_2076a8dc83fb["_warn_search_parameters_deprecated()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| 6b4c5a93_f8d3_047a_9128_2076a8dc83fb
  df8a3094_663c_7104_b67f_a6b2a74aa1a7["validate_environment()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| df8a3094_663c_7104_b67f_a6b2a74aa1a7
  b07ffba0_f074_e5c3_8098_35d4b1d0bf59["_set_model_profile()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| b07ffba0_f074_e5c3_8098_35d4b1d0bf59
  698feae5_7712_e1d0_4752_695c5302d9f4["_stream()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| 698feae5_7712_e1d0_4752_695c5302d9f4
  a7c0fb81_f09e_2022_4cff_91a797070c9c["_astream()"]
  44814818_ed14_7dba_0cd5_a8f2cd67fb61 -->|method| a7c0fb81_f09e_2022_4cff_91a797070c9c

Relationship Graph

Source Code

libs/partners/xai/langchain_xai/chat_models.py lines 43–734

class ChatXAI(BaseChatOpenAI):  # type: ignore[override]
    r"""ChatXAI chat model.

    Refer to [xAI's documentation](https://docs.x.ai/docs/api-reference#chat-completions)
    for more nuanced details on the API's behavior and supported parameters.

    Setup:
        Install `langchain-xai` and set environment variable `XAI_API_KEY`.

        ```bash
        pip install -U langchain-xai
        export XAI_API_KEY="your-api-key"
        ```

    Key init args — completion params:
        model:
            Name of model to use.
        temperature:
            Sampling temperature between `0` and `2`. Higher values mean more random completions,
            while lower values (like `0.2`) mean more focused and deterministic completions.
            (Default: `1`.)
        max_tokens:
            Max number of tokens to generate. Refer to your [model's documentation](https://docs.x.ai/docs/models#model-pricing)
            for the maximum number of tokens it can generate.
        logprobs:
            Whether to return logprobs.

    Key init args — client params:
        timeout:
            Timeout for requests.
        max_retries:
            Max number of retries.
        api_key:
            xAI API key. If not passed in will be read from env var `XAI_API_KEY`.

    Instantiate:
        ```python
        from langchain_xai import ChatXAI

        model = ChatXAI(
            model="grok-4",
            temperature=0,
            max_tokens=None,
            timeout=None,
            max_retries=2,
            # api_key="...",
            # other params...
        )
        ```

    Invoke:
        ```python
        messages = [
            (
                "system",
                "You are a helpful translator. Translate the user sentence to French.",
            ),
            ("human", "I love programming."),
        ]
        model.invoke(messages)
        ```

        ```python
        AIMessage(
            content="J'adore la programmation.",
            response_metadata={
                "token_usage": {
                    "completion_tokens": 9,
                    "prompt_tokens": 32,
                    "total_tokens": 41,
                },
                "model_name": "grok-4",
                "system_fingerprint": None,
                "finish_reason": "stop",
                "logprobs": None,
            },
            id="run-168dceca-3b8b-4283-94e3-4c739dbc1525-0",
            usage_metadata={
                "input_tokens": 32,
                "output_tokens": 9,
                "total_tokens": 41,

Frequently Asked Questions

What is the ChatXAI class?
ChatXAI is a class in the langchain codebase, defined in libs/partners/xai/langchain_xai/chat_models.py.
Where is ChatXAI defined?
ChatXAI is defined in libs/partners/xai/langchain_xai/chat_models.py at line 43.
What does ChatXAI extend?
ChatXAI extends BaseChatOpenAI, AIMessageChunk.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free