createLyriaModel function

Model createLyriaModel(
  1. CommonGoogleGenPlugin plugin,
  2. String modelName, {
  3. ModelInfo? info,
  4. List<String>? defaultModalities = const ['AUDIO', 'TEXT'],
})

Creates the Lyria model modelName for plugin, with info (default: the googleAI catalog, lyriaModelInfo).

defaultModalities are asked for when the request sets no responseModalities. googleAI asks for audio and text; vertexAI passes null and sends nothing, because the platform rejects an audio entry without a bit_rate with a 400 ("bit_rate must be positive", seen live by the JS plugin, Oct 2026) and returns audio and text by default anyway.

Implementation

Model createLyriaModel(
  CommonGoogleGenPlugin plugin,
  String modelName, {
  ModelInfo? info,
  List<String>? defaultModalities = const ['AUDIO', 'TEXT'],
}) {
  return Model(
    name: '${plugin.name}/$modelName',
    customOptions: LyriaOptions.$schema,
    metadata: {'model': (info ?? lyriaModelInfo(modelName)).toJson()},
    fn: (req, ctx) async {
      final options = LyriaOptions.$schema.parse({...?req.config});
      if (options.location != null && !plugin.supportsVertexOptions) {
        warnDroppedConfigField(modelName, 'location');
      }
      final request = CreateInteractionRequest(
        model: modelName,
        input: toInteractionSteps(ensureToolIds(req.messages)),
        responseFormat: toInteractionResponseFormat(
          modalities: options.responseModalities ?? defaultModalities,
        ),
        // Not stored unless asked for, as on the Gemini Interactions models
        // (the server stores by default); the request's value wins.
        store: options.store ?? plugin.store ?? false,
      );

      // Applies the request's apiKey (googleAI) or location (vertexAI).
      final service = await plugin.getApiClientForRequest(req.config);
      // Aborts the in-flight request on a plugin-owned client (see
      // generateWithInteractions).
      final disposeCancel = ctx.cancel?.onCancel(service.client.close);
      try {
        ctx.cancel?.throwIfCancelled();
        final interaction = await service.createInteraction(request);
        ctx.cancel?.throwIfCancelled();
        return fromInteractionSync(interaction);
      } catch (e, stack) {
        throw plugin.handleException(e, stack);
      } finally {
        disposeCancel?.call();
        service.client.close();
      }
    },
  );
}