Schema Types¶
Define your expected output structure using strutex's schema types.
Basic Types¶
String¶
from strutex import String
# Standard
name = String(description="Customer name")
# Simplified (if no args needed)
middle_name = String # equivalent to String()
Number¶
For floating-point values:
Integer¶
For whole numbers:
Boolean¶
Specialized Types¶
Enum¶
Restrict output to a specific set of strings.
from strutex import Enum
category = Enum(
values=["Food", "Transport", "Lodging"],
description="Expense category"
)
Date¶
Extracts a string formatted as YYYY-MM-DD.
DateTime¶
Extracts a string formatted as ISO 8601 (YYYY-MM-DDTHH:MM:SS).
Complex Types¶
Array¶
from strutex import Array, String, Object
# Array of strings
tags = Array(items=String(), description="Item tags")
# Array of objects
items = Array(
items=Object(
properties={
"name": String(),
"price": Number()
}
)
)
Object¶
from strutex import Object, String
address = Object(
description="Shipping address",
properties={
"street": String(),
"city": String(),
"zip": String()
}
)
Required vs Optional¶
By default, all properties are required. To make fields optional:
Complete Example¶
from strutex import Object, String, Number, Integer, Array, Boolean
invoice_schema = Object(
description="Complete invoice",
properties={
"invoice_number": String(description="Unique ID"),
"date": String(description="YYYY-MM-DD"),
"vendor": Object(
properties={
"name": String(),
"address": String(nullable=True)
}
),
"items": Array(
items=Object(
properties={
"description": String(),
"quantity": Integer(),
"price": Number()
}
)
),
"total": Number(),
}
)
JSON Schema Serialization¶
You can convert any schema object into a standard JSON Schema dictionary, useful for debugging or integrating with other tools.
from strutex import String, Object
import json
schema = Object(
properties={"name": String(description="User name")},
required=["name"]
)
# Convert to dict
print(schema.to_dict())
# {
# "type": "object",
# "properties": {"name": {"type": "string", "description": "User name"}},
# "required": ["name"],
# "additionalProperties": False
# }
# Convert to JSON string
print(str(schema))