Structured Output
Enforce the Agent output based on the provided schema.
PREREQUISITES
This guide assumes you are already familiar with the following concepts:
There are many use cases where we need Agents to understand natural language, but output in a structured format. One common use-case is extracting data from text to insert into a database or use with some other downstream system. This guide covers how Neuron allows you to enforce structured outputs from the agent.

How to use Structured Output
The central concept is that the output structure of LLM responses needs to be represented in some way. The schema that Neuron validates against is defined by PHP type hints. Basically you have to define a class with strictly typed properties:
Neuron generates the corresponding JSON schema from the PHP object to instruct the underlying model about your required data format. Then the agent parse the LLM output to extract data and returns an object instance filled with appropriate values:
Default output class
You can also encapsulate the output format into the Agent implementation, so it will be the Agent standard output format. You always need to call the structured() method to require strict output.
Control the output generation
Neuron requires you to define two layers of rules to create the structured output class.
The first is the SchemaProperty attribute that allows you to control the JSON schema sent to the LLM to understand the required data format.
The second layer is validation. Validation attributes will ensure data gathered from the LLM response are consistent with your requirements.

SchemaProperty
The SchemaProperty attribute allows you to define the JSON schema parameters of each property:
Nested Class
You can construct complex output structures using other PHP objects as a property type. Following the example of a the Person class we can add the address property typed as another structured class.
In the Address definition we require only the street and zip code properties, and allow city to be empty.
Now when you ask the agent for the structured output you will get the filled instance back:
Array
If you declare a property as an array, Neuron assumes the list of items to be a list of string. If you want the array to contains a list of other structured objects you can specify that using the anyOf argument:
And here is the hypotetical implementation of the Tag class with its own validation rules and property info:
Array with multiple types
As you can notice from the example above the anyOf argument is an array. Neuron also supports the composition of arrays with multiple object types. Just list the structured objects the array can contains and Neuron will include all of their specs in the JSON schema for the LLM.
Max Retries
Since the LLM are not perfectly deterministic it's mandatory to have a retry mechanism in place if something is missing in the LLM response.
By default Neuron extracts and validates the data from the LLM response and if there is one or more validation errors automatically retry the request just one more time informing the LLM about what went wrong and for what properties.
You can eventually customize the number of times the agent must retry to get a correct answer from the LLM:
If you work with a less capable LLM consider to use a number of retries balancing the probability to get e valid answer, and the potential token consumption.
You can disable retry just passing zero. It will be a one shot attempt:
Monitoring & Debugging
To watch inside this workflow you should connect your Agent to the Inspector monitoring dashboard in order to see the tool call execution flow in real-time.
After you sign up at the link above, make sure to set the INSPECTOR_INGESTION_KEY variable in the application environment file to start monitoring:

Each segment bring its own debug information to follow the agent execution in real time:

Learn how to enable observability in the next section.
Validation Rules
Since LLMs are not-deterministic systems and the consistency of their output can be highly influenced by the quality of the context they have as input, and they can still hallucinate, we provide a set of validation rules you can add to structured class properties to instruct Neuron verify the final set of data generated by the LLM.
Validation rules allows Neuron to resend the generation request to the LLM multiple times with a detailed report of what was wrong if one or more properties are not valid, until it reaches the maxRetries value.
If you don't define any validation rule, the data extracted from the LLM response will be filled into the structured output class directly.
#[NotBlank]
The property under validation cannot be blank. It accept the allowNull flag to treat explicitly null value as empty equivalent or not.
#[Length]
Determine if the length of a string match the given criteria:
#[WordsCount]
Determine if the number of words in a string match the given criteria:
#[Count]
Determine if the size of an array match the given criteria:
#[EqualTo] - #[NotEqualTo]
These rules have the same structure and meaning, and accept a single argument to define the value to compare against. The property under validation must be strictly equal (#[EqualTo]) or different (#[NotEqualTo]) than the reference value:
#[GreaterThan] - #[GreaterThanEqual]
These rules have the same structure and meaning, and accept a single argument to define the value to compare against. The property under validation must be strictly greater (#[GreaterThan]) or equal (#[GreaterThanEqual]) than the reference value:
#[LowerThan] - #[LowerThanEqual]
These rules have the same structure and meaning, and accept a single argument to define the value to compare against. The property under validation must be strictly lower (#[LowerThan]) or equal (#[LowerThanEqual]) than the reference value:
#[OutOfRange]
Determin if a number is out of the given range:
#[IsFalse] - #[IsTrue]
The property under validation must have exactly the boolean value defined by the rule:
#[IsNull] - #[IsNotNull]
The property under validation must respect the nullable condition defined by the rule:
#[Json]
The property under validation must contains a valid JSON string:
#[Url]
The property under validation must contains a valid URL:
#[Email]
The property under validation must contains a valid Email address:
#[IpAddress]
The property under validation must contains a valid IP address:
#[ArrayOf]
The property under validation must be an array that contains all of the given type of object. Notice that you also need to add the doc-block in order to make the agent able to instance the correct class. Use the full class namespace in the doc-block.
Last updated