Move Automators to Structured Output
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3577170. -->
Reported by: [marcus_johansson](https://www.drupal.org/user/385947)
Related to !1291
>>>
<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>Currently Automators is using pseudo json to create the output that is required when calling AI. This has historical reasons that are all explained here: <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/ai/issues/3565806" title="Status: Closed (fixed)">#3565806: [Discuss] Move Automators to Structured Output</a></span>. The main reason was that structured output only existed in OpenAI when the Automators was built and that it was faster inference with pseudo json. Both are not issues anymore.</p>
<p>Since 2.0.0 will be breaking, this also means that we can change behaviour, in this case for the better, but still changed behaviour.</p>
<p>As example for the office hours, this means that instead of appending this to the prompt:</p>
<pre>\n\n\n\nDo not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.\n[{"value": {"day": "1 for monday, 2 for tuesday and so on", "starthours": "opening hour in hi format, so 16:00 would be 1600", "endhours": "closing hour in hi format, so 20:00 would be 2000"}].\n\nOnly give back the days they are open.</pre><p>We should add this in the structured output:</p>
<pre>{<br> "schema": {<br> "type": "array",<br> "title": "OpeningHours",<br> "items": {<br> "type": "object",<br> "additionalProperties": false,<br> "required": [<br> "value"<br> ],<br> "properties": {<br> "value": {<br> "type": "object",<br> "additionalProperties": false,<br> "required": [<br> "day",<br> "starthours",<br> "endhours"<br> ],<br> "properties": {<br> "day": {<br> "type": "string",<br> "title": "Day",<br> "pattern": "^[1-7]$"<br> },<br> "starthours": {<br> "type": "string",<br> "title": "Start Hours",<br> "pattern": "^([01][0-9]|2[0-3])[0-5][0-9]$"<br> },<br> "endhours": {<br> "type": "string",<br> "title": "End Hours",<br> "pattern": "^([01][0-9]|2[0-3])[0-5][0-9]$"<br> }<br> }<br> }<br> }<br> }<br> },<br> "name": "opening_hours",<br> "strict": true<br>}</pre><p>With possible an extra append for one shot learning reasons.</p>
<p>We should before we start, check if we can derive this from the field type, meaning we have in the base class something that produces json correctly, and only in exceptions will this be needed to be overriden.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<ul>
<li>Check if we can produce json schema for each field in a consistent manner. Assume that Drupal 11+ will be used.</li>
<li>Add a method in the base class that produces the schema if so, otherwise add it abstract.</li>
<li>This method should produce an json schema array.</li>
<li>This should be added into base class for inference.</li>
<li>Write tests that have a correct result tested against each json schema.</li>
<li>This is needed for all Automator plugins in the AI module.</li>
</ul>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<h3>Optional: Other details as applicable (e.g., User interface changes, API changes, Data model changes)</h3>
<h3 id="summary-ai-usage">AI usage (if applicable)</h3>
<p>[ ] AI Assisted Issue<br>
This issue was generated with AI assistance, but was reviewed and refined by the creator.</p>
<p>[ ] AI Assisted Code<br>
This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.</p>
<p>[ ] AI Generated Code<br>
This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p>
<p>[ ] Vibe Coded<br>
This code was generated by an AI and has only been functionally tested.</p>
> Related issue: [Issue #3565806](https://www.drupal.org/node/3565806)
issue