-
Notifications
You must be signed in to change notification settings - Fork 0
API Errors
Amayyas edited this page Dec 5, 2025
·
1 revision
All error types in Flutter AI SDK.
AIError (sealed base class)
├── AIAuthenticationError
├── AIRateLimitError
├── AIContextLengthError
├── AIContentFilterError
├── AIInvalidRequestError
├── AIModelNotFoundError
├── AINetworkError
├── AIServerError
└── AITimeoutError
sealed class AIError extends Equatable implements Exception {
const AIError({
required String message,
String? code,
Map<String, dynamic>? details,
StackTrace? stackTrace,
});
}| Property | Type | Description |
|---|---|---|
message |
String |
Human-readable error message |
code |
String? |
Error code from provider |
details |
Map<String, dynamic>? |
Additional error details |
stackTrace |
StackTrace? |
Stack trace for debugging |
Thrown when authentication fails (invalid/expired API key).
final class AIAuthenticationError extends AIError {
const AIAuthenticationError({
required super.message,
super.code,
super.details,
super.stackTrace,
});
}try {
await ai.chat('Hello');
} on AIAuthenticationError catch (e) {
print('Check your API key: ${e.message}');
}Thrown when rate limit is exceeded.
final class AIRateLimitError extends AIError {
const AIRateLimitError({
required super.message,
super.code,
super.details,
super.stackTrace,
Duration? retryAfter,
});
final Duration? retryAfter;
}| Property | Type | Description |
|---|---|---|
retryAfter |
Duration? |
Time to wait before retrying |
try {
await ai.chat('Hello');
} on AIRateLimitError catch (e) {
print('Rate limited: ${e.message}');
if (e.retryAfter != null) {
await Future.delayed(e.retryAfter!);
// Retry
}
}Thrown when context/conversation is too long.
final class AIContextLengthError extends AIError {
const AIContextLengthError({
required super.message,
super.code,
super.details,
super.stackTrace,
int? maxTokens,
int? requestedTokens,
});
final int? maxTokens;
final int? requestedTokens;
}| Property | Type | Description |
|---|---|---|
maxTokens |
int? |
Maximum allowed tokens |
requestedTokens |
int? |
Tokens that were requested |
try {
await ai.chat(longMessage);
} on AIContextLengthError catch (e) {
print('Context too long');
print('Max: ${e.maxTokens}, Requested: ${e.requestedTokens}');
ai.clearContext(); // Clear and retry
}Thrown when content is blocked by safety filters.
final class AIContentFilterError extends AIError {
const AIContentFilterError({
required super.message,
super.code,
super.details,
super.stackTrace,
List<String>? categories,
});
final List<String>? categories;
}| Property | Type | Description |
|---|---|---|
categories |
List<String>? |
Filter categories triggered |
try {
await ai.chat(message);
} on AIContentFilterError catch (e) {
print('Content filtered');
print('Categories: ${e.categories?.join(", ")}');
}Thrown for malformed requests.
final class AIInvalidRequestError extends AIError {
const AIInvalidRequestError({
required super.message,
super.code,
super.details,
super.stackTrace,
String? parameter,
});
final String? parameter;
}| Property | Type | Description |
|---|---|---|
parameter |
String? |
The invalid parameter |
try {
await ai.chat('Hello');
} on AIInvalidRequestError catch (e) {
print('Invalid request: ${e.message}');
if (e.parameter != null) {
print('Bad parameter: ${e.parameter}');
}
}Thrown when the specified model doesn't exist.
final class AIModelNotFoundError extends AIError {
const AIModelNotFoundError({
required super.message,
super.code,
super.details,
super.stackTrace,
String? model,
});
final String? model;
}| Property | Type | Description |
|---|---|---|
model |
String? |
The model that wasn't found |
try {
await ai.chat('Hello');
} on AIModelNotFoundError catch (e) {
print('Model not found: ${e.model}');
}Thrown for network-related issues.
final class AINetworkError extends AIError {
const AINetworkError({
required super.message,
super.code,
super.details,
super.stackTrace,
bool isTimeout = false,
int? statusCode,
});
final bool isTimeout;
final int? statusCode;
}| Property | Type | Description |
|---|---|---|
isTimeout |
bool |
Whether it was a timeout |
statusCode |
int? |
HTTP status code |
try {
await ai.chat('Hello');
} on AINetworkError catch (e) {
if (e.isTimeout) {
print('Request timed out');
} else {
print('Network error: ${e.message}');
}
}Thrown for provider server errors.
final class AIServerError extends AIError {
const AIServerError({
required super.message,
super.code,
super.details,
super.stackTrace,
int? statusCode,
});
final int? statusCode;
}| Property | Type | Description |
|---|---|---|
statusCode |
int? |
HTTP status code (500, 502, etc.) |
try {
await ai.chat('Hello');
} on AIServerError catch (e) {
print('Server error: ${e.statusCode}');
// Wait and retry
}Thrown when a request times out.
final class AITimeoutError extends AIError {
const AITimeoutError({
required super.message,
super.code,
super.details,
super.stackTrace,
Duration? timeout,
});
final Duration? timeout;
}| Property | Type | Description |
|---|---|---|
timeout |
Duration? |
The timeout duration |
Future<String?> safeChatRequest(FlutterAI ai, String message) async {
try {
final response = await ai.chat(message);
return response.text;
} on AIAuthenticationError {
// Invalid API key
return null;
} on AIRateLimitError catch (e) {
// Rate limited - wait and retry
if (e.retryAfter != null) {
await Future.delayed(e.retryAfter!);
return safeChatRequest(ai, message);
}
return null;
} on AIContextLengthError {
// Context too long - clear and retry
ai.clearContext();
return safeChatRequest(ai, message);
} on AIContentFilterError {
// Content was filtered
return 'Your message could not be processed.';
} on AIInvalidRequestError catch (e) {
// Bad request
print('Invalid: ${e.parameter}');
return null;
} on AIModelNotFoundError catch (e) {
// Model doesn't exist
print('Model not found: ${e.model}');
return null;
} on AINetworkError catch (e) {
// Network issues
if (e.isTimeout) {
return 'Request timed out';
}
return 'Network error';
} on AIServerError {
// Provider issues
return 'Service temporarily unavailable';
} on AIError catch (e) {
// Other AI errors
print('AI Error: ${e.message}');
return null;
} catch (e) {
// Unexpected errors
print('Unexpected: $e');
return null;
}
}- Error Handling - Error handling guide
- API-FlutterAI - Main class reference
Getting Started
Core Concepts
Advanced Features
API Reference
Examples
Other