{"id":4973,"library":"langchain-xai","title":"LangChain xAI Integration","description":"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.","status":"active","version":"1.2.2","language":"en","source_language":"en","source_url":"https://github.com/langchain-ai/langchain/tree/master/libs/partners/xai","tags":["AI","LLM","LangChain","xAI","Grok","integration","chatbots"],"install":[{"cmd":"pip install langchain-xai","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core LangChain functionalities and base abstractions.","package":"langchain-core","optional":false},{"reason":"Asynchronous HTTP client for network requests.","package":"aiohttp","optional":false},{"reason":"Synchronous HTTP client for network requests.","package":"requests","optional":false}],"imports":[{"note":"As a partner integration, ChatXAI is imported directly from its dedicated package, not from the top-level langchain.chat_models module.","wrong":"from langchain.chat_models import ChatXAI","symbol":"ChatXAI","correct":"from langchain_xai import ChatXAI"}],"quickstart":{"code":"import os\nfrom langchain_xai import ChatXAI\nfrom langchain_core.messages import HumanMessage, SystemMessage\n\n# Set your xAI API key as an environment variable (e.g., in your shell: export XAI_API_KEY=\"YOUR_API_KEY\")\n# Or pass it directly as xai_api_key=\"YOUR_API_KEY\" to ChatXAI\napi_key = os.environ.get(\"XAI_API_KEY\", \"YOUR_XAI_API_KEY_HERE\") # Replace with actual key or ensure env var is set\n\nif api_key == \"YOUR_XAI_API_KEY_HERE\":\n    print(\"Warning: XAI_API_KEY not found in environment variables. Please set it or pass it directly.\")\n    print(\"Skipping example due to missing API key.\")\nelse:\n    model = ChatXAI(\n        model=\"grok-4\",\n        temperature=0,\n        xai_api_key=api_key # Pass the API key\n    )\n\n    messages = [\n        SystemMessage(content=\"You are a helpful assistant.\"),\n        HumanMessage(content=\"What is the capital of France?\"),\n    ]\n\n    print(\"Invoking Grok model...\")\n    response = model.invoke(messages)\n    print(response.content)\n\n    print(\"\\nStreaming response:\")\n    for chunk in model.stream(messages):\n        print(chunk.content, end=\"\")\n    print()","lang":"python","description":"This example demonstrates how to initialize the `ChatXAI` model, pass messages, and invoke it to get a response. It also shows how to stream responses. Ensure your `XAI_API_KEY` is set as an environment variable or passed directly to the `ChatXAI` constructor."},"warnings":[{"fix":"Verify that you are importing `ChatXAI` from `langchain_xai` and using the `XAI_API_KEY` for authentication. Do not confuse with `langchain-groq`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Set the `XAI_API_KEY` environment variable (e.g., `export XAI_API_KEY=\"your_key_here\"`) or explicitly pass `xai_api_key=\"your_key_here\"` to the `ChatXAI` constructor.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Regularly update `langchain-core` alongside `langchain-xai` by running `pip install -U langchain-core langchain-xai`.","message":"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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For Python, use `pip install langchain-xai` and `from langchain_xai import ChatXAI`. Do not confuse with the npm package or its import syntax.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}