LogicException in Speech-To-Text Generation Explorer
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3564433. -->
Reported by: [martin-helmich](https://www.drupal.org/user/3856068)
Related to !1069
>>>
<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>The "Speech-To-Text Generation Explorer" (part of the AI API Explorer) raises a LogicException when using a STT model for text generation. The error message only says, "Form errors cannot be set after form validation has finished" (see preliminary analysis regarding the cause below).</p>
<p>I came across this issue while implementing my own AI provider (<a href="https://www.drupal.org/project/ai_provider_mittwald">ai_provider_mittwald</a>) when I wanted to test the STT integration.</p>
<p>The issue seems to stem from inconsistent usage of the form prefix in the handler code (detailed analysis below). In this matter, this issue is possibly related to <a href="https://www.drupal.org/project/ai/issues/3553353">#3553353</a> (which concerns the Translate Text Explorer).</p>
<h4 id="summary-steps-reproduce">Steps to reproduce (required for bugs, but not feature requests)</h4>
<p>Please provide information like AI modules enabled, which AI provider, browser, etc.</p>
<ol>
<li>Configure any AI provider with STT capabilities (I stumbled upon this while working on <a href="https://www.drupal.org/project/ai_provider_mittwald/issues/3563882">ai_provider_mittwald#3563882</a>, but ai_provider_openai "works" just as well to reproduce; I highly suspect the issue is independent of any specific provider)</li>
<li>Open the "Speech-To-Text Generation Explorer" in the "AI API Explorer"</li>
<li>Select an audio file, choose provider and model, click "Generate a text"</li>
</ol>
<p>Enabled modules:</p>
<pre>$ ddev drush pml --filter=AI<br> ------------------- -------------------------------------------------------------- ---------- -------------<br> Package Name Status Version<br> ------------------- -------------------------------------------------------------- ---------- -------------<br> AI AI Core (ai) Enabled 1.2.4<br> AI Providers DropAI Provider (dropai_provider) Disabled 1.2.4<br> AI AI API Explorer (ai_api_explorer) Enabled 1.2.4<br> AI Tools AI Assistant API (ai_assistant_api) Enabled 1.2.4<br> AI AI Automators (ai_automators) Disabled 1.2.4<br> AI Tools AI Chatbot (ai_chatbot) Enabled 1.2.4<br> AI AI CKEditor integration (ai_ckeditor) Enabled 1.2.4<br> AI AI Content Suggestions (ai_content_suggestions) Enabled 1.2.4<br> AI AI ECA integration (ai_eca) Disabled 1.2.4<br> AI AI External Moderation (Deprecated) (ai_external_moderation) Disabled 1.2.4<br> AI AI Logging (ai_logging) Disabled 1.2.4<br> AI AI Observability (ai_observability) Disabled 1.2.4<br> AI (Experimental) AI Search (ai_search) Disabled 1.2.4<br> AI AI Translate (ai_translate) Enabled 1.2.4<br> AI AI Validations (ai_validations) Disabled 1.2.4<br> AI Tools AI Agents (ai_agents) Enabled 1.2.1<br> AI Tools AI Agents Explorer (ai_agents_explorer) Enabled 1.2.1<br> AI Tools AI Agents Extra (ai_agents_extra) Disabled 1.2.1<br> AI Tools AI Agents Extra Tools (ai_agents_extra_tools) Disabled 1.2.1<br> AI Tools AI Agents Form Integration (ai_agents_form_integration) Disabled 1.2.1<br> AI Tools AI Image Alt Text (ai_image_alt_text) Enabled 1.0.2<br> AI Tools AI Image Bulk Alt Text (ai_image_bulk_alt_text) Disabled 1.0.2<br> AI Providers Anthropic Provider (ai_provider_anthropic) Disabled 1.2.1<br> AI Providers mittwald Provider (ai_provider_mittwald) Enabled<br> AI Providers OpenAI Provider (ai_provider_openai) Enabled 1.2.0</pre><h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>This is not a proposed solution, but rather a preliminary analysis as to why this error occurs. I'm happy to work on a fix for this, but might need some pointers as to where to start.</p>
<p>The underlying error is a form error pertaining to the <code>ai_model</code> field (verbatim error is, "Please select a model to continue. The AI provider may not be properly configured."). Find the <a href="https://www.drupal.org#details-stack-trace">full stack trace</a> and a <a href="https://www.drupal.org#details-form-state">dump of the submitted form state</a> below.</p>
<p>From the looks of it, it seems like the form field name prefix is handled inconsistently throughout this specific API explorer. The submitted form elements have a <code>stt_</code> prefix, whereas much of the form handler code seems to assume <code>speech_to_text_</code> as form prefix.</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<p>n/a</p>
<h3>Optional: Other details as applicable (e.g., User interface changes, API changes, Data model changes)</h3>
<h4 id="details-form-state">Full form state</h4>
<pre>{<br> "file":{<br> "\u0000Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0000originalName": "example.mp3",<br> "\u0000Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0000mimeType": "audio\/mpeg",<br> "\u0000Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0000error": 0,<br> "\u0000Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0000originalPath":"example.mp3", <br> "\u0000Symfony\\Component\\HttpFoundation\\File\\UploadedFile\u0000test": false<br> },<br> "stt_ai_provider": "openai",<br> "stt_ai_model": "whisper-1",<br> "stt_ajax_prefix_configuration_language": "de",<br> "stt_ajax_prefix_configuration_prompt": "",<br> "stt_ajax_prefix_configuration_response_format": "json",<br> "stt_ajax_prefix_configuration_temperature": "0",<br> "submit": "Generate a Text",<br> "form_build_id": "...",<br> "form_token": "...",<br> "form_id": "ai_api_explorer_form",<br> "op": "Generate a Text"<br>} </pre><h4 id="details-stack-trace">Full stack trace of the form error</h4>
<pre>#0 /var/www/html/web/modules/contrib/ai/src/Service/AiProviderFormHelper.php(290): Drupal\Core\Form\FormState->setErrorByName() <br>#1 /var/www/html/web/modules/contrib/ai/src/Service/AiProviderFormHelper.php(256): Drupal\ai\Service\AiProviderFormHelper->generateAiProvidersConfigurationFromForm() <br>#2 /var/www/html/web/modules/contrib/ai/modules/ai_api_explorer/src/Plugin/AiApiExplorer/SpeechToTextGenerator.php(79): Drupal\ai\Service\AiProviderFormHelper->generateAiProviderFromFormSubmit() <br>#3 /var/www/html/web/modules/contrib/ai/modules/ai_api_explorer/src/Form/AiApiExplorerForm.php(82): Drupal\ai_api_explorer\Plugin\AiApiExplorer\SpeechToTextGenerator->getResponse() <br>#4 [internal function]: Drupal\ai_api_explorer\Form\AiApiExplorerForm->ajaxResponse() <br>#5 /var/www/html/web/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php(82): call_user_func_array() <br>#6 /var/www/html/web/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php(112): Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse() <br>#7 /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php(246): Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException() <br>#8 /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php(206): Symfony\Component\EventDispatcher\EventDispatcher::{closure:Symfony\Component\EventDispatcher\EventDispatcher::optimizeListeners():241}() <br>#9 /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php(56): Symfony\Component\EventDispatcher\EventDispatcher->callListeners() <br>#10 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(241): Symfony\Component\EventDispatcher\EventDispatcher->dispatch() <br>#11 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(91): Symfony\Component\HttpKernel\HttpKernel->handleThrowable() <br>#12 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle() <br>#13 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle() <br>#14 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle() <br>#15 /var/www/html/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle() <br>#16 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(118): Drupal\big_pipe\StackMiddleware\ContentLength->handle() <br>#17 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(92): Drupal\page_cache\StackMiddleware\PageCache->pass() <br>#18 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle() <br>#19 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() <br>#20 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(53): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() <br>#21 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(54): Drupal\Core\StackMiddleware\AjaxPageState->handle() <br>#22 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(745): Drupal\Core\StackMiddleware\StackedHttpKernel->handle() <br>#23 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle() <br>#24 {main}</pre>
> Related issue: [Issue #3553353](https://www.drupal.org/node/3553353)
issue