In packages/ai/src/generate-speech/generated-audio-file.ts, when an audio format cannot be determined from the provider's media type, the constructor throws a plain JavaScript Error instead of an AISDKError subclass. There is even a // TODO this should be an AI SDK error comment on the line.
Location
packages/ai/src/generate-speech/generated-audio-file.ts, lines 43–48:
if (!format) {
// TODO this should be an AI SDK error
throw new Error(
'Audio format must be provided or determinable from media type',
);
}
Why this matters
All SDK errors should extend AISDKError so that:
- Callers can use
AISDKError.isInstance(e) to distinguish SDK errors from unexpected runtime errors.
- Errors carry a consistent
name and marker that middleware and error-reporting systems can rely on.
- The error is consistent with the rest of the SDK's error handling pattern.
This path is reachable when a provider returns audio data with a malformed media type (e.g. "audio/" — an empty subtype), leaving format as an empty string (falsy).
Proposed fix
Replace the generic Error with InvalidResponseDataError from @ai-sdk/provider, which already exists for cases where a provider returns data the SDK cannot interpret:
import { InvalidResponseDataError } from '@ai-sdk/provider';
if (!format) {
throw new InvalidResponseDataError({
data: mediaType,
message: `Could not determine audio format from media type: ${mediaType}`,
});
}
This also removes the long-standing // TODO comment.
AI SDK Version
- Reproduced on latest main (post-6.0.168).
- Affected package: ai (latest)
Code of Conduct
In
packages/ai/src/generate-speech/generated-audio-file.ts, when an audio format cannot be determined from the provider's media type, the constructor throws a plain JavaScriptErrorinstead of anAISDKErrorsubclass. There is even a// TODO this should be an AI SDK errorcomment on the line.Location
packages/ai/src/generate-speech/generated-audio-file.ts, lines 43–48:Why this matters
All SDK errors should extend
AISDKErrorso that:AISDKError.isInstance(e)to distinguish SDK errors from unexpected runtime errors.nameandmarkerthat middleware and error-reporting systems can rely on.This path is reachable when a provider returns audio data with a malformed media type (e.g.
"audio/"— an empty subtype), leavingformatas an empty string (falsy).Proposed fix
Replace the generic
ErrorwithInvalidResponseDataErrorfrom@ai-sdk/provider, which already exists for cases where a provider returns data the SDK cannot interpret:This also removes the long-standing
// TODOcomment.AI SDK Version
Code of Conduct