Commits
Click on a commit to change the comparison rangeFix 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