ai_sdk_google 3.0.0
ai_sdk_google: ^3.0.0 copied to clipboard
Google Generative AI provider for the AI SDK Dart port.
ai_sdk_google examples #
Installation #
dart pub add ai_sdk_dart ai_sdk_google
The top-level google factory reads
const String.fromEnvironment('GOOGLE_API_KEY'). Use it with
fvm dart run --define=GOOGLE_API_KEY=AIza... bin/app.dart, or read
Platform.environment['GOOGLE_API_KEY'] yourself and pass apiKey: to
GoogleGenerativeAIProvider in server and CLI apps.
Text generation #
import 'package:ai_sdk_dart/ai_sdk_dart.dart';
import 'package:ai_sdk_google/ai_sdk_google.dart';
final result = await generateText(
model: google('gemini-2.0-flash'),
prompt: 'What is the speed of light?',
);
print(result.text);
print('tokens used: ${result.usage}');
Streaming #
import 'dart:io';
final result = await streamText(
model: google('gemini-2.0-flash'),
prompt: 'Explain how neural networks learn.',
);
await for (final chunk in result.textStream) {
stdout.write(chunk);
}
System prompt #
final result = await generateText(
model: google('gemini-2.0-flash'),
system: 'You are a helpful assistant that replies only in haiku.',
prompt: 'Tell me about Flutter.',
);
print(result.text);
instructions is the current canonical top-level instruction parameter;
system remains a supported fallback and is used above for continuity with
older code. Prefer instructions in new code:
final result = await generateText(
model: google('gemini-2.0-flash'),
instructions: 'You are a helpful assistant that replies only in haiku.',
prompt: 'Tell me about Flutter.',
);
print(result.text);
Reasoning #
The core reasoning: parameter (LanguageModelV4Reasoning, from
package:ai_sdk_provider) works with Google models — it's mapped internally
to Gemini's thinkingConfig/thinkingBudget:
import 'package:ai_sdk_provider/ai_sdk_provider.dart';
final result = await generateText(
model: google('gemini-2.5-pro'),
reasoning: LanguageModelV4Reasoning.high,
prompt: 'Solve step by step: if 3x + 5 = 20, what is x?',
);
print(result.text);
print(result.reasoningText);
Signed reasoning replay #
Google's reasoning parts carry an opaque thoughtSignature in
providerOptions['google']['thoughtSignature']. Don't read or construct it
yourself — carry result.responseMessages through unchanged as history and it
round-trips automatically:
const question = ModelMessage(
role: ModelMessageRole.user,
content: 'Solve step by step: if 3x + 5 = 20, what is x?',
);
final first = await generateText(
model: google('gemini-2.5-pro'),
reasoning: LanguageModelV4Reasoning.high,
messages: [question],
);
final second = await generateText(
model: google('gemini-2.5-pro'),
reasoning: LanguageModelV4Reasoning.high,
messages: [
question,
...first.responseMessages.map(ModelMessage.fromProvider),
const ModelMessage(role: ModelMessageRole.user, content: 'Now check your work.'),
],
);
print(second.text);
Structured output #
final result = await generateText<Map<String, dynamic>>(
model: google('gemini-2.0-flash'),
prompt: 'Return the capital and timezone of Japan as JSON.',
output: Output.object(
schema: Schema<Map<String, dynamic>>(
jsonSchema: const {
'type': 'object',
'properties': {
'capital': {'type': 'string'},
'timezone': {'type': 'string'},
},
},
fromJson: (json) => json,
),
),
);
print(result.output); // {capital: Tokyo, timezone: Asia/Tokyo}
Tools (multi-step agent) #
final result = await generateText(
model: google('gemini-2.0-flash'),
prompt: 'What is the weather in Tokyo?',
maxSteps: 5,
tools: {
'getWeather': tool<Map<String, dynamic>, String>(
description: 'Get current weather for a city.',
inputSchema: Schema<Map<String, dynamic>>(
jsonSchema: const {
'type': 'object',
'properties': {'city': {'type': 'string'}},
'required': ['city'],
},
fromJson: (json) => json,
),
execute: (input, _) async => 'Sunny, 28°C in ${input['city']}',
),
},
);
print(result.text);
Embeddings #
final a = await embed(
model: google.embedding('text-embedding-004'),
value: 'Hello, world!',
);
final b = await embed(
model: google.embedding('text-embedding-004'),
value: 'Hi there!',
);
print('similarity: ${cosineSimilarity(a.embedding, b.embedding)}');
Multimodal (image + text) #
import 'dart:io';
import 'package:ai_sdk_provider/ai_sdk_provider.dart';
final imageBytes = await File('photo.jpg').readAsBytes();
final result = await generateText(
model: google('gemini-2.0-flash'),
messages: [
ModelMessage.parts(
role: ModelMessageRole.user,
parts: [
LanguageModelV4ImagePart(
image: DataContentBytes(imageBytes),
mediaType: 'image/jpeg',
),
const LanguageModelV4TextPart(text: 'What is in this image?'),
],
),
],
);
print(result.text);
Custom API key #
final myGoogle = GoogleGenerativeAIProvider(apiKey: 'AIza...');
final result = await generateText(
model: myGoogle('gemini-2.0-flash'),
prompt: 'Hello!',
);
Runnable example apps #
examples/basic— Dart CLI covering all core APIsexamples/advanced_app— Flutter app with Google provider, multimodal, and embeddings