Problem/Motivation
My current setup is using:
AI module version : 1.0.4
OpenAI Provider: 1.0.2
I have observed a fatal error while saving a node after enabling automator for a body field, that uses "LLM: text" and "Default vision model" using an image field.
TypeError: Drupal\ai\OperationType\Chat\ChatMessage::__construct(): Argument #2 ($text) must be of type string, null given, called in /var/www/html/docroot/modules/contrib/ai_provider_openai/src/Plugin/AiProvider/OpenAiProvider.php on line 342 in Drupal\ai\OperationType\Chat\ChatMessage->__construct() (line 47 of modules/contrib/ai/src/OperationType/Chat/ChatMessage.php).
I saw that the response is not always returning a string , its null sometimes and then its being pased to the ChatMessage class.
$message = new ChatMessage($response['choices'][0]['message']['role'], $response['choices'][0]['message']['content']);
RESPONSE example:
"id" => "chatcmpl-AzfllweZLeHljsk901oECIOtGmLTT"
"object" => "chat.completion"
"created" => 1739261933
"model" => "gpt-4o-2024-08-06"
"system_fingerprint" => "fp_4691090a87"
"choices" => array:1 [▼
0 => array:3 [▼
"index" => 0
"message" => array:2 [▼
"role" => "assistant"
"content" => null
]
"finish_reason" => "stop"
]
]
"usage" => array:5 [▼
"prompt_tokens" => 637
"completion_tokens" => 12
"total_tokens" => 649
"prompt_tokens_details" => array:2 [▶]
"completion_tokens_details" => array:4 [▶]
]
Steps to reproduce
- Enable ai automator module
- use any content type and add a image field to the content type.
- For Text (formatted, long, with summary) e.g body field enable the automator using "LLM: text"
- use the default vision model and chose the image field type under provider configuration.
- make sure you add a promt for the field.
- save the settings and create a new node for the same. (The issue is appearing intermittently)
Proposed resolution
adding a null coalescing operator to this line has fixed the issue:
$message = new ChatMessage($response['choices'][0]['message']['role'], $response['choices'][0]['message']['content']);
TO
$message = new ChatMessage($response['choices'][0]['message']['role'], $response['choices'][0]['message']['content'] ?? "");
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork ai_provider_openai-3505750
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
vakulrai commentedComment #3
vakulrai commentedComment #5
vakulrai commentedComment #6
vakulrai commentedComment #7
mrdalesmith commentedLGTM
Comment #8
marcus_johansson commentedThis seems to have been fixed elsewhere, but I'll give out credits. Thanks