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

Created on 11 February 2025, 3 months ago

Problem/Motivation

My currentr setup is using:
AI module version : 1.0.4
OpenAI Provider: 1.0.2

I have observed the fatal error while saving the content after enabling automator for a body field, that uses "LLM: text" and "Default vision model" using a 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 somtimes and then its being pased to the ChatMessage class.
$message = new ChatMessage($response['choices'][0]['message']['role'], $response['choices'][0]['message']['content']);

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']);

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India vakulrai

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024