ContentEntitySeeder crashes on double-wrapped entity_array when ComplexToolItems + multiple=TRUE context definition produces nested array schema
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3578197. -->
Reported by: [gxleano](https://www.drupal.org/user/3591999)
Related to !244
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>When a context definition in <code>ContentEntitySeeder</code> uses both the <code>ComplexToolItems</code> constraint and <code>multiple: TRUE</code>, the <code>ContextDefinitionNormalizer</code> produces a double-wrapped JSON Schema</p>
<p>This occurs because:</p>
<ol>
<li>The <strong>ComplexToolItems handler</strong> sets the property to <code>type: array</code> with <code>items</code> being the normalized complex tool schema.</li>
<li>The <strong>isMultiple() handler</strong> then wraps that already-array property in another array type.</li>
</ol>
<p>The resulting schema instructs the LLM to produce nested arrays. As a result, <code>entity_array</code> arrives as:</p>
<pre>[[{field_name, field_values}]]</pre><p>instead of:</p>
<pre>[{field_name, field_values}]</pre><p>The method <code>ContentEntitySeeder::processToolCalls()</code> then crashes with <strong>undefined index errors</strong> (<code>field_name</code>, <code>field_values</code>) because it iterates the outer array and encounters an inner array instead of the expected associative objects.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<ol>
<li>Configure an AI Agent that uses the <code>ContentEntitySeeder</code> function call.</li>
<li>Trigger the agent to create a content entity (such as a node with fields).</li>
<li>The LLM returns <code>entity_array</code> wrapped in an extra array level due to the double-wrapped schema</li>
<li><code>ContentEntitySeeder::processToolCalls()</code> crashes with PHP errors because <code>$lists['field_name']</code> is undefined — <code>$lists</code> is an array of objects rather than an associative array containing <code>field_name</code>.
</li>
</ol>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<ul>
<li>Add a condition in <code>ContextDefinitionNormalizer</code> to skip the <code>isMultiple()</code> wrapping when the property is already <code>type: array</code> from <code>ComplexToolItems</code>.</li>
<li>Add defensive checks in <code>ContentEntitySeeder::processToolCalls()</code> for malformed LLM responses.</li>
<li>Add test coverage for <code>ComplexToolItems + multiple: TRUE</code> context definitions to verify that the generated schema remains flat.</li>
<li>Add test coverage ensuring <code>ContentEntitySeeder</code> handles malformed or double-wrapped LLM responses gracefully.</li>
</ul>
issue