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> 'name' => 'countries_population',<br> 'schema' => [<br> 'type' => 'object',<br> 'properties' => [<br> 'countries' => [<br> 'type' => 'array',<br> 'minItems' => 1,<br> 'items' => [<br> 'type' => 'object',<br> 'properties' => [<br> 'name' => ['type' => 'string', 'minLength' => 1],<br> 'iso_code' => ['type' => 'string', 'pattern' => '^[A-Z]{2,3}$'],<br> 'population' => ['type' => 'integer', 'minimum' => 0],<br> 'year' => ['type' => 'integer', 'minimum' => 1800, 'maximum' => 2100],<br> ],<br> 'required' => ['name', 'population'],<br> 'additionalProperties' => false,<br> ],<br> ],<br> ],<br> 'required' => ['countries'],<br> 'additionalProperties' => false,<br> ],<br>];<br>$messages = [new ChatMessage("List the population of Germany, France and Spain")];<br>$input = new ChatInput("User", $messages);<br>$input->setChatStructuredJsonSchema($schema);</pre><p>and it would return</p>
<pre>{<br> "countries": [<br> { "name": "France", "iso_code": "FR", "population": 68600000, "year": 2025 },<br> { "name": "Germany", "iso_code": "DE", "population": 83600000, "year": 2025 },<br> { "name": "Spain", "iso_code": "ES", "population": 49077984, "year": 2025 }<br> ]<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