Index of every page on this site: llms.txt
All skills
genkit-ai avatar

/developing-genkit-dart

@d12c988
by Genkitgenkit-ai/skills40 stars
13
Run it yourself

Provides guidance on developing AI-powered applications using the Genkit Dart SDK. Use when the user asks to build AI agents in Dart, use Genkit flows, or integrate LLMs into Dart/Flutter applications. Don't use for Genkit in JavaScript/TypeScript, Go, or Python.

Use this Skill: https://skilld.dev/gh/genkit-ai/skills/developing-genkit-dart

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesschemantic.md

≈1.3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Schemantic

Schemantic is a general-purpose Dart library used for defining strongly typed data classes that automatically bind to reusable runtime JSON schemas. It is standard for the genkit-dart framework but works independently as well.


Core Concepts

Always use schemantic when strongly typed JSON parsing or programmatic schema validation is required.

  • Annotate your abstract classes with @Schema().
  • Use the $ prefix for abstract schema class names (e.g., abstract class $User).
  • Always run dart run build_runner build to generate the .g.dart schema files.

Installation

Add dependencies. The code generator lives in a separate schemantic_builder package, so you must add it as a dev dependency too:

dart pub add schemantic
dart pub add dev:schemantic_builder
dart pub add dev:build_runner

Gotcha: if schemantic_builder is missing, dart run build_runner build completes "successfully" but reports wrote 0 outputs and generates no .g.dart files, with no error explaining why. If you see zero outputs, confirm schemantic_builder is in your dev_dependencies.

Note: plain dart run build_runner build is correct. Recent build_runner versions removed --delete-conflicting-outputs (it is now ignored and warns "These options have been removed and were ignored"), so do not pass it.

Basic Usage

  1. Defining a schema:
import 'package:schemantic/schemantic.dart';

part 'my_file.g.dart'; // Must match the filename

@Schema()
abstract class $MyObj {
  String get name;
  $MySubObj get subObj;
}

@Schema()
abstract class $MySubObj {
  String get foo;
}
  1. Using the Generated Class:

The builder creates a concrete class MyObj (no $) with a factory constructor (MyObj.fromJson) and a regular constructor.

// Creating an instance
final obj = MyObj(name: 'test', subObj: MySubObj(foo: 'bar'));

// Serializing to JSON
print(obj.toJson()); 

// Parsing from JSON
final parsed = MyObj.fromJson({'name': 'test', 'subObj': {'foo': 'bar'}});
  1. Accessing Schemas at Runtime:

The generated data classes have a static $schema field (of type SchemanticType<T>) which can be used to pass the definition into functions or to extract the raw JSON schema.

// The JSON Schema, as a plain map
final Map<String, Object?> jsonSchema = MyObj.$schema.jsonSchema();
print(jsonEncode(jsonSchema)); // import 'dart:convert';

// Validate arbitrary JSON at runtime (empty list = valid)
final errors = await MyObj.$schema.validate({'invalid': 'data'});
for (final e in errors) {
  print(e);
}

// Parse JSON into the typed class
final MyObj obj = MyObj.$schema.parse({'name': 'x', 'subObj': {'foo': 'y'}});

Primitive Schemas

When a full data class is not required, Schemantic provides functions to create schemas dynamically.

final ageSchema = SchemanticType.integer(description: 'Age in years', minimum: 0);
final nameSchema = SchemanticType.string(minLength: 2);
final nothingSchema = SchemanticType.voidSchema();
final anySchema = SchemanticType.dynamicSchema();

final userSchema = SchemanticType.map(.string(), .integer()); // Map<String, int>
final tagsSchema = SchemanticType.list(.string()); // List<String>

Union Types (AnyOf)

To allow a field to accept multiple types, use @AnyOf.

@Schema()
abstract class $Poly {
  @AnyOf([int, String, $MyObj])
  Object? get id;
}

Schemantic generates a specific helper class (e.g., PolyId) to handle the values:

final poly1 = Poly(id: PolyId.int(123));
final poly2 = Poly(id: PolyId.string('abc'));

Field Annotations

You can use specialized annotations for more validation boundaries:

@Schema()
abstract class $User {
  @IntegerField(
    name: 'years_old', // Change JSON key
    description: 'Age of the user',
    minimum: 0,
    defaultValue: 18,
  )
  int? get age;

  @StringField(
    minLength: 2,
    enumValues: ['user', 'admin'], 
  )
  String get role;
}

Recursive Schemas

For recursive structures (like trees), must use useRefs: true inside the generated jsonSchema property. You define it normally:

@Schema()
abstract class $Node {
  String get id;
  List<$Node>? get children;
}

Note: Node.$schema.jsonSchema(useRefs: true) generates schemas with JSON Schema $ref.

Non-nullable getters throw on partial data

Generated getters for required fields are non-nullable casts (e.g. _json['estimatedCostUsd'] as num). A JSON blob that is missing such a field throws on access (Null is not a subtype of num) rather than returning a default — this commonly bites computed/optional values that were never written, e.g. when reloading a partially populated state blob. Make computed or optional fields nullable (num?) or give them a defaultValue so partial data can still be read.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 alert2d4 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    The Genkit Dart skill provides comprehensive documentation and examples for building AI agents and flows using the Genkit framework. It includes guidance on using A2UI for interactive interfaces, managing agent sessions, and implementing middleware for filesystem access and multi-agent orchestration. While the skill includes instructions for installing the Genkit CLI from a vendor-controlled domain, its primary security considerations involve the attack surface for indirect prompt injection created by agents that ingest external data while possessing capabilities such as file modification and command execution.

  • Socket2d

    No alerts

  • Snyk3d

    Risk: CRITICAL · 2 issues

  • Runlayer8mo

    2/11 files flagged

Signed by skilld at d12c988. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 5 days ago
metadata
{
  "version": "1.0.0",
  "category": "AiAndMachineLearning"
}

README badge

README badge for genkit-ai/skills/developing-genkit-dart