When initializing MistralProvider without a MISTRAL_API_KEY environment variable or an explicit apiKey, creating a model crashes with a cryptic Null check operator used on a null value error instead of the clean, user-friendly ArgumentError thrown by other providers (like Google or Anthropic).
This issue was discovered while experimenting with the antigravity-cli (agy CLI) agent, and still needs repo-side verification and fixing.
Root Cause Analysis
In packages/dartantic_ai/lib/src/providers/mistral_provider.dart, the MistralProvider constructor omits passing apiKeyName: defaultApiKeyName to its parent Provider constructor:
MistralProvider({String? apiKey, super.headers})
: super(
apiKey: apiKey ?? tryGetEnv(defaultApiKeyName),
name: 'mistral',
displayName: 'Mistral',
defaultModelNames: {
ModelKind.chat: 'mistral-medium-latest',
ModelKind.embeddings: 'mistral-embed',
},
baseUrl: null,
aliases: ['mistralai'],
// apiKeyName: defaultApiKeyName is missing here!
);
As a result:
apiKeyName evaluates to null.
- In
createChatModel(), the following validation check is completely bypassed because apiKeyName != null is false:
if (apiKeyName != null && (apiKey == null || apiKey!.isEmpty)) {
throw ArgumentError('$apiKeyName is required for $displayName provider');
}
- The method then tries to construct
MistralChatModel and applies a bang operator to apiKey (which is null):
return MistralChatModel(
...
apiKey: apiKey!, // <-- Crashes here with Null check operator used on a null value
...
);
Suggested Fix
-
Surgically fix the constructor:
Update packages/dartantic_ai/lib/src/providers/mistral_provider.dart to specify the missing parameter:
MistralProvider({String? apiKey, super.headers})
: super(
apiKey: apiKey ?? tryGetEnv(defaultApiKeyName),
name: 'mistral',
displayName: 'Mistral',
defaultModelNames: {
ModelKind.chat: 'mistral-medium-latest',
ModelKind.embeddings: 'mistral-embed',
},
baseUrl: null,
aliases: ['mistralai'],
apiKeyName: defaultApiKeyName, // <-- Fix
);
-
Add unit test coverage:
Add a regression test in packages/dartantic_ai/test/provider_initialization_test.dart to ensure it resolves correctly and throws a clean ArgumentError:
test('Mistral provider requires MISTRAL_API_KEY to create chat models', () {
final mistralProvider = Agent.getProvider('mistral');
expect(
mistralProvider.createChatModel,
throwsA(
isA<ArgumentError>().having(
(e) => e.message,
'message',
contains('MISTRAL_API_KEY is required'),
),
),
);
});
When initializing
MistralProviderwithout aMISTRAL_API_KEYenvironment variable or an explicitapiKey, creating a model crashes with a crypticNull check operator used on a null valueerror instead of the clean, user-friendlyArgumentErrorthrown by other providers (like Google or Anthropic).This issue was discovered while experimenting with the
antigravity-cli(agyCLI) agent, and still needs repo-side verification and fixing.Root Cause Analysis
In
packages/dartantic_ai/lib/src/providers/mistral_provider.dart, theMistralProviderconstructor omits passingapiKeyName: defaultApiKeyNameto its parentProviderconstructor:As a result:
apiKeyNameevaluates tonull.createChatModel(), the following validation check is completely bypassed becauseapiKeyName != nullis false:MistralChatModeland applies a bang operator toapiKey(which isnull):Suggested Fix
Surgically fix the constructor:
Update
packages/dartantic_ai/lib/src/providers/mistral_provider.dartto specify the missing parameter:Add unit test coverage:
Add a regression test in
packages/dartantic_ai/test/provider_initialization_test.dartto ensure it resolves correctly and throws a cleanArgumentError: