Type error and infinite AJAX request loop when the Agent is unable to resolve a request job
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3547470. --> Reported by: [codebymikey](https://www.drupal.org/user/3573206) Related to !1021 !894 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>When the AI agent is unable to solve a job, the following exception is thrown:</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>TypeError</span><span style="color: #007700">: </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">ai</span><span style="color: #007700">\</span><span style="color: #0000BB">OperationType</span><span style="color: #007700">\</span><span style="color: #0000BB">Chat</span><span style="color: #007700">\</span><span style="color: #0000BB">ChatMessage</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(): <br></span><span style="color: #0000BB">Argument </span><span style="color: #FF8000">#2 ($text) must be of type string, null given, called in /app/application/web/modules/contrib/ai/modules/ai_assistant_api/src/Service/AgentRunner.php on line 85 in<br></span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">ai</span><span style="color: #007700">\</span><span style="color: #0000BB">OperationType</span><span style="color: #007700">\</span><span style="color: #0000BB">Chat</span><span style="color: #007700">\</span><span style="color: #0000BB">ChatMessage</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">() (</span><span style="color: #0000BB">line 61 of </span><span style="color: #007700">/</span><span style="color: #0000BB">app</span><span style="color: #007700">/</span><span style="color: #0000BB">application</span><span style="color: #007700">/</span><span style="color: #0000BB">web</span><span style="color: #007700">/</span><span style="color: #0000BB">modules</span><span style="color: #007700">/</span><span style="color: #0000BB">contrib</span><span style="color: #007700">/</span><span style="color: #0000BB">ai</span><span style="color: #007700">/</span><span style="color: #0000BB">src</span><span style="color: #007700">/</span><span style="color: #0000BB">OperationType</span><span style="color: #007700">/</span><span style="color: #0000BB">Chat</span><span style="color: #007700">/</span><span style="color: #0000BB">ChatMessage</span><span style="color: #007700">.</span><span style="color: #0000BB">php</span><span style="color: #007700">).<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>Then after the initial error is thrown, requesting it to "Retry last instruction", triggers an infinite AJAX request loop because the last request given by the AI API can include a request to query the tool, but because the max loop of the agent has been reached, it keeps failing indefinitely.</p> <h4 id="summary-steps-reproduce">Steps to reproduce (required for bugs, but not feature requests)</h4> <p>Using the AI deep chat block with a pgvector-based database powered by ai_vdb_provider_postgres.</p> <p>1. Set up a RAG based agent similar to <a href="https://www.qed42.com/insights/setting-up-ai-powered-semantic-search-in-drupal">https://www.qed42.com/insights/setting-up-ai-powered-semantic-search-in-drupal</a> with a max loop level of 3.<br> 2. Ask it a question that it's unable to process due to lack of contextual data such as: "What is the first content published by John Doe?"<br> 3. Sometimes, rather than returning a final message stating that it could not resolve. The last request before the max loop might be for it to make a further request to the RAG tool, so it keeps doing that indefinitely.</p> <p>It seems related to the AI model being used. It's more apparent when using the <code>azure/gpt-5-mini</code> model in LiteLLM. Switching to a different model such as <code>azure/gpt-5-chat</code> provides results before the loop max is reached.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>I'm unsure if this needs to be fixed in the ai_agents or here.</p> <p>But if the request is unsolvable, should it provide a generic response without any further tools attached or is there a use case for it to keep calling other tools past its loop limit?</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> > Related issue: [Issue #3547457](https://www.drupal.org/node/3547457)
issue