TypeError in AgentRunner::runAsAgent() when agent solve() returns null
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3577216. --> Reported by: [scontzen](https://www.drupal.org/user/3838981) Related to !1286 >>> <p>[Tracker]<br> <strong>Update Summary: </strong>[One-line status update for stakeholders]<br> <strong>Short Description: </strong>[One-line issue summary for stakeholders]<br> <strong>Check-in Date: </strong>MM/DD/YYYY<br> <em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br> [/Tracker]</p> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>When using the AI Chatbot with an agent-based assistant, the following TypeError occurs intermittently:</p> <p><code>TypeError: Drupal\ai\OperationType\Chat\ChatMessage::__construct(): Argument #2 ($text) must be of type string, null given, called in modules/contrib/ai/modules/ai_assistant_api/src/Service/AgentRunner.php on line 101</code></p> <p> <code>$agent-&gt;solve()</code> returns <code>$this-&gt;question (AiAgentEntityWrapper::solve()</code>). This property is set via <code>$response-&gt;getText()</code>, which can return null when the LLM response contains no textual content, for example, when the model only produces tool calls without accompanying text.</p> <h4 id="summary-steps-reproduce">Steps to reproduce</h4> <p><strong>Environment</strong></p> <p>- Drupal: 10.6.2<br> - AI module (drupal/ai): 1.2.11<br> - AI Agents (drupal/ai_agents): 1.2.1<br> - OpenAI Provider (drupal/ai_provider_openai): 1.2.0<br> - AI Chatbot submodule enabled (part of drupal/ai)<br> - AI Assistant API submodule enabled (part of drupal/ai)<br> - LLM: OpenAI GPT-5 (gpt-5-chat-latest)<br> - Agent tool: RAG search (ai_search:rag_search)<br> - Browser: Reproducible in all browsers (server-side error)</p> <p><strong>Steps</strong><br> 1. Configure an AI Assistant with an agent (e.g., a RAG search agent)<br> 2. Use the AI Chatbot to chat with the assistant<br> 3. Ask a question where the LLM produces no textual response (e.g., only tool calls, or the RAG tool returns no relevant results)<br> 4. The chatbot crashes with the TypeError above</p> <p>The error is intermittent, it depends on the LLM's response for a given query.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>A null coalescing fallback on line 101:</p> <p><code>$message = new ChatMessage('assistant', $response ?? '');</code></p> <p>This ensures a valid (empty) string is always passed. In practice, when <code>$response</code> is null, the chat history (line 103-106) usually contains the actual answer from the tool interaction, so the user still gets a proper response - the <code>$message</code> variable gets overwritten with the last history entry.</p>
issue