LanguageModel: availability() static method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The availability() static method of the LanguageModel interface returns a status identifier indicating whether the browser's language model supports a given set of configuration options, without creating a session or triggering a download.

Use availability() before calling LanguageModel.create() to determine whether the desired configuration is supported. This avoids initiating a session only to have it fail, and lets you provide a meaningful fallback to users when the configuration is not supported.

Syntax

js
LanguageModel.availability()
LanguageModel.availability(options)

Parameters

options Optional

An object that represents the base set of options used when checking language model support. Properties include:

expectedInputs Optional

An array of objects representing the required input modalities and languages. Each object can include the following properties:

type

An enumerated value indicating the content type. Must be one of:

text

Plain text content.

image

Image content.

audio

Audio content.

tool-call

A tool invocation issued by the model.

tool-response

The result of a tool invocation.

languages Optional

An array of strings containing BCP 47 language tags (for example, en, fr, ja) representing languages that the session is expected to handle. The user agent uses this list to determine whether the model supports the specified languages.

expectedOutputs

An array of objects representing the required output modalities and languages. Each object can include the following properties:

type

An enumerated value indicating the content type. Must be one of:

text

Textual content.

image

Image content.

audio

Audio content.

tool-call

A tool invocation issued by the model.

tool-response

The result of a tool invocation.

languages Optional

An array of strings containing BCP 47 language tags (for example, en, fr, ja) that the session is expected to output.

tools

An array of objects representing tools available to the AI. Each object can include the following properties:

name

A string giving the tool a unique name the model uses to refer to it when issuing a tool call.

description

A string describing what the tool does. The model uses this description to decide when and whether to invoke the tool.

inputSchema

An object containing a JSON Schema that describes the tool's input parameters. The model uses this schema to construct the arguments it passes to the tool's execute function.

execute

A callback function that the user agent invokes when the model calls this tool. It can receive any arguments provided by the model as appropriate and returns a Promise that resolves with a String representing the tool's result.

Return value

A Promise that resolves with one of the values listed below.

available

The model is ready to use with the given options.

downloadable

The model can support the given options but needs to download additional data to do so. The download has not yet started.

downloading

The model can support the given options with an additional data download. The download is currently in progress.

unavailable

The model cannot support the given options, or the user agent cannot determine availability, for example, due to a transient activation error. In that case, the caller should retry or fall back to an alternative implementation.

Exceptions

InvalidStateError DOMException

Thrown if the calling document is not fully active.

NotAllowedError DOMException

Thrown if usage of the method is blocked by a language-model Permissions-Policy.

Examples

See also Using the Prompt API > Checking configuration support and the Complete example on the same page.

Requesting input support

This example shows how to determine whether text and image inputs are supported by the model.

js
const status = await LanguageModel.availability({
  expectedInputs: [{ type: "text" }, { type: "image" }],
});

Checking availability for a specific language

This example tests whether the model supports English before asking it to translate Japanese text to English.

js
const status = await LanguageModel.availability({
  expectedInputs: [{ type: "text", languages: ["ja"] }],
  expectedOutputs: [{ type: "text", languages: ["en"] }],
});

if (status === "available") {
  const session = await LanguageModel.create({
    expectedInputs: [{ type: "text", languages: ["ja"] }],
    expectedOutputs: [{ type: "text", languages: ["en"] }],
  });

  const translation = await session.prompt([
    {
      role: "user",
      content: "Translate the following text into English",
    },
    {
      role: "user",
      content: "桜はきれいです",
    },
  ]);

  console.log(translation);
}

Checking availability for multimodal input

Multimodal input describes sessions that can use more than one type of input, such as text and images. Since the availability of input types varies by language model, your code should check the availability of desired modes before creating a session. An example is shown here.

js
const availability = await LanguageModel.availability({
  expectedInputs: [{ type: "text" }, { type: "image" }],
  expectedOutputs: [{ type: "text", languages: ["en"] }],
});

if (availability === "unavailable") {
  console.warn("This configuration is not supported.");
} else {
  const session = await LanguageModel.create({
    expectedInputs: [{ type: "text" }, { type: "image" }],
    expectedOutputs: [{ type: "text", languages: ["en"] }],
  });
}

Specifications

Specification
Prompt API
# dom-languagemodel-availability

Browser compatibility

See also