langchain-ai
langchain
BlogDocsChangelog

xai[patch]: Fix 400 error for AIMessages with tool_calls but no content

#34229
Comparing
arrdel:fix-chatxai-empty-content-34140
(
35c1c1f
) with
master
(
80c3970
)
CodSpeed Performance Gauge
0%
Untouched
1
Skipped
33

Benchmarks

Skipped (33)

Passed

test_init_time
libs/partners/xai/tests/unit_tests/test_chat_models_standard.py::TestXAIStandard
CodSpeed Performance Gauge
0%
3.3 s*3.3 s

Commits

Click on a commit to change the comparison range
Base
master
80c3970
+0.01%
Fix ChatXAI: Ensure AIMessages with tool_calls have empty content instead of None xAI's API is stricter than OpenAI's and requires all messages to have at least an empty content field. When using LangGraph agents with tool calling, AIMessages can have tool_calls but no content, which causes xAI to return a 400 error: 'Invalid request content: Each message must have at least one content element.' This fix overrides the message conversion logic in ChatXAI to ensure that AIMessages with tool_calls or function_call always have at least an empty string for content, rather than None (which is acceptable for OpenAI but not xAI). Changes: - Added _convert_message_to_dict_xai() function that wraps the OpenAI converter and ensures content is never None for AIMessages with tool calls - Overrode _get_request_payload() to use the xAI-specific converter - Added necessary imports from langchain_openai.chat_models.base Fixes #34140
35c1c1f
20 hours ago
by arrdel
© 2025 CodSpeed Technology
Home Terms Privacy Docs