generateObject method
Future<Map<String, Object?> >
generateObject({
- required String instructions,
- required List<
AiMessage> messages, - required AiSchema schema,
- String locale = 'en',
- int maxTokens = 512,
override
Asks for one JSON object shaped like schema, whose root must be
AiSchema.object (otherwise throws ArgumentError).
instructions are trusted prompt text; messages are the conversation,
oldest first, and must not be empty.
Implementation
@override
Future<Map<String, Object?>> generateObject({
required String instructions,
required List<AiMessage> messages,
required AiSchema schema,
String locale = 'en',
int maxTokens = 512,
}) async {
if (schema is! AiObjectSchema) {
throw ArgumentError.value(
schema,
'schema',
'The root must be an object.',
);
}
_checkMessages(messages);
// One retry, as on Android: models occasionally break the shape.
for (var attempt = 0; attempt < 2; attempt++) {
final value = await _requestObject(
instructions: instructions,
messages: messages,
schema: schema,
maxTokens: maxTokens,
);
if (value is Map && schema.matches(value)) {
return Map<String, Object?>.from(value);
}
}
throw const SystemLlmException(SystemLlmException.invalidResponse);
}