Move the input of structured output to an object.
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3545462. -->
Reported by: [marcus_johansson](https://www.drupal.org/user/385947)
Related to !1114 !873 !1115
>>>
<p>--- AI TRACKER METADATA ---<br>
Update Summary: Changes to make it easier to input structured output.<br>
Check-in Date: MM/DD/YYYY (US format) [When we should see progress/get an update]<br>
Due Date: MM/DD/YYYY (US format) [When the issue should be fully completed]<br>
Blocked by: [#XXXXXX] (New issues on new lines)<br>
Additional Collaborators: @username1, @username2<br>
AI Tracker found here: <a href="https://www.drupalstarforge.ai/">https://www.drupalstarforge.ai/</a><br>
--- END METADATA ---</p>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>The current solution for adding a structured output, is based on an OpenAI array and needs to be manipulated by any processor that might need another structure. OpenAI for instance has name, description on top of a json schema.</p>
<p>These two things are important for guidance, but are in theory optional. They also have no usage for certain other providers.</p>
<p>If we instead create a DTO or a class that can take the name, description, strict and json schema as methods, this will allow for a more structured input on top of keeping the array as optional. Similar to how the input works for the actual provider input, where it takes raw or object. We could then add validation for the json schema later.</p>
<p>So the method for now would be something like the following.</p>
<pre> public function setChatStructuredJsonSchema(array|StructuredOutputSchema $schema): void {<br> if ($schema instranceof StructuredOutputSchema) {<br> $schema = $schema->toArray();<br> }<br> $this->setDebugDataValue('chat_structured_json_schema', $schema);<br> $this->chatStructuredJsonSchema = $schema;<br> }</pre><p>Then for 2.0.x, we can remove free array and also look into how you can add any class, similar to how Symfony is doing it, see: <a href="https://github.com/symfony/ai/blob/a40908dfdc2a0bf96e1a84b6d6437c6a2d6a2019/fixtures/StructuredOutput/User.php#L12">https://github.com/symfony/ai/blob/a40908dfdc2a0bf96e1a84b6d6437c6a2d6a2019/fixtures/StructuredOutput/User.php#L12</a></p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<ul>
<li>Create an interface called StructuredOutputSchemaInterface, that has name, description, strict and json schema.</li>
<li>Create an class for it, for now just getters and setters and toArray (and maybe fromArray)</li>
<li>For now, just make an array when its set in the ChatInput.</li>
<li>If the json_schema or schema is missing, it should throw an error.</li>
<li>By default it should be setting the schema key since that is the most common way of doing structured calling.</li>
<li>If the name is missing, it should set something generic like "json_schema".</li>
<li>Strict should be set to false.</li>
<li>If strict is set in the object, we should set chatStrictSchema with it, for BC.</li>
<li>Add a deprecation notice that setChatStrictSchema and getChatStrictSchema will be removed for 2.0 and should be set on the setChatStructuredJsonSchema input instead.</li>
</ul>
> Related issue: [Issue #3540885](https://www.drupal.org/node/3540885)
issue