OpenAI Plugin
The genkit_openai package provides access to OpenAI models as well as any OpenAI-compatible API (e.g. xAI/Grok, DeepSeek, Together AI).
Installation
Section titled “Installation”dart pub add genkit_openaiConfiguration
Section titled “Configuration”To use this plugin, import it and specify it when you initialize Genkit:
import 'dart:io';import 'package:genkit/genkit.dart';
import 'package:genkit_openai/genkit_openai.dart';
void main() async { // Initialize Genkit with the OpenAI plugin final ai = Genkit(plugins: [ openAI(apiKey: Platform.environment['OPENAI_API_KEY']), ]);}The plugin requires an API key for the OpenAI API. You can get one from the OpenAI Platform.
The plugin provides helpers to reference supported models.
Chat Models
Section titled “Chat Models”You can reference chat models like gpt-4o using the openAI.model() helper.
import 'dart:io';import 'package:genkit/genkit.dart';import 'package:genkit_openai/genkit_openai.dart';
void main() async { final ai = Genkit(plugins: [ openAI(apiKey: Platform.environment['OPENAI_API_KEY']), ]);
final response = await ai.generate( model: openAI.model('gpt-4o'), prompt: 'Tell me a joke about Dart.', );
print(response.text);}You can also pass model-specific configuration:
final response = await ai.generate( model: openAI.model('gpt-4o'), prompt: 'Write a haiku about Dart.', config: OpenAIOptions( temperature: 0.7, maxTokens: 100, ),);Tool Calling
Section titled “Tool Calling”You can define and use tools with OpenAI models.
import 'package:schemantic/schemantic.dart';
// part 'main.g.dart'; // generated by build_runner
@Schema()abstract class $WeatherInput { String get location;}
// ... inside main ...
ai.defineTool( name: 'getWeather', description: 'Get the weather for a location', inputSchema: WeatherInput.$schema, outputSchema: .string(), fn: (input, ctx) async => 'The weather in ${input.location} is sunny and 72 degrees.',);
final response = await ai.generate( model: openAI.model('gpt-4o'), prompt: 'What\'s the weather in Boston?', toolNames: ['getWeather'],);
print(response.text);Streaming
Section titled “Streaming”The plugin supports streaming responses.
final stream = ai.generateStream( model: openAI.model('gpt-4o'), prompt: 'Count from 1 to 5.',);
await for (final chunk in stream) { print(chunk.text);}Structured Output
Section titled “Structured Output”import 'package:schemantic/schemantic.dart';
// part 'main.g.dart'; // generated by build_runner
@Schema()abstract class $Person { String get name; int get age;}
// ... inside main ...
final response = await ai.generate( model: openAI.model('gpt-4o'), prompt: 'Generate a person named John Doe, age 30', outputSchema: Person.$schema,);
final person = Person.fromJson(response.output!);print('Name: ${person.name}, Age: ${person.age}');OpenAI-Compatible APIs
Section titled “OpenAI-Compatible APIs”The plugin supports any OpenAI-compatible API by specifying a custom baseUrl:
final ai = Genkit(plugins: [ openAI( apiKey: Platform.environment['GROQ_API_KEY'], baseUrl: 'https://api.groq.com/openai/v1', models: [ CustomModelDefinition( name: 'llama-3.3-70b-versatile', info: ModelInfo( label: 'Llama 3.3 70B', supports: { 'multiturn': true, 'tools': true, 'systemRole': true, }, ), ), ], ),]);
final response = await ai.generate( model: openAI.model('llama-3.3-70b-versatile'), prompt: 'Hello Groq!',);