The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(xai): Live search deprecation (#34919)
Rohan Disa40048838067b
diff --git a/libs/partners/xai/langchain_xai/chat_models.py b/libs/partners/xai/langchain_xai/chat_models.pyindex 0fe7df227d..ca05da76c1 100644--- a/libs/partners/xai/langchain_xai/chat_models.py+++ b/libs/partners/xai/langchain_xai/chat_models.py@@ -2,6 +2,7 @@from __future__ import annotations+import warningsfrom typing import TYPE_CHECKING, Any, Literal, TypeAlias, castimport openai@@ -338,30 +339,12 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override])```- Live Search:- xAI supports a [Live Search](https://docs.x.ai/docs/guides/live-search)- feature that enables Grok to ground its answers using results from web searches.-- ```python- from langchain_xai import ChatXAI-- model = ChatXAI(- model="grok-4",- search_parameters={- "mode": "auto",- # Example optional parameters below:- "max_search_results": 3,- "from_date": "2025-05-26",- "to_date": "2025-05-27",- },- )-- model.invoke("Provide me a digest of world news in the last 24 hours.")- ```-- !!! note- [Citations](https://docs.x.ai/docs/guides/live-search#returning-citations)- are only available in [Grok 3](https://docs.x.ai/docs/models/grok-3).+ Web search:+ **Live Search** (the legacy `search_parameters` option) has been deprecated by xAI.+ Use `bind_tools` with compatible tool definitions when using the OpenAI-compatible+ Responses API instead. If you pass `search_parameters` to `ChatXAI`, a+ `DeprecationWarning` is emitted and the parameter is ignored; requests otherwise+ succeed without search.Token usage:```python@@ -425,7 +408,12 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]xai_api_base: str = Field(default="https://api.x.ai/v1/")"""Base URL path for API requests."""search_parameters: dict[str, Any] | None = None- """Parameters for search requests. Example: `{"mode": "auto"}`."""+ """**Deprecated.** Use web search tools instead:++ ```python+ ChatXAI(model="...").bind_tools([{"type": "web_search"}])+ ```+ """openai_api_key: SecretStr | None = Noneopenai_api_base: str | None = None@@ -484,6 +472,19 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]params["ls_provider"] = "xai"return params+ @model_validator(mode="after")+ def _warn_search_parameters_deprecated(self) -> Self:+ """Emit deprecation warning if search_parameters (Live Search) is used."""+ if self.search_parameters:+ warnings.warn(+ "search_parameters (Live Search) is deprecated by xAI and is ignored. "+ 'Use `ChatXAI(model="...").bind_tools([{"type": "web_search"}])` '+ "instead.",+ DeprecationWarning,+ stacklevel=2,+ )+ return self+@model_validator(mode="after")def validate_environment(self) -> Self:"""Validate that api key and python package exists in environment."""@@ -542,18 +543,6 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override]self.profile = _get_default_model_profile(self.model_name)return self- @property- def _default_params(self) -> dict[str, Any]:- """Get default parameters."""- params = super()._default_params- if self.search_parameters:- if "extra_body" in params:- params["extra_body"]["search_parameters"] = self.search_parameters- else:- params["extra_body"] = {"search_parameters": self.search_parameters}-- return params-def _stream(self, *args: Any, **kwargs: Any) -> Iterator[ChatGenerationChunk]:"""Route to Chat Completions or Responses API."""if self._use_responses_api({**kwargs, **self.model_kwargs}):