Fix xAI driver compatibility with OpenAI-compatible endpoints#964
Open
meirdick wants to merge 1 commit intoprism-php:mainfrom
Open
Fix xAI driver compatibility with OpenAI-compatible endpoints#964meirdick wants to merge 1 commit intoprism-php:mainfrom
meirdick wants to merge 1 commit intoprism-php:mainfrom
Conversation
Two changes to improve compatibility with OpenAI-compatible providers (e.g. Cloudflare Workers AI) that use the xAI driver: 1. MessageMap: Use plain string content for text-only user messages, array content format only when images are present. Some /chat/completions endpoints only accept string content and reject the array format with errors like "Type mismatch of '/messages/0/content', 'array' not in 'string'". 2. Structured handler: Handle content returned as a parsed object/array instead of a JSON string. Some providers return structured output directly as an object in the content field rather than a JSON-encoded string with a separate parsed field. The handler now normalizes this by json_encoding array content and using it as the parsed data when no parsed field is present. Both changes are backward-compatible with xAI's native API.
Author
|
Note: The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The xAI driver doesn't work with OpenAI-compatible
/chat/completionsendpoints like Cloudflare Workers AI's/compatendpoint. Two issues:1. User messages always use array content format
MessageMapserializes all user messages as:{"role": "user", "content": [{"type": "text", "text": "Hello"}]}Some OpenAI-compatible endpoints only accept string content:
{"role": "user", "content": "Hello"}The array format causes the model to receive garbled input and respond with nonsense, or reject the request with:
"Type mismatch of '/messages/0/content', 'array' not in 'string'".2. Structured handler crashes when content is returned as object
The structured handler expects
choices.0.message.contentto be a string (per the OpenAI spec), but some providers return it as a parsed object/array:This crashes on
new AssistantMessage($content)with:TypeError: Argument #1 ($content) must be of type string, array given.Fix
MessageMap: Use plain string content when no images are present, array format only when images exist. Both formats are valid per the OpenAI spec.
Structured handler: Normalize array content to a JSON string via
json_encode(), and use it as the parsed data when noparsedfield is present.Both changes are backward-compatible with xAI's native API — verified by all existing tests passing.
Testing
it('handles content returned as object instead of string')covering the Workers AI response formatMessageMapTestto expect string content for text-only messagesContext
Relates to #520. The xAI driver is commonly used with Cloudflare Workers AI (via the
/compatendpoint) because it sends to/chat/completionsrather than/responses. These two fixes make that combination work reliably for text generation and structured output.