Can't properly produce structured response with array #217
-
Hello, Just checking to see if others have experience or fix this issue. I need my agent to respond with an structured output likeso: // ParentData.php
use NeuronAI\StructuredOutput\Validation\Rules\ArrayOf;
final class ParentData
{
/**
* @var ItemData[]
*/
#[ArrayOf(ItemData::class)]
public array $items;
} // ItemData.php
use NeuronAI\StructuredOutput\SchemaProperty;
final class ItemData
{
#[SchemaProperty(description: 'The description of the item.', required: true)]
public string $description;
#[SchemaProperty(description: 'The priority of the item.', required: true)]
public int $priority;
} I'm met with this error:
Already tried variations like using FQCNs or using Tried stronger models like Gemini 2.5 Pro and Claude 4 Sonnet but still throws the same exception. Don't get me wrong, but I did not experience a problem like this with Prism using the same structure and even weaker models like Gemini 2.0 Flash. Am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
Hi @acgonzales two things can help:
|
Beta Was this translation helpful? Give feedback.
-
I was getting the same problem, I needed to use a fully-qualified namespace in my docblock for it to work! Before: /**
* @var DocumentSplitChunk[]
*/ After: /**
* @var \App\Splitters\DocumentSplitChunk[]
*/ |
Beta Was this translation helpful? Give feedback.
-
@ilvalerione I still encounter this problem. I'm running version 2.2 and using gpt-5 with structured output support through Azure OpenAI. Error:
I have defined a schema: class Schema
{
#[SchemaProperty(description: 'Sales staff code', required: true)]
#[NotBlank]
public string $staff_code;
#[SchemaProperty(description: 'Array of generated sales insights for the sales staff member', required: true)]
/**
* @var \App\Services\AI\Schemas\UserSalesAgent\Insight[]
*/
#[ArrayOf(Insight::class)]
public array $insights;
} And the Insights: class Insight
{
#[SchemaProperty(description: 'The category of the insight (e.g. declining_client, upsell_opportunity, quote_followup)', required: true)]
public string $type;
#[SchemaProperty(description: 'Description of the insight. Format the description to be clear and concise. Give it some prose in a proffesional tone. Avoid abbrevations.', required: true)]
public string $summary;
#[SchemaProperty(description: 'Clear recommended next step for the staff member. Format the action to be clear and concise. Give it some prose in a proffesional tone. Avoid abbrevations.', required: true)]
public string $action;
#[SchemaProperty(description: 'Optional contextual references for drill-down in the widget (client code, quote number, shipment number)', required: false)]
/**
* @var \App\Services\AI\Schemas\UserSalesAgent\ContextReference[]
*/
#[ArrayOf(ContextReference::class)]
public array $context_references;
} My agent is using the schema: protected function provider(): AIProviderInterface
{
return new AzureOpenAI(
key: config('ai.azure_openai.key'),
endpoint: config('ai.azure_openai.endpoint'),
model: config('ai.azure_openai.model'),
version: config('ai.azure_openai.version')
);
}
protected function getOutputClass(): string
{
return Schema::class;
} Looking at this through Inspector, everything seems to be outputted correctly by the model: validate schema returns the right format (each sub item within insights is also structured correctly and the JSON is able to be parsed:
|
Beta Was this translation helpful? Give feedback.
-
Weirdly enough, it works if I remove the nested Commenting this out, and it generates without an issue. /**
* @var \App\Services\AI\Schemas\UserSalesAgent\ContextReference[]
*/
/*
#[SchemaProperty(description: 'Optional contextual references for drill-down in the widget (client code, quote number, shipment number)', required: false)]
#[ArrayOf(ContextReference::class)]
public array $context_references;*/ Can't we provide nested |
Beta Was this translation helpful? Give feedback.
@acgonzales The latest 1.15.0 also support nested output structures for Gemini. Thank you for reporting it here.