feat(xai): support base_url alias and XAI_API_BASE env var (#35790)

Add `base_url` alias and `XAI_API_BASE` env variable support to
`ChatXAI.xai_api_base`, aligning the xAI integration with the pattern
used across other partner packages (OpenAI, Groq, Fireworks, etc.).
Previously the base URL was a plain string field with no alias or
env-var lookup, making it inconsistent with the rest of the ecosystem
and harder to configure in deployment environments.

## Changes
- Add `alias="base_url"` and `default_factory=from_env("XAI_API_BASE",
default="https://api.x.ai/v1/")` to `ChatXAI.xai_api_base`, matching the
convention in `langchain_openai`, `langchain_groq`, and
`langchain_fireworks`
This commit is contained in:
Mason Daugherty
2026-03-12 10:55:36 -04:00
committed by GitHub
parent b676167707
commit 6b9b4c6546
5 changed files with 45 additions and 10 deletions

View File

@@ -17,6 +17,7 @@ def test_reasoning(output_version: Literal["", "v1"]) -> None:
"""Test reasoning features.
!!! note
`grok-4` does not return `reasoning_content`, but may optionally return
encrypted reasoning content if `use_encrypted_content` is set to `True`.
"""
@@ -25,12 +26,14 @@ def test_reasoning(output_version: Literal["", "v1"]) -> None:
chat_model = ChatXAI(
model="grok-3-mini",
reasoning_effort="low",
temperature=0,
output_version=output_version,
)
else:
chat_model = ChatXAI(
model="grok-3-mini",
reasoning_effort="low",
temperature=0,
)
input_message = "What is 3^3?"
response = chat_model.invoke(input_message)
@@ -95,7 +98,7 @@ def test_reasoning(output_version: Literal["", "v1"]) -> None:
def test_web_search() -> None:
llm = ChatXAI(model=MODEL_NAME).bind_tools([{"type": "web_search"}])
llm = ChatXAI(model=MODEL_NAME, temperature=0).bind_tools([{"type": "web_search"}])
# Test invoke
response = llm.invoke("Look up the current time in Boston, MA.")