Creating Genkit plugins
Genkit’s capabilities are designed to be extended by plugins. Genkit plugins are configurable modules that can provide models, retrievers, trace stores, and more.
Creating a Plugin
Section titled “Creating a Plugin”In Dart, a Genkit plugin is a class that extends GenkitPlugin.
1. Define the Plugin
Section titled “1. Define the Plugin”Create a class that extends GenkitPlugin. You usually define this in a separate package or within your app’s code.
import 'package:genkit/genkit.dart';
class MyPlugin extends GenkitPlugin { MyPlugin({this.apiKey});
final String? apiKey;
@override String get name => 'myplugin';
@override Future<List<ActionMetadata>> list() async { // Return a list of all actions this plugin provides. // This is used for discovery (e.g., in the Dev UI). return [ ActionMetadata( type: ActionType.model, name: 'myplugin/my-model', opt: MyModelOptions.$schema, // Your Schemantic schema ), ]; }
@override Action? resolve(String type, String name) { // Lazily create the action when requested. if (type == ActionType.model.name && name == 'myplugin/my-model') { return _createMyModel(); } return null; }
Action _createMyModel() { return Model( name: 'myplugin/my-model', fn: (request, context) async { // Implement your model logic here return ModelResponse( message: Message( role: Role.model, content: [TextPart('Hello from MyPlugin!')], ), ); }, ); }}2. Register the Plugin
Section titled “2. Register the Plugin”To use your plugin, instantiate it and pass it to the Genkit constructor.
final ai = Genkit( plugins: [ MyPlugin(apiKey: '...'), ],);3. Idiomatic usage
Section titled “3. Idiomatic usage”To make your plugin easy to discover and use within the Genkit ecosystem, consistent with other plugins like genkit_google_genai, it is recommended to create a plugin handle.
This pattern allows users to instantiate your plugin with a simple global function or constant, and provides a namespace for accessing your plugin’s actions (like myPlugin.myModel) in a type-safe way if you choose to implement it.
// Define a global constant for easy accessconst myPlugin = MyPluginHandle();
class MyPluginHandle { const MyPluginHandle();
// "Call" method allows instance to be called like a function GenkitPlugin call({String? apiKey}) { return MyPlugin(apiKey: apiKey); }
// Helper to create model references associated with this plugin ModelRef myModel(String name) { return modelRef('myplugin/$name', customOptions: MyModelOptions.$schema); }}
// User code:final ai = Genkit( plugins: [ myPlugin(apiKey: '...'), ],);
final response = await ai.generate( // Use the handle to create a type-safe model reference model: myPlugin.myModel('my-model'), prompt: 'Hello!',);4. Publishing a Plugin
Section titled “4. Publishing a Plugin”Genkit Dart plugins can be published as normal Dart packages to pub.dev.
To increase discoverability, your package should be named genkit_plugin_<name> or genkit_<name> and include genkit in the topics within your pubspec.yaml.
name: genkit_plugin_myplugindescription: My awesome Genkit plugin.version: 0.0.1# ...topics: - genkit - ai