Fix xAI driver compatibility with OpenAI-compatible endpoints#964
Fix xAI driver compatibility with OpenAI-compatible endpoints#964meirdick 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.
|
Note: The |
|
Hi @meirdick as I understood current workaround is your package: /prism-workers-ai (thank you) But is definetly nice to have this built-in in prism |
|
Hi @SergkeiM, yes that is correct. The package provides the full driver for complete worker-ai compatibility. It also makes for a clean automated implementation via this skill: |
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.