generateObject method

  1. @override
Future<Map<String, Object?>> generateObject({
  1. required String instructions,
  2. required List<AiMessage> messages,
  3. required AiSchema schema,
  4. String locale = 'en',
  5. 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);
}