Add possibility to add structured output for Chat action or create a separate action.
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3544537. --> Reported by: [marcus_johansson](https://www.drupal.org/user/385947) Related to !4 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>Structured Output is something that has gone a little bit in the shadows due to Function Calling being added in 1.1.0, but its actually quite powerful as well and a very good fit with ECA.</p> <p>I have added an issue here <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/ai/-/work_items/3544533" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/ai/-/work_items/3544533</a></span>, because I realized its not even documented, but what you can do with providers that support it is, adding something in the line of</p> <pre>$schema = [<br>&nbsp; 'name' =&gt; 'countries_population',<br>&nbsp; 'schema' =&gt; [<br>&nbsp;&nbsp;&nbsp; 'type' =&gt; 'object',<br>&nbsp;&nbsp;&nbsp; 'properties' =&gt; [<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'countries' =&gt; [<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'type' =&gt; 'array',<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'minItems' =&gt; 1,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'items' =&gt; [<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'type' =&gt; 'object',<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'properties' =&gt; [<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'name' =&gt; ['type' =&gt; 'string', 'minLength' =&gt; 1],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'iso_code' =&gt; ['type' =&gt; 'string', 'pattern' =&gt; '^[A-Z]{2,3}$'],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'population' =&gt; ['type' =&gt; 'integer', 'minimum' =&gt; 0],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'year' =&gt; ['type' =&gt; 'integer', 'minimum' =&gt; 1800, 'maximum' =&gt; 2100],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'required' =&gt; ['name', 'population'],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'additionalProperties' =&gt; false,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<br>&nbsp;&nbsp;&nbsp; ],<br>&nbsp;&nbsp;&nbsp; 'required' =&gt; ['countries'],<br>&nbsp;&nbsp;&nbsp; 'additionalProperties' =&gt; false,<br>&nbsp; ],<br>];<br>$messages = [new ChatMessage("List the population of Germany, France and Spain")];<br>$input = new ChatInput("User", $messages);<br>$input-&gt;setChatStructuredJsonSchema($schema);</pre><p>and it would return</p> <pre>{<br>&nbsp; "countries": [<br>&nbsp;&nbsp;&nbsp; { "name": "France",&nbsp; "iso_code": "FR", "population": 68600000, "year": 2025 },<br>&nbsp;&nbsp;&nbsp; { "name": "Germany", "iso_code": "DE", "population": 83600000, "year": 2025 },<br>&nbsp;&nbsp;&nbsp; { "name": "Spain",&nbsp;&nbsp; "iso_code": "ES", "population": 49077984, "year": 2025 }<br>&nbsp; ]<br>}</pre><p>Because ECA has the "Render: unserialize" action, it should be able to split up the result and do really complex things with this.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Either add a new action specifically for structured output chat or add it as an option to the chat action.</p>
issue