ai_sdk_dart 2.0.0
ai_sdk_dart: ^2.0.0 copied to clipboard
Core AI SDK for Dart with provider-agnostic model APIs.
๐ค AI SDK Dart #
A Dart/Flutter port of Vercel AI SDK v6 โ provider-agnostic APIs for text generation, streaming, structured output, tool use, embeddings, image generation, speech, and more.
What is this? #
AI SDK Dart brings the core concepts of Vercel AI SDK v6 to Dart and Flutter. Write your AI logic once, swap providers without changing business code, and ship on mobile, web, and server. The API follows the same provider-agnostic model while using idiomatic Dart types and lifecycle primitives.
Upgrading to 2.0 #
Version 2.0 replaces the language-model V3 provider seam with V4 and removes
the obsolete V3 types. Most apps can upgrade their coordinated ai_sdk_*
dependencies together; custom providers, middleware, and direct provider-type
consumers need source changes. See the 2.0 migration guide
for the exact renames and contract changes.
Screenshots #
Flutter Chat App (examples/flutter_chat) #
| Multi-turn Chat | Streaming Response |
![]() |
![]() |
| Completion | Object Stream |
![]() |
![]() |
Advanced App (examples/advanced_app) #
| Provider Chat | Tools Chat |
![]() |
![]() |
| Image Generation | Multimodal |
![]() |
![]() |
โจ Features #
๐ฃ๏ธ Text Generation & Streaming #
generateTextโ single-turn or multi-step text generation with full result envelopestreamTextโ real-time token streaming with typed event taxonomysmoothStreamtransform โ configurable chunk-size smoothing;delayInMsoption adds per-chunk delay for UX pacing- Multi-step agentic loops with
maxSteps,prepareStep, andstopConditions timeoutparameter on all core functions โ applyDurationdeadlines to any model call- Callbacks:
onFinish,onStepFinish,onChunk,onError,experimentalOnStart,onAbort
๐งฉ Structured Output #
Output.object(schema)โ parse model output into a typed Dart objectOutput.array(schema)โ parse model output into a typed Dart listOutput.choice(options)โ constrain output to a fixed set of string valuesOutput.json()โ raw JSON without schema validation- Automatic code-fence stripping (
```json ... ```)
๐ง Type-Safe Tools & Multi-Step Agents #
tool<Input, Output>()โ fully typed tool definitions with JSON schemadynamicTool()โ tools with unknown input type for dynamic use cases- Tool choice:
auto,required,none, or specific tool - Tool approval workflow with
needsApproval - Multi-step agentic loops with automatic tool result injection
onInputStart,onInputDelta,onInputAvailablelifecycle hooks
๐ผ๏ธ Multimodal #
generateImageโ image generation (gpt-image-1 / DALLยทE via OpenAI)generateSpeechโ text-to-speech audio synthesistranscribeโ speech-to-text transcription- Image inputs in prompts (multimodal vision)
๐งฎ Embeddings & Cosine Similarity #
embed()โ single value embedding with usage trackingembedMany()โ batch embedding for multiple values with configurable chunk sizecosineSimilarity()โ built-in similarity computationwrapEmbeddingModel()โ composable middleware pipeline for embedding models
๐งฑ Middleware System #
wrapLanguageModel(model: ..., middleware: ...)โ composable middleware pipelineextractReasoningMiddlewareโ strips<think>tags intoReasoningPartextractJsonMiddlewareโ strips```json ```fencessimulateStreamingMiddlewareโ converts non-streaming models to streamingdefaultSettingsMiddlewareโ applies default temperature/top-p/etc.addToolInputExamplesMiddlewareโ enriches tool descriptions with exampleswrapEmbeddingModel/wrapImageModelโ the same composable middleware pattern for embedding and image models
๐ Provider Registry #
createProviderRegistryโ map provider aliases to model factoriescustomProvider()โ lightweight on-the-fly provider construction without a full registry- Resolve models by
'provider:modelId'string at runtime - Supports 6 model categories: language, embedding, image, speech, transcription, rerank
- Mix providers in a single registry for multi-provider apps
๐ฑ Flutter UI Controllers & Widgets #
ChatControllerโ multi-turn streaming chat with message historyCompletionControllerโ single-turn text completion with statusObjectStreamControllerโ streaming typed JSON object updates- 19 prebuilt, themeable Material widgets โ
AiChatScaffold, message list/bubbles, composer, streaming text, typing indicator, tool-call & approval cards, reasoning, citations, usage, and more
๐ MCP Client (Model Context Protocol) #
MCPClientโ connect to MCP servers, discover tools, invoke themStreamableHttpClientTransportโ MCP Streamable HTTP transport (2025-06-18) for remote serversStdioMCPTransportโ stdio process transport (native platforms)- Web-safe โ
dart:iois isolated behind conditional imports, so the client runs on Flutter web - Discovered tools are directly compatible with
generateText/streamText
๐จ Typed Errors #
- Sealed
AiSdkErrorhierarchy โAiApiCallError,AiNoObjectGeneratedError,AiRetryError, and more - Provider API errors are typed โ a non-2xx response throws
AiApiCallErrorcarrying the provider'smessage,type,code,statusCode, raw body, and anisRetryableflag, consistently across every provider
๐งช Conformance Suite #
- Comprehensive Dart and Flutter tests across every package and both example apps
- A repository-wide 99% line-coverage gate enforced in CI
- Spec-driven JSON fixtures as the source of truth
- Provider wire-format conformance tests for every provider (plus a typed-error conformance test per provider)
MockEmbeddingModelV3testing utility for embedding model conformance
๐ฆ Packages #
| Package | pub.dev | What it gives you |
|---|---|---|
ai_sdk_dart |
dart pub add ai_sdk_dart |
generateText, streamText, tools, middleware, embeddings, registry |
ai_sdk_openai |
dart pub add ai_sdk_openai |
openai('gpt-4.1-mini'), embeddings, image gen, speech, transcription, reasoning options |
ai_sdk_anthropic |
dart pub add ai_sdk_anthropic |
anthropic('claude-sonnet-4-5'), extended thinking, speed options |
ai_sdk_google |
dart pub add ai_sdk_google |
google('gemini-2.0-flash'), embeddings |
ai_sdk_azure |
dart pub add ai_sdk_azure |
AzureOpenAIProvider(endpoint, apiKey), language models, embeddings |
ai_sdk_cohere |
dart pub add ai_sdk_cohere |
cohere('command-r-plus'), embeddings, reranking |
ai_sdk_groq |
dart pub add ai_sdk_groq |
groq('llama3-8b-8192'), ultra-low latency inference |
ai_sdk_mistral |
dart pub add ai_sdk_mistral |
mistral('mistral-large-latest'), embeddings |
ai_sdk_ollama |
dart pub add ai_sdk_ollama |
ollama('llama3'), local inference, embeddings |
ai_sdk_flutter_ui |
dart pub add ai_sdk_flutter_ui |
ChatController, CompletionController, ObjectStreamController + 19 prebuilt chat widgets |
ai_sdk_mcp |
dart pub add ai_sdk_mcp |
MCPClient, StreamableHttpClientTransport, native-only StdioMCPTransport |
ai_sdk_provider |
(transitive) | Provider interfaces for building custom providers |
ai_sdk_openai_compatible |
(transitive) | Shared OpenAI Chat Completions base โ powers the OpenAI/Azure/Groq/Mistral language models |
ai_sdk_providerandai_sdk_openai_compatibleare transitive dependencies โ you do not need to add them directly.
๐ Quick Start #
Dart CLI #
dart pub add ai_sdk_dart ai_sdk_openai
import 'dart:io';
import 'package:ai_sdk_dart/ai_sdk_dart.dart';
import 'package:ai_sdk_openai/ai_sdk_openai.dart';
Future<void> main() async {
final apiKey = Platform.environment['OPENAI_API_KEY'];
if (apiKey == null || apiKey.isEmpty) {
throw StateError('Set OPENAI_API_KEY before running this example.');
}
final provider = OpenAIProvider(apiKey: apiKey);
final result = await generateText(
model: provider('gpt-4.1-mini'),
prompt: 'Say hello from AI SDK Dart!',
);
print(result.text);
}
For server and CLI apps, prefer reading credentials from your runtime
environment and passing apiKey: yourself, as shown above. The convenience
factories like openai('...'), anthropic('...'), and google('...') read
compile-time defines such as OPENAI_API_KEY, so they are best paired with
dart run --define=... or Flutter --dart-define=....
Streaming #
import 'dart:io';
final result = await streamText(
model: openai('gpt-4.1-mini'),
prompt: 'Count from 1 to 5.',
);
await for (final chunk in result.textStream) {
stdout.write(chunk);
}
Structured Output #
final result = await generateText<Map<String, dynamic>>(
model: openai('gpt-4.1-mini'),
prompt: 'Return the capital and currency of Japan as JSON.',
output: Output.object(
schema: Schema<Map<String, dynamic>>(
jsonSchema: const {
'type': 'object',
'properties': {
'capital': {'type': 'string'},
'currency': {'type': 'string'},
},
},
fromJson: (json) => json,
),
),
);
print(result.output); // {capital: Tokyo, currency: JPY}
Type-Safe Tools #
final result = await generateText(
model: openai('gpt-4.1-mini'),
prompt: 'What is the weather in Paris?',
maxSteps: 5,
tools: {
'getWeather': tool<Map<String, dynamic>, String>(
description: 'Get current weather for a city.',
inputSchema: Schema(
jsonSchema: const {
'type': 'object',
'properties': {'city': {'type': 'string'}},
},
fromJson: (json) => json,
),
execute: (input, _) async => 'Sunny, 18ยฐC',
),
},
);
print(result.text);
Error handling #
try {
final result = await generateText(
model: openai('gpt-4.1-mini'),
prompt: 'Hello',
);
} on AiApiCallError catch (e) {
// Typed provider error โ message, status, and retryability are all available.
print('${e.statusCode}: ${e.message} (retryable: ${e.isRetryable})');
}
Flutter Chat UI #
dart pub add ai_sdk_dart ai_sdk_openai ai_sdk_flutter_ui
import 'package:ai_sdk_dart/ai_sdk_dart.dart';
import 'package:ai_sdk_openai/ai_sdk_openai.dart';
import 'package:ai_sdk_flutter_ui/ai_sdk_flutter_ui.dart';
final agent = ToolLoopAgent(
model: openai('gpt-4.1-mini'),
instructions: 'You are a helpful assistant.',
);
final chat = ChatController();
// In your widget โ a complete chat surface:
AiChatScaffold(controller: chat, agent: agent);
Do not ship long-lived provider API keys inside distributed browser, mobile, or desktop clients. Use a trusted proxy or backend-minted short-lived credentials instead. The Flutter examples below use
--dart-definefor local development and smoke testing, not as a production secret-distribution strategy.
๐ค Providers #
| Capability | OpenAI | Anthropic | Azure | Cohere | Groq | Mistral | Ollama | |
|---|---|---|---|---|---|---|---|---|
| Text generation | โ | โ | โ | โ | โ | โ | โ | โ |
| Streaming | โ | โ | โ | โ | โ | โ | โ | โ |
| Structured output | โ | โ | โ | โ | โ | โ | โ | โ |
| Native JSON schema output | โ | โ | โ | โ | โ | โ | โ | โ |
| Tool use | โ | โ | โ | โ | โ | โ | โ | โ |
| Embeddings | โ | โ | โ | โ | โ | โ | โ | โ |
| Reranking | โ | โ | โ | โ | โ | โ | โ | โ |
| Image generation | โ | โ | โ | โ | โ | โ | โ | โ |
| Speech synthesis | โ | โ | โ | โ | โ | โ | โ | โ |
| Transcription | โ | โ | โ | โ | โ | โ | โ | โ |
| Extended thinking | โ | โ | โ | โ | โ | โ | โ | โ |
| Reasoning options | โ | โ | โ | โ | โ | โ | โ | โ |
| Multimodal (image input) | โ | โ | โ | โ | โ | โ | โ | โ |
๐ ๏ธ Flutter UI #
The ai_sdk_flutter_ui package provides three reactive controllers plus a library of 19 prebuilt,
themeable Material widgets โ so you can wire up a full chat UI in a few lines, or drop down to the
controllers and render everything yourself.
Drop-in chat UI #
import 'package:ai_sdk_dart/ai_sdk_dart.dart';
import 'package:ai_sdk_flutter_ui/ai_sdk_flutter_ui.dart';
final agent = ToolLoopAgent(model: openai('gpt-4.1-mini'));
final chat = ChatController();
// A complete message list + composer, wired to the controller + agent:
AiChatScaffold(controller: chat, agent: agent);
Other widgets โ ChatMessageList, ChatMessageBubble, ChatComposer, StreamingTextView,
TypingIndicator, ToolCallCard, ToolApprovalCard, ReasoningView, SourceCitations,
UsageView, PromptSuggestions, ObjectStreamView, and more โ can be composed ร la carte. They
read only the controllers' public state, so they work with any state-management approach.
ChatController โ Multi-turn streaming chat #
final agent = ToolLoopAgent(model: openai('gpt-4.1-mini'));
final chat = ChatController();
// In your widget:
ListenableBuilder(
listenable: chat,
builder: (context, _) {
return Column(
children: [
for (final msg in chat.messages)
Text('${msg.role}: ${msg.content}'),
if (chat.isLoading) const CircularProgressIndicator(),
],
);
},
);
// Send a message:
await chat.sendMessage(agent: agent, text: 'What is the capital of France?');
CompletionController โ Single-turn completion #
final completion = CompletionController(
agent: ToolLoopAgent(model: openai('gpt-4.1-mini')),
);
await completion.complete('Write a haiku about Dart.');
print(completion.completion);
ObjectStreamController โ Streaming typed JSON #
final controller = ObjectStreamController<Map<String, dynamic>>(
model: openai('gpt-4.1-mini'),
schema: Schema<Map<String, dynamic>>(
jsonSchema: const {'type': 'object'},
fromJson: (json) => json,
),
);
await controller.submit('Describe Japan as a JSON object.');
print(controller.value); // Partial updates arrive in real-time
๐ MCP Support #
Connect to any Model Context Protocol server and use its tools directly in your AI calls:
import 'package:ai_sdk_dart/ai_sdk_dart.dart';
import 'package:ai_sdk_mcp/ai_sdk_mcp.dart';
import 'package:ai_sdk_openai/ai_sdk_openai.dart';
final client = MCPClient(
transport: StreamableHttpClientTransport(
url: Uri.parse('http://localhost:3000/mcp'),
headers: {'Authorization': 'Bearer <short-lived-token>'},
),
);
await client.initialize();
final tools = await client.tools(); // Returns a ToolSet
final result = await generateText(
model: openai('gpt-4.1-mini'),
prompt: 'What files are in the project?',
tools: tools,
maxSteps: 5,
);
For stdio-based MCP servers (local processes):
final client = MCPClient(
transport: StdioMCPTransport(
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', '/path/to/dir'],
),
);
StreamableHttpClientTransport speaks the MCP Streamable HTTP transport
(2025-06-18) against a single endpoint. It negotiates the protocol version
during initialize(), sends notifications/initialized, accepts JSON or SSE
responses to each POST, starts the optional GET SSE listener for
server-pushed notifications, reconnects that listener with Last-Event-ID, and
sends DELETE on shutdown when the server assigned Mcp-Session-Id. Put
required auth or routing headers in headers, but avoid embedding long-lived
secrets in shipped browser or mobile clients. The HTTP transport is web-safe โ
dart:io is only pulled in by StdioMCPTransport on native platforms, behind
a conditional import โ so the client also runs on Flutter web.
๐บ๏ธ Roadmap #
โ Implemented #
- โ
generateTextโ full result envelope (text, steps, usage, reasoning, sources, files) - โ
streamTextโ complete event taxonomy (20 typed event types),onAbortcallback - โ
generateObject/ structured output (object, array, choice, json) with native JSON schema - โ
embed/embedMany+cosineSimilarity,wrapEmbeddingModel - โ
generateImage(OpenAI gpt-image-1 / DALLยทE) - โ
generateSpeech(OpenAI TTS) - โ
transcribe(OpenAI Whisper) - โ
rerank - โ
timeoutparameter on all core functions - โ
customProvider()for lightweight on-the-fly provider construction - โ Middleware system โ 5 built-in language-model middlewares, plus embedding & image model middleware
- โ
Provider registry (
createProviderRegistry) โ 6 model categories - โ Multi-step agentic loops with tool approval
- โ Flutter UI controllers (Chat, Completion, ObjectStream) + 19 prebuilt Material widgets
- โ MCP client (Streamable HTTP + stdio transports, prompts, resources, web-safe)
- โ
Typed provider API errors (
AiApiCallErrorwith status / type / code / body) across all providers - โ OpenAI (with reasoning options), Anthropic (with thinking options), Google providers
- โ Cohere, Mistral, Groq, Ollama, Azure OpenAI providers โ all with tools + multimodal
- โ Comprehensive tests with a repository-wide 99% line-coverage gate
๐ Planned #
- ๐ Streaming MCP tool outputs
- ๐ Richer attachment widgets (file/image pickers, audio capture)
- ๐ Dart Edge / Cloudflare Workers support
- ๐ WebSocket transport for MCP
๐ค Contributing #
Contributions are welcome! Please open an issue first to discuss changes before submitting a PR.
- ๐ Bug reports โ use the Bug Report template
- ๐ก Feature requests โ use the Feature Request template
- ๐ฌ Questions & discussions โ use GitHub Discussions
Running tests #
fvm dart pub get
make test
make analyze
Or run a smaller set of pinned toolchain smoke checks directly:
fvm dart analyze .
fvm dart test packages/ai_sdk_dart/test/
fvm dart test packages/ai_sdk_openai/test/
fvm dart test packages/ai_sdk_anthropic/test/
fvm dart test packages/ai_sdk_google/test/
fvm flutter test examples/flutter_chat/
fvm flutter test examples/advanced_app/
Runnable examples #
CLI and server examples can read credentials from the process environment. The
repo make targets forward those values to the provider factories as
compile-time defines when needed:
OPENAI_API_KEY=sk-... make run-basic
OPENAI_API_KEY=sk-... make run-mcp
Equivalent direct Dart invocation:
OPENAI_API_KEY=sk-... \
fvm dart run --define=OPENAI_API_KEY=sk-... examples/basic/lib/main.dart
Flutter example apps are different: they read compile-time defines from
String.fromEnvironment, so pass keys with --dart-define:
fvm flutter run -C examples/flutter_chat \
--dart-define=OPENAI_API_KEY=sk-...
fvm flutter run -C examples/advanced_app \
--dart-define=OPENAI_API_KEY=sk-... \
--dart-define=ANTHROPIC_API_KEY=sk-ant-... \
--dart-define=GOOGLE_API_KEY=AIza...
The Flutter commands above compile the keys into the client app. Use them for local demos only. Production apps should call a trusted backend or fetch short-lived provider credentials instead of embedding long-lived secrets.
| Example | Command | What it shows |
|---|---|---|
| Dart CLI | OPENAI_API_KEY=sk-... make run-basic |
generateText, streaming, structured output, tools, embeddings, middleware |
| Flutter chat | cd examples/flutter_chat && fvm flutter run --dart-define=OPENAI_API_KEY=sk-... |
ChatController, CompletionController, ObjectStreamController |
| Flutter chat (web) | cd examples/flutter_chat && fvm flutter run -d chrome --dart-define=OPENAI_API_KEY=sk-... |
Same as above on Chrome |
| Advanced app | cd examples/advanced_app && fvm flutter run --dart-define=OPENAI_API_KEY=sk-... --dart-define=ANTHROPIC_API_KEY=sk-ant-... --dart-define=GOOGLE_API_KEY=AIza... |
All providers, tools, image gen, TTS, STT, multimodal, embeddings, completion, object stream + widget gallery |
| Advanced app (web) | cd examples/advanced_app && fvm flutter run -d chrome --dart-define=OPENAI_API_KEY=sk-... --dart-define=ANTHROPIC_API_KEY=sk-ant-... --dart-define=GOOGLE_API_KEY=AIza... |
Same as above on Chrome |
| MCP demo | make run-mcp |
MCP tool discovery + direct tool calls (works without an API key) |
Development #
Managed with Melos as a monorepo workspace:
fvm dart pub get
make analyze
make test
See docs/v6-parity-matrix.md for a feature-by-feature parity matrix against Vercel AI SDK v6.







