Skip to content

Conversation

@KushagraaWadhwa
Copy link

@KushagraaWadhwa KushagraaWadhwa commented Nov 14, 2025

Description

Adds comprehensive test coverage and documentation for X.AI's agentic tool calling API support in langchain-xai.

X.AI is deprecating the Live Search API by December 15, 2025 in favor of the new agentic tool calling API (docs). While the server_tools parameter already provides support for this feature, it lacked test coverage and documentation visibility, leading to user confusion.

Issue: Closes #33961

Changes

🧪 Test Coverage Added

Unit Tests (tests/unit_tests/test_chat_models.py):

  • test_server_tools_param(): Validates server_tools parameter handling
  • test_server_tools_and_search_parameters_conflict(): Ensures both APIs can't be used together
  • test_search_parameters_deprecation_warning(): Verifies deprecation warnings

Integration Tests (tests/integration_tests/test_chat_models.py):

  • test_server_tools_web_search(): Tests autonomous web search
  • test_server_tools_x_search(): Tests X (Twitter) search
  • test_server_tools_code_execution(): Tests code execution capability
  • test_server_tools_multiple(): Tests using multiple tools simultaneously

📚 Documentation Enhancements

  • Enhanced ChatXAI docstring with comprehensive agentic tool calling examples
  • Added deprecation notices for search_parameters API
  • Added validation to prevent using both old and new APIs simultaneously
  • Added deprecation warnings with clear migration path
  • Updated README with migration notice and quick examples

🔧 Code Quality

  • Fixed import linting errors (PLC0415, I001)
  • Properly sorted imports into standard groups

Migration Example

# Old (deprecated - remove by Dec 15, 2025)
model = ChatXAI(
    model="grok-4",
    search_parameters={"mode": "auto"},
)

# New (recommended)
model = ChatXAI(
    model="grok-4",
    server_tools=[{"type": "web_search"}],
)

# Multiple tools
model = ChatXAI(
    model="grok-4",
    server_tools=[
        {"type": "web_search"},
        {"type": "x_search"},
        {"type": "code_execution"},
    ],
)

Available Server Tools

  • web_search: Autonomous web search for current information
  • x_search: Search posts on X (formerly Twitter)
  • code_execution: Execute code to solve complex problems

Dependencies

None - no new dependencies added.

Testing

cd libs/partners/xai
make format  # ✅ Passed
make lint    # ✅ Passed
make test    # ✅ Passed

All tests pass locally and are backwards compatible.

Breaking Changes

None - this PR is fully backward compatible. The old search_parameters API continues to work with deprecation warnings.

SHTLPMac027 added 3 commits November 14, 2025 15:21
- Add unit tests for server_tools parameter validation
- Add integration tests for web_search, x_search, and code_execution
- Enhance ChatXAI docstring with agentic tools examples
- Add deprecation warnings for search_parameters API
- Add validation to prevent using both old and new APIs
- Update README with migration notice

The server_tools parameter enables X.AI's new agentic tool calling API,
replacing the deprecated search_parameters (sunset Dec 15, 2025).

Available tools:
- web_search: Autonomous web search
- x_search: Search X (Twitter) posts
- code_execution: Execute code to solve problems
PLC0415: Import statements must be at module level, not inside functions.
Moved 'import warnings' from validate_environment method to top of file.
Reorder imports into proper groups:
1. __future__ imports
2. Standard library (warnings, typing)
3. Third-party (openai, pydantic, typing_extensions)
4. Local packages (langchain_core, langchain_openai)

Fixes ruff I001: Import block is un-sorted or un-formatted
@github-actions github-actions bot added documentation Improvements or additions to documentation integration Related to a provider partner package integration xai labels Nov 14, 2025
@KushagraaWadhwa KushagraaWadhwa changed the title Feat/xai agentic tools clean feat(xai): Add comprehensive tests and docs for agentic tool calling API Nov 14, 2025
@github-actions github-actions bot added documentation Improvements or additions to documentation feature and removed documentation Improvements or additions to documentation feature labels Nov 14, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 14, 2025

CodSpeed Performance Report

Merging #33967 will not alter performance

Comparing KushagraaWadhwa:feat/xai-agentic-tools-clean (06a288a) with master (8a3bb73)

Summary

✅ 1 untouched
⏩ 33 skipped1

Footnotes

  1. 33 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Alphabetize third-party imports (langchain_core before pydantic)
to match the repository's import ordering rules.
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 14, 2025
SHTLPMac027 added 3 commits November 14, 2025 16:50
- Add None check before len() on server_tools (can be Optional)
- Handle response.content as str or list for .lower() call

Fixes mypy errors:
- tests/unit_tests/test_chat_models.py:149 (arg-type)
- tests/integration_tests/test_chat_models.py:149 (union-attr)
Break long line into multi-line expression to satisfy E501 (line <= 88 chars)
Use formatter-preferred single-line ternary within parentheses
@github-actions github-actions bot removed feature documentation Improvements or additions to documentation labels Nov 14, 2025
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 15, 2025
@hyacinthus
Copy link

hyacinthus commented Nov 17, 2025

Thanks for this PR. I noticed that XAI should proactively unify API with OpenAI. However, in the LangChain integration, server_tools differs from OpenAI. The OpenAI package directly shares the bind_tools method. Is the OpenAI package also preparing for migration? If not, I think it's best to unify the behavior of both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation feature integration Related to a provider partner package integration xai

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XAI Live Search API will be deprecated, use tools instead

3 participants