DataModel system in LarAgent provides a robust foundation for handling structured data.
It ensures strict typing, automatic validation, serialization, and OpenAPI schema generation - flexible enough for simple DTOs while powerful enough for complex, nested, and polymorphic data structures.
Used in storage, tool arguments and structured output with LLMs, the DataModel system is a core part of LarAgent’s architecture.
Core Features
All data models extend theLarAgent\Core\Abstractions\DataModel abstract class, which provides:
Automatic Hydration
Populate objects from arrays using
fill() or fromArray()Schema Generation
Automatically generate OpenAPI/JSON Schemas using PHP types and attributes
Serialization
Convert objects back to arrays or JSON
Performance
Uses static runtime caching to minimize Reflection overhead
Basic Usage
When creating a DataModel for use with LLMs (structured output or tool arguments), use the#[Desc] attribute to provide context that helps the AI understand the purpose and expected format of each field.
- Without Property Promotion
- With Property Promotion
Even when you don’t need LLM integration, you can still use
DataModel instead of a plain DTO to benefit from the built-in fromArray() and toArray() methods.Polymorphic Arrays
To handle lists of different model types (e.g., a message containing both text and images), extendDataModelArray.
DataModelArray allows you to define a set of allowed models and a discriminator field to determine which model to instantiate for each item in the array.
It’s like a collection, but strictly typed to only allow specific DataModels based on a discriminator field.
Differentiation
Sometime one discriminator field is not enough to differentiate between multiple models. In such cases, you can override thematchesArray() method in DataModel.
For example, in a chat message array where both AssistantMessage and ToolCallMessage share the same role value of assistant, you can implement custom logic in matchesArray to check for the presence of specific fields:
ToolCallMessage & AssistantMessage, you can implement custom logic:
ToolCallMessage.php
AssistantMessage.php
Supported Property Types
DataModel supports various types for properties that are automatically mapped to Schema types, automatically validated, and serialized:Basic Types
string, int, float, boolNullable Types
?string, ?int, etc. - marks property as optional in schemaArrays
array - simple arrays without type hintsNested Models
Other
DataModel classes as propertiesEnums
PHP
Enum for constrained valuesData Model Arrays
DataModelArray for typed collectionsNested Data Models
DataModels can contain other DataModels as properties, enabling you to build complex, hierarchical data structures. Nested models are automatically hydrated when usingfromArray() and properly serialized with toArray().
Enums
PHP backed enums are fully supported and automatically converted to JSON Schemaenum constraints. This is useful when a property should only accept specific values.
DataModelArray as Property
ADataModelArray can be used as a property within a DataModel, enabling typed collections of polymorphic items.
When a
DataModelArray is used as a property, it behaves like any nested DataModel—automatically hydrated from arrays and serialized back to arrays.DTO-Style Data Models
For simple data transfer objects that don’t need schema generation (For example, when you use it only in storage), you can use a lightweight DTO approach:Performance Optimization
TheDataModel class uses Reflection to inspect properties and types. While LarAgent implements static runtime caching to mitigate the cost, Reflection is still slower than native code.
You are not required to override
fromArray() or toArray() methods. The base implementation works perfectly for 90% of use cases.When to Override
OverridefromArray() and toArray() only if:
- The model is instantiated frequently (thousands of times in a loop)
- The model is part of a core hot path (like
MessageContentin a streaming response) - You need custom transformation logic that standard casting doesn’t support

