Skip to content

DefaultGeneratedAudioFile throws a generic Error instead of AISDKError when audio format cannot be determined #15814

@Abuhaithem

Description

@Abuhaithem

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

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions