Skip to content

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).

Terminal window
dart pub add genkit_openai

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.

You can reference chat models like gpt-5.5 using the openAI.model() helper. The plugin ships a curated per-model catalog that describes each model’s capabilities. Resolution falls back to a dated-suffix alias and then generic defaults, so uncurated model ids still work with openAI.model().

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-5.5'),
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-5.5'),
prompt: 'Write a haiku about Dart.',
config: OpenAIOptions(
temperature: 0.7,
maxTokens: 100,
),
);

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-5.5'),
prompt: 'What\'s the weather in Boston?',
toolNames: ['getWeather'],
);
print(response.text);

The plugin supports streaming responses.

final stream = ai.generateStream(
model: openAI.model('gpt-5.5'),
prompt: 'Count from 1 to 5.',
);
await for (final chunk in stream) {
print(chunk.text);
}
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-5.5'),
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}');

You can reference embedding models using openAI.embedder(name):

final vectors = await ai.embed(
embedder: openAI.embedder('text-embedding-3-small'),
document: DocumentData(
content: [TextPart(text: 'The cat sat on the mat.')],
),
options: OpenAIEmbedderOptions(dimensions: 256),
);
print(vectors.single.embedding.length); // 256

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!',
);