Schema
Defining structured I/O
AXAR uses structured input and output to provide a consistent and reliable way to work with agents and tools. It adopts a decorator-based approach that works seamlessly with TypeScript's strong typing. AXAR allows us to define object schema with decorators for metadata, validation, and property descriptions. Alternatively, AXAR also supports Zod, offering a flexible option for schema definitions alongside the decorator-based approach.
Where schemas are needed
Schemas are necessary in the following contexts:
Agent input and output
Schemas define the format of data that an agent receives and returns. For example, in the code below, the GreetingAgent takes input of type GreetingAgentRequest and produces output of type GreetingAgentResponse. We must define schema definitions for both.
@input(GreetingAgentRequest)
@output(GreetingAgentResponse)
export class GreetingAgent extends Agent<
GreetingAgentRequest,
GreetingAgentResponse
> {}Tool parameters
Schemas describe the format of objects passed as parameters to tools. In the example below, the tool getWeatherInfo() accepts a parameter of type WeatherParams. A schema definition is required for this type.
@tool('Get weather info')
getWeatherInfo(weather: WeatherParams): string {
return `The weather is rainy today in ${weather.location}.`;
}Defining a schema
Schemas are defined using TypeScript classes annotated with @schema(). Each property of that class must have a @property() decorator that describes its purpose.
Required properties: Use
!after the property name.Optional properties: Use
?and include the@optional()decorator.
Example: Complete schema definition
Special annotations for enums and arrays
Defining array items
The @arrayItems() decorator specifies the schema for array elements.
Defining enum properties
The @enumValues() decorator defines valid values for an enum property.
Validation decorators
AXAR includes a set of validation decorators to apply constraints on schema properties:
Range:
@min(value),@max(value)Optional:
@optional()Uniqueness:
@uniqueItems()(for arrays)Pattern matching:
@pattern(regex)Common formats:
@email(),@url(),@uuid(),@datetime(), etc.
Using Zod with AXAR
Zod is a popular TypeScript library for runtime type validation and schema definitions. AXAR supports using Zod schemas alongside its decorator-based schema definition approach.
Defining Agent Input and Output with Zod
We can define agent input and output schemas using Zod. Here's an example:
The @input and @output annotations accept both @schema-based definitions and ZodSchema.
Using Zod with tools
Zod can also define the schema for tool parameters. Here's an example:
When using ZodSchemas with tools, we need to specify the schema as the second argument in the @tool decorator.
Last updated