AI Chatbot blocks fail to render: Uninitialized $userMessage property in AiAssistantApiRunner
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3555856. -->
Reported by: [jeffpurtle](https://www.drupal.org/user/296334)
Related to !1303
>>>
<p># Drupal.org Issue Report</p>
<p>## Title<br>
AI Chatbot blocks fail to render: Uninitialized $userMessage property in AiAssistantApiRunner</p>
<p>## Component<br>
AI Assistant API</p>
<p>## Version<br>
1.2.2</p>
<p>## Category<br>
Bug report</p>
<p>## Priority<br>
Normal</p>
<p>## Issue Summary<br>
Both `ai_chatbot_block` and `ai_deepchat_block` fail to render on pages due to an uninitialized typed property error in the `AiAssistantApiRunner` class. This prevents chatbot blocks from appearing on the site, even when properly configured.</p>
<p>## Steps to Reproduce<br>
1. Install AI module 1.2.2<br>
2. Configure an AI Assistant with RAG capabilities<br>
3. Place either `ai_chatbot_block` or `ai_deepchat_block` in any region<br>
4. Enable the block<br>
5. Visit a page where the block should appear<br>
6. Block does not render; error appears in logs</p>
<p>## Expected Result<br>
The chatbot block should render successfully and display the chat interface.</p>
<p>## Actual Result<br>
The block fails to render with the following error:</p>
<p>```<br>
Error: Typed property Drupal\ai_assistant_api\AiAssistantApiRunner::$userMessage<br>
must not be accessed before initialization in<br>
Drupal\ai_assistant_api\AiAssistantApiRunner->getMessageHistory()<br>
(line 578 of modules/ai_assistant_api/src/AiAssistantApiRunner.php)<br>
```</p>
<p>## Root Cause Analysis<br>
The `getMessageHistory()` method in `AiAssistantApiRunner.php` attempts to access the `$userMessage` typed property without checking if it has been initialized. </p>
<p>In line 578, when session storage is disabled (`!$this->shouldStoreSession()`), the method tries to return the user message directly:</p>
<p>```php<br>
// Otherwise just return the last message.<br>
return [<br>
['role' => 'user', 'message' => $this->userMessage->getMessage()],<br>
];<br>
```</p>
<p>However, when the chatbot block initially renders (before any user interaction), the `$userMessage` property has not been set via `setUserMessage()`. Since this is a typed property declared as `protected UserMessage|NULL $userMessage;` (line 42), PHP 8.x throws an error when attempting to access it before initialization.</p>
<p>## Technical Details<br>
- The property is declared but not initialized with a default value<br>
- The `getMessageHistory()` method is called during block rendering<br>
- At render time, no user message exists yet<br>
- Accessing an uninitialized typed property triggers a fatal error in PHP 8.x</p>
<p>## Proposed Solution<br>
Add a null check using `isset()` before accessing the `$userMessage` property. If the property hasn't been initialized, return an empty array since there is no message history yet.</p>
<p>## Patch<br>
The attached patch adds proper null checking to prevent accessing the uninitialized property:</p>
<p>1. Checks if `$userMessage` is set using `isset()`<br>
2. Only accesses the property if it exists and is truthy<br>
3. Returns an empty array if no message exists yet (which is appropriate for initial render)</p>
<p>This follows PHP best practices for typed properties and prevents the fatal error while maintaining the intended functionality.</p>
<p>## Testing<br>
After applying the patch:</p>
<p>1. ✅ Chatbot blocks render successfully without errors<br>
2. ✅ Blocks appear on pages as configured<br>
3. ✅ Chat functionality works correctly once user sends a message<br>
4. ✅ Message history is properly maintained after first message<br>
5. ✅ RAG (Retrieval Augmented Generation) functionality works as expected<br>
6. ✅ No regression in existing functionality</p>
<p>Tested with:<br>
- Drupal 11.x<br>
- AI module 1.2.2<br>
- PHP 8.3<br>
- Both `ai_chatbot_block` and `ai_deepchat_block`<br>
- RAG configuration with Pinecone vector database<br>
- OpenAI integration (gpt-4o-mini)</p>
<p>## Additional Context<br>
This issue affects any site attempting to use the AI chatbot blocks introduced in version 1.2.2. Without this fix, the chatbot functionality is completely unusable via blocks, though the underlying AI Assistant API works correctly when called programmatically.</p>
<p>## Related Code Files<br>
- `modules/ai_assistant_api/src/AiAssistantApiRunner.php` (lines 42, 577-580)<br>
- `modules/ai_chatbot/src/Plugin/Block/DeepChatFormBlock.php` (line 756 - calls getMessageHistory)<br>
- `modules/ai_chatbot/src/Plugin/Block/ChatFormBlock.php` (similar issue)</p>
<p>## Backwards Compatibility<br>
This patch is fully backwards compatible. It:<br>
- Does not change any public APIs<br>
- Does not alter expected behavior<br>
- Only adds defensive programming to handle an edge case<br>
- Returns an appropriate empty array when no message exists yet</p>
<p>## Credit<br>
Issue discovered and patch created during implementation of AI chatbot with RAG functionality on Drupal 11 site.</p>
issue