1. Introduction
The Prompt API gives web pages the ability to directly prompt a browser-provided language model. It provides a uniform JavaScript API that abstracts away specific details of the underlying model (such as templating or tokenization). By leveraging built-in language models, it offers benefits such as local processing of sensitive data, offline usage, model sharing, and reduced cost compared to cloud-based or bring-your-own-model approaches.
2. Dependencies
This specification depends on the Infra Standard. [INFRA]
As
with
the
rest
of
the
web
platform,
human
languages
are
identified
in
these
APIs
by
BCP
47
language
tags,
such
as
"
ja
",
"
en-US
",
"
sr-Cyrl
",
or
"
de-CH-1901-x-phonebk-extended
".
The
specific
algorithms
used
for
validation,
canonicalization,
and
language
tag
matching
are
those
from
the
ECMAScript
Internationalization
API
Specification
,
which
in
turn
defers
some
of
its
processing
to
Unicode
Locale
Data
Markup
Language
(LDML)
.
[BCP47]
[ECMA-402]
[UTS35]
.
These APIs are part of a family of APIs expected to be powered by machine learning models, which share common API surface idioms and specification patterns. Currently, the specification text for these shared parts lives in Writing Assistance APIs § 5 Shared infrastructure , and the common privacy and security considerations are discussed in Writing Assistance APIs § 6 Privacy considerations and Writing Assistance APIs § 7 Security considerations . Implementing these APIs requires implementing that shared infrastructure, and conforming to those privacy and security considerations. But it does not require implementing or exposing the actual writing assistance APIs. [WRITING-ASSISTANCE-APIS]
3. The API
// The return type from prompt() method and those alike.typedef (DOMString or sequence <LanguageModelMessageContent >); [LanguageModelPromptResult Exposed =Window ,SecureContext ]interface :LanguageModel EventTarget {static Promise <LanguageModel >create (optional LanguageModelCreateOptions = {});options static Promise <Availability >availability (optional LanguageModelCreateCoreOptions = {}); // **EXPERIMENTAL**: Only available in extension and experimental contexts.options static Promise <LanguageModelParams ?>();params // These will throw "NotSupportedError" DOMExceptions if role = "system" (// These will throw a TypeError if role = "system"Promise <LanguageModelPromptResult >prompt (LanguageModelPrompt ,input optional LanguageModelPromptOptions = {} );options ReadableStream promptStreaming (LanguageModelPrompt ,input optional LanguageModelPromptOptions = {} );options Promise <undefined >append (LanguageModelPrompt ,input optional LanguageModelAppendOptions = {} );options Promise <double >measureContextUsage (LanguageModelPrompt ,input optional LanguageModelPromptOptions = {} );options readonly attribute double contextUsage ;readonly attribute unrestricted double contextWindow ;attribute EventHandler oncontextoverflow ; // **DEPRECATED**: This method is only available in extension contexts.Promise <double >measureInputUsage (LanguageModelPrompt ,input optional LanguageModelPromptOptions = {} ); // **DEPRECATED**: This attribute is only available in extension contexts.options readonly attribute double inputUsage ; // **DEPRECATED**: This attribute is only available in extension contexts.readonly attribute unrestricted double inputQuota ; // **DEPRECATED**: This attribute is only available in extension contexts.attribute EventHandler onquotaoverflow ; // **DEPRECATED**: This attribute is only available in extension contexts.readonly attribute unsigned long topK ; // **DEPRECATED**: This attribute is only available in extension contexts.readonly attribute float temperature ; // **EXPERIMENTAL**: Only available in experimental contexts.readonly attribute LanguageModelSamplingMode ?samplingMode ;Promise <LanguageModel >clone (optional LanguageModelCloneOptions = {}); };options LanguageModel includes DestroyableModel ; // **DEPRECATED**: Only available in extension contexts. [Exposed =Window ,SecureContext ]interface {LanguageModelParams readonly attribute unsigned long ;defaultTopK readonly attribute unsigned long ;maxTopK readonly attribute float ;defaultTemperature readonly attribute float ; };maxTemperature );// A description of a tool call that a language model can invoke.{ ; ;// Note: When considering changes to this dictionary, authors should ensure // general alignment with ModelContextTool from WebMCP // (https://webmachinelearning.github.io/webmcp/#model-context-tool).dictionary {LanguageModelToolDeclaration required DOMString ;name required DOMString ; // JSON schema for the input parameters.description ; // The function to be invoked by user agent on behalf of language model. ;required object ; };inputSchema dictionary { // Note: these two have custom out-of-range handling behavior, not in the IDL layer. // They are unrestricted double so as to allow +Infinity without failing. // **DEPRECATED**: Only available in extension contexts.LanguageModelCreateCoreOptions unrestricted double ; // **DEPRECATED**: Only available in extension contexts.topK unrestricted double ; // **EXPERIMENTAL**: Only available in experimental contexts.temperature LanguageModelSamplingMode ; // The expected types and languages for the session.samplingMode ; ;sequence <LanguageModelExpected >;expectedInputs sequence <LanguageModelExpected >; // Tools that the language model can use. // **EXPERIMENTAL**: Only available in experimental contexts.expectedOutputs ;sequence <LanguageModelToolDeclaration >= []; };tools dictionary :LanguageModelCreateOptions LanguageModelCreateCoreOptions {AbortSignal ;signal CreateMonitorCallback ;monitor ;sequence <LanguageModelMessage >= []; };initialPrompts dictionary {LanguageModelPromptOptions object ;responseConstraint boolean =omitResponseConstraintInput false ;AbortSignal ; };signal dictionary {LanguageModelAppendOptions AbortSignal ; };signal dictionary {LanguageModelCloneOptions AbortSignal ; };signal dictionary {LanguageModelExpected required LanguageModelMessageType ;type ;sequence <DOMString >; }; // The argument to the prompt() method and others like itlanguages typedef (>sequence <LanguageModelMessage > // Shorthand for `[{ role: "user", content: [{ type: "text", value: providedValue }] }]`or DOMString );LanguageModelPrompt dictionary {LanguageModelMessage required LanguageModelMessageRole ; // The DOMString branch is shorthand for `[{ type: "text", value: providedValue }]`role ;required (DOMString or sequence <LanguageModelMessageContent >);content boolean =prefix false ; };dictionary {LanguageModelMessageContent required LanguageModelMessageType ;type required LanguageModelMessageValue ; };value enum {LanguageModelSamplingMode ,"most-predictable" ,"predictable" ,"slightly-predictable" ,"balanced" ,"slightly-creative" ,"creative" };"most-creative" enum {LanguageModelMessageRole ,"system" ,"user" };"assistant" enum {LanguageModelMessageType ,"text" ,"image" ,"audio" ,"tool-call" };"tool-response" typedef (ImageBitmapSource or AudioBuffer or BufferSource or DOMString or LanguageModelToolCall or LanguageModelToolResponse ); // The definitions of `LanguageModelToolCall` and `LanguageModelToolResponse` valuesLanguageModelMessageValue enum {LanguageModelToolResultType ,"text" ,"image" ,"audio" };"object" dictionary {LanguageModelToolResultContent required LanguageModelToolResultType ;type required any ; }; // Represents a tool call requested by the language model. [value Exposed =Window ,SecureContext ]interface {LanguageModelToolCall constructor (LanguageModelToolCallInit );init readonly attribute DOMString callId ;readonly attribute DOMString name ;readonly attribute object ?arguments ; };dictionary {LanguageModelToolCallInit required DOMString ;callId required DOMString ;name object ; }; [arguments Exposed =Window ,SecureContext ]interface {LanguageModelToolSuccess constructor (LanguageModelToolSuccessInit );init readonly attribute DOMString callId ;readonly attribute DOMString name ;readonly attribute FrozenArray <LanguageModelToolResultContent >result ; };dictionary {LanguageModelToolSuccessInit required DOMString ;callId required DOMString ;name required sequence <LanguageModelToolResultContent >; }; [result Exposed =Window ,SecureContext ]interface {LanguageModelToolError constructor (LanguageModelToolErrorInit );init readonly attribute DOMString callId ;readonly attribute DOMString name ;readonly attribute DOMString errorMessage ; };dictionary {LanguageModelToolErrorInit required DOMString ;callId required DOMString ;name required DOMString ; }; // The response from executing a tool call - either success or error.errorMessage typedef (LanguageModelToolSuccess or LanguageModelToolError );LanguageModelToolResponse
3.1. Creation
create(
options
)
method
steps
are:
-
Return the result of creating an AI model object given options , "
language-model", validate and canonicalize language model options , compute language model options availability , download the language model , initialize the language model , create a language model object , and false.
LanguageModelCreateCoreOptions
options
,
perform
the
following
steps.
They
mutate
options
in
place
to
canonicalize
and
deduplicate
language
tags,
and
throw
an
exception
if
any
are
invalid.
-
If options ["
samplingMode"] exists and either options ["topK"] exists or options ["temperature"] exists , then throw aTypeError. -
If options ["
expectedInputs"] exists , then for each expected of options ["expectedInputs"]:-
If expected ["
languages"] exists , thenValidatevalidate and canonicalize language tags given expected and "languages".
-
-
Let hasToolCallInExpectedOutputs be false.
If options ["
expectedOutputs"] exists , then for each expected of options ["expectedOutputs"]:-
If options ["
tools"] is not empty , then:If hasToolCallInExpectedOutputs is false, then throw a
TypeError.Let toolNames be an empty ordered set of strings .
For each tool of options ["
tools"]:If tool ["
name"] is the empty string , then throw aTypeError.If toolNames contains tool ["
name"], then throw aTypeError.If tool ["
description"] is the empty string , then throw aTypeError.Let schema be tool ["
inputSchema"].Let typeValue be ? Get \( schema , "type").
If typeValue is not "
object", then throw aTypeError.Let propertiesValue be ? Get \( schema , "properties").
If propertiesValue is not undefined and propertiesValue is not an Object , then throw a
TypeError.Let requiredValue be ? Get \( schema , "required").
If requiredValue is not undefined and ? IsArray ( requiredValue ) is false, then throw a
TypeError.Perform ? serialize a JavaScript value to a JSON string given schema .
If options ["
initialPrompts"] exists and is not empty , then:-
Let expectedInputs be options ["
expectedInputs"] if it exists ; otherwise an empty list . -
Let expectedInputTypes be the result of get the expected content types given expectedInputs .
-
Perform validating and canonicalizing a prompt given options ["
initialPrompts"], expectedInputTypes , and false.
-
LanguageModelCreateCoreOptions
options
:
-
Assert : these steps are running in parallel .
-
Initiate the download process for everything the user agent needs to prompt a language model according to options . This could include a base AI model, fine-tunings for specific languages or option values, or other resources.
-
If the download process cannot be started for any reason, then return false.
-
Return true.
LanguageModelCreateOptions
options
:
-
Assert : these steps are running in parallel .
-
Let availability be the result of compute language model options availability given options .
-
If availability is null or
unavailable, then return a DOMException error information whose name is "NotSupportedError" and whose details contain appropriate detail.
-
-
Perform any necessary initialization operations for the AI model backing the user agent’s prompting capabilities.
This could include loading the appropriate model and any fine-tunings necessary to support options into memory.
-
If options ["
initialPrompts"]existsis not empty , then:-
Let expectedInputs be options ["
expectedInputs"] if it exists ; otherwise an empty list . -
Let expectedInputTypes be the result of get the expected content types given expectedInputs .
-
Let initialMessages be the result of validating and canonicalizing a prompt given options ["
initialPrompts"], expectedInputTypes , and false. -
Load initialMessages into the model’s context window.
-
-
If options ["
tools"]existsis not empty , then load options ["tools"] into the model’s context window.
-
-
If initialization failed because the process of loading options resulted in using up all of the model’s context window, then:
-
Let requested be the amount of context window needed to encode options . The encoding of options as input is implementation-defined .
-
Let maximum be the maximum context window size that the user agent supports.
-
Assert : requested is greater than maximum . (That is how we reached this error branch.)
-
Return a quota exceeded error information whose requested is requested and quota is maximum .
-
-
If initialization failed for any other reason, then return a DOMException error information whose name is "
OperationError" and whose details contain appropriate detail. -
Return null.
LanguageModelCreateOptions
options
:
-
Assert : these steps are running on realm ’s surrounding agent ’s event loop .
-
Let contextWindowSize be the amount of context window that is available to the user agent for this model. (This value is implementation-defined , and may be +∞ if there are no specific limits beyond, e.g., the user’s memory, or the limits of JavaScript strings.)
-
Let initialMessages be an empty list of
LanguageModelMessages. -
Let
initialMessagesUsagetools be options ["tools"]. Let initialContextUsage be 0.
-
If options ["
initialPrompts"] exists and is not empty , then:-
Let expectedInputs be options ["
expectedInputs"] if it exists ; otherwise an empty list . -
Let expectedInputTypes be the result of get the expected content types given expectedInputs .
-
Set initialMessages to the result of validating and canonicalizing a prompt given options ["
initialPrompts"], expectedInputTypes , and false.
-
-
If initialMessages is not empty or tools is not empty , then:
-
Set
initialMessagesUsageinitialContextUsage to theresultamount ofmeasure language modelcontextusage givenwindow used to encode initialMessages,andoptions [" signal "].tools .
-
-
Return a new
LanguageModelobject, created in realm , with- initial messages
-
initialMessages
- top K
-
options ["
topK"] if it exists ; otherwise an implementation-defined value - temperature
-
options ["
temperature"] if it exists ; otherwise an implementation-defined value - sampling mode
-
options ["
samplingMode"] if it exists ; otherwise null if options ["topK"] exists or options ["temperature"] exists ; otherwise "balanced" - expected inputs
-
options ["
expectedInputs"] if it exists ; otherwise an empty list - expected outputs
-
options ["
expectedOutputs"] if it exists ; otherwise an empty list - tools
-
options ["tools"] if it exists ; otherwise an empty list - context window size
-
contextWindowSize
- current context usage
-
initialMessagesUsageinitialContextUsage
3.2. Availability
availability(
options
)
method
steps
are:
-
Return the result of computing AI model availability given options , "
language-model", validate and canonicalize language model options , and compute language model options availability .
LanguageModelCreateCoreOptions
options
,
perform
the
following
steps.
They
return
either
an
Availability
value
or
null,
and
they
mutate
options
in
place
to
update
language
tags
to
their
best-fit
matches.
-
Assert : this algorithm is running in parallel .
-
Let availability be the language model non-options availability .
-
If availability is null, then return null.
-
Let availabilities be a list containing availability .
-
Let inputPartition be the result of getting the language availabilities partition given the purpose of prompting a language model with text in that language.
-
Let outputPartition be the result of getting the language availabilities partition given the purpose of producing language model output in that language.
-
If options ["
expectedInputs"] exists , then for each expected of options ["expectedInputs"]:-
If expected ["
languages"] exists , then:-
Let inputLanguageAvailability be the result of computing language availability given expected ["
languages"] and inputPartition . -
Append inputLanguageAvailability to availabilities .
-
-
Let inputTypeAvailability be the language model content type availability given expected ["
type"] and true. -
Append inputTypeAvailability to availabilities .
-
-
If options ["
expectedOutputs"] exists , then for each expected of options ["expectedOutputs"]:-
If expected ["
languages"] exists , then:-
Let outputLanguageAvailability be the result of computing language availability given expected ["
languages"] and outputPartition . -
Append outputLanguageAvailability to availabilities .
-
-
Let outputTypeAvailability be the language model content type availability given expected ["
type"] and false. -
Append outputTypeAvailability to availabilities .
-
-
Return the minimum availability given availabilities .
Availability
value
or
null.
-
Assert : this algorithm is running in parallel .
-
If there is some error attempting to determine whether the user agent can support prompting a language model, which the user agent believes to be transient (such that re-querying could stop producing such an error), then return null.
-
If the user agent currently supports prompting a language model, then return "
available". -
If the user agent believes it will be able to support prompting a language model, but only after finishing a download that is already ongoing, then return "
downloading". -
If the user agent believes it will be able to support prompting a language model, but only after performing a not-currently-ongoing download, then return "
downloadable". -
Otherwise, return "
unavailable".
LanguageModelMessageType
type
and
a
boolean
isInput
,
is
given
by
the
following
steps.
They
return
an
Availability
value.
-
Assert : this algorithm is running in parallel .
-
If the user agent currently supports type as an input if isInput is true, or as an output if isInput is false, then return "
available". -
If the user agent believes it will be able to support type as such, but only after finishing a download that is already ongoing, then return "
downloading". -
If the user agent believes it will be able to support type as such, but only after performing a not-currently-ongoing download, then return "
downloadable". -
Otherwise, return "
unavailable".
3.3.
The
LanguageModel
class
Every
LanguageModel
has
an
initial
messages
,
a
list
of
LanguageModelMessage
s,
set
during
creation.
Every
LanguageModel
has
a
top
K
,
an
unsigned
long,
set
during
creation.
Every
LanguageModel
has
a
temperature
,
a
float,
set
during
creation.
Every
LanguageModel
has
a
sampling
mode
,
a
LanguageModelSamplingMode
or
null,
set
during
creation.
Every
LanguageModel
has
an
expected
inputs
,
a
list
of
LanguageModelExpected
s,
set
during
creation.
Every
LanguageModel
has
an
expected
outputs
,
a
list
of
LanguageModelExpected
s,
set
during
creation.
Every
LanguageModel
has
a
tools
,
a
list
of
s,
set
during
creation.
LanguageModelTool
LanguageModelToolDeclaration
Every
LanguageModel
has
a
context
window
size
,
an
unrestricted
double,
set
during
creation.
Every
LanguageModel
has
a
current
context
usage
,
a
double,
initially
0.
The
contextUsage
getter
steps
are
to
return
this
’s
current
context
usage
.
The
inputUsage
getter
steps
are
to
return
this
’s
current
context
usage
.
The
contextWindow
getter
steps
are
to
return
this
’s
context
window
size
.
The
inputQuota
getter
steps
are
to
return
this
’s
context
window
size
.
The
topK
getter
steps
are
to
return
this
’s
top
K
.
The
temperature
getter
steps
are
to
return
this
’s
temperature
.
The
samplingMode
getter
steps
are
to
return
this
’s
sampling
mode
.
The
following
are
the
event
handlers
(and
their
corresponding
event
handler
event
types
)
that
must
be
supported,
as
event
handler
IDL
attributes
,
by
all
LanguageModel
objects:
| Event handler | Event handler event type |
|---|---|
oncontextoverflow
|
contextoverflow
|
onquotaoverflow
|
quotaoverflow
|
prompt(
input
,
options
)
method
steps
are:
-
Let responseConstraint be options ["
responseConstraint"] if it exists ; otherwise null. -
Let omitResponseConstraintInput be options ["
omitResponseConstraintInput"]. -
Let hasNonTextExpectedOutput be false.
For each expected of this ’s expected outputs :
Let contents be an empty list of
LanguageModelMessageContents.Let text be the empty string .
Let operation be an algorithm step which takes arguments chunkProduced , done , error , and stopProducing , and performs the following steps:
-
Let prefillSuccess be the result of prefilling given this , input , omitResponseConstraintInput , responseConstraint , error , and stopProducing .
-
If prefillSuccess is
true,false, then return. If hasNonTextExpectedOutput is false:
generateGenerate given this , responseConstraint , chunkProduced , done , error , and stopProducing .-
Return.
-
ReturnLet onChunk be an algorithm step which takes argument chunk and performs the following steps: Let onDone be an algorithm step which takes no arguments and performs the following steps:
Generate given this , responseConstraint , onChunk , onDone , error , and stopProducing .
-
Let promise be the result of getting an aggregated AI model result given this , options , and operation .
-
If hasNonTextExpectedOutput is false, then return promise .
Return the result of reacting to promise with a fulfillment handler that returns contents .
promptStreaming(
input
,
options
)
method
steps
are:
-
Let responseConstraint be options ["
responseConstraint"] if it exists ; otherwise null. -
Let omitResponseConstraintInput be options ["
omitResponseConstraintInput"]. -
Let operation be an algorithm step which takes arguments chunkProduced , done , error , and stopProducing , and performs the following steps:
-
Let prefillSuccess be the result of prefilling given this , input , omitResponseConstraintInput , responseConstraint , error , and stopProducing .
-
If prefillSuccess is true, then generate given this , responseConstraint , chunkProduced , done , error , and stopProducing .
-
-
Return the result of getting a streaming AI model result given this , options , and operation .
append(
input
,
options
)
method
steps
are:
-
Let operation be an algorithm step which takes arguments chunkProduced , done , error , and stopProducing , and performs the following steps:
chunkProduced is never called because the prefilling algorithm does not generate chunks.
-
Let prefillSuccess be the result of prefilling given this , input , false, null, error , and stopProducing .
-
If prefillSuccess is true and done is not null, then perform done .
-
-
Return the result of getting an aggregated AI model result given this , options , and operation .
measureContextUsage(
input
,
options
)
method
steps
are:
-
If options ["
omitResponseConstraintInput"] is true and options ["responseConstraint"] does not exist , then throw a "TypeError"DOMException. -
Let expectedInputTypes be the result of get the expected content types given this ’s expected inputs .
-
Let messages be the result of validating and canonicalizing a prompt given input , expectedInputTypes , and false.
-
If options ["
responseConstraint"] exists and is not null and options ["omitResponseConstraintInput"] is false, then implementations may insert an implementation-definedLanguageModelMessageto messages to guide the model’s behavior. -
Let measureUsage be an algorithm step which takes argument stopMeasuring , and returns the result of measuring language model context usage given messages , and stopMeasuring .
-
Return the result of measuring AI model input usage given this , options , and measureUsage .
measureInputUsage(
input
,
options
)
method
steps
are:
-
Return the result of running the
measureContextUsage()method steps given input and options .
clone(
options
)
method
steps
are:
-
Return the result of cloning a language model given this and options .
3.3.1. Prefilling and generating
-
a
LanguageModelmodel , -
a
LanguageModelPromptinput , -
a boolean omitResponseConstraintInput ,
-
an object-or-null responseConstraint ,
-
an algorithm-or-null error that takes error information and returns nothing, and
-
an algorithm-or-null stopPrefilling that takes no arguments and returns a boolean,
perform the following steps:
-
Assert : this algorithm is running in parallel .
-
Let expectedInputTypes be the result of get the expected content types given model ’s expected inputs .
Let messages be the result of validating and canonicalizing a prompt given input , expectedInputTypes , and true if model ’s current context usage is greater than 0, otherwise false.
If this throws an exception e , then:
-
If error is not null, perform error given a DOMException error information whose name is e ’s name and whose details contain appropriate detail.
-
Return false.
-
-
If responseConstraint is not null and omitResponseConstraintInput is false, then implementations may insert an implementation-defined
LanguageModelMessageto messages to guide the model’s behavior. -
Let requested be the result of measuring language model context usage given messages , and stopPrefilling .
-
If requested is null, then return false.
-
If requested is an error information , then:
-
If error is not null, perform error given requested .
-
Return false.
-
-
Assert : requested is a number.
-
If model ’s current context usage + requested is greater than model ’s context window size , then:
-
If error is not null, then:
-
Let errorInfo be a quota exceeded error information with a requested of model ’s current context usage + requested and a quota of model ’s context window size .
-
Perform error given errorInfo .
-
-
Return false.
-
-
Let expectedInputTypes be the result of get the expected content types given model ’s expected inputs .In an implementation-defined manner, update the underlying model’s internal state to include messages .The process should use model ’s initial messages , model ’s sampling mode , model ’s top K , model ’s temperature , model ’s expected inputs , model ’s expected outputs , and model ’s tools to guide how the state is updated.
The process must conform to the guidance given in § 4 Privacy considerations and § 5 Security considerations .
If during this process stopPrefilling returns true, then return false.
If an error occurred during prefilling:
-
Let the error be represented as error information errorInfo according to the guidance in § 3.3.4 Errors .
-
If error is not null, perform error given errorInfo .
-
Return false.
-
-
Set model ’s current context usage to model ’s current context usage + requested .
-
Return true.
-
a
LanguageModelmodel , -
an object-or-null responseConstraint ,
-
an algorithm-or-null chunkProduced that takes a string or a
LanguageModelMessageContentand returns nothing, -
an algorithm-or-null done that takes no arguments and returns nothing,
-
an algorithm-or-null error that takes error information and returns nothing, and
-
an algorithm-or-null stopProducing that takes no arguments and returns a boolean,
perform the following steps:
-
Assert : this algorithm is running in parallel .
-
In an implementation-defined manner, subject to the following guidelines, begin the process of producing a response from the language model based on its current internal state.
The process should use model ’s initial messages , model ’s sampling mode , model ’s top K , model ’s temperature , model ’s expected inputs , model ’s expected outputs , model ’s tools , and responseConstraint to guide the model’s behavior.
The prompting process must conform to the guidance given in § 4 Privacy considerations and § 5 Security considerations .
If model ’s tools is not
empty,empty , the model mayuseproduce one or more tool calls based on theprovideddeclared toolsby calling theirinexecutemodelfunctions.’s tools , in addition to or instead of text. -
While true:
-
Wait for the next chunk of response data (text or a tool call) to be produced, for the process to finish, or for the result of calling stopProducing to become true.
-
If
sucha text chunk is successfully produced:-
Let it be represented as a string chunk .
-
If chunkProduced is not null, perform chunkProduced given chunk .
-
-
Otherwise, if a tool call is successfully produced:
Let callId be an implementation-defined non-empty string identifying the tool call.
Let name be a string representing the name of the tool being called.
Let arguments be an Object representing the JSON object of arguments for the tool call, created in model ’s relevant realm .
Let toolCall be a new
LanguageModelToolCallcreated in model ’s relevant realm with call ID set to callId , name set to name , and arguments set to arguments .Let toolCallContent be a
LanguageModelMessageContentinitialized with «[ "type" → "tool-call", "value" → toolCall ]» .If chunkProduced is not null, perform chunkProduced given toolCallContent .
Otherwise, if the process has finished:
-
In an implementation-defined manner, update the underlying model’s internal state and model ’s current context usage to include the generated response (both text and any tool calls).
If done is not null, perform done .
-
Break .
-
-
Otherwise, if stopProducing returns true, then break .
-
Otherwise, if an error occurred during prompting:
-
Let the error be represented as error information errorInfo according to the guidance in § 3.3.4 Errors .
-
If error is not null, perform error given errorInfo .
-
Break .
-
-
3.3.2. Usage
-
a list of
LanguageModelMessagemessages , -
an algorithm stopMeasuring that takes no arguments and returns a boolean,
perform the following steps:
-
Assert : this algorithm is running in parallel .
-
Let inputToModel be the implementation-defined input that would be sent to the underlying model in order to prefill given messages .
This will generally consist of the encoding of all of the inputs, possibly with prompt engineering or other implementation-defined wrappers.
If during this process stopMeasuring starts returning true, then return null.
If an error occurs during this process, then return an appropriate DOMException error information according to the guidance in § 3.3.4 Errors .
-
Return the amount of context usage needed to represent inputToModel when given to the underlying model. The exact calculation procedure is implementation-defined , subject to the following constraints.
The returned context usage must be nonnegative and finite. It should be roughly proportional to the amount of data in inputToModel .
This might be the number of tokens needed to represent the input in a language model tokenization scheme , or it might be related to the size of the data in bytes.
If during this process stopMeasuring starts returning true, then instead return null.
If an error occurs during this process, then instead return an appropriate DOMException error information according to the guidance in § 3.3.4 Errors .
3.3.3. Options
LanguageModelExpected
s
expectedContents
:
LanguageModelPrompt
input
,
a
list
of
LanguageModelMessageType
s
expectedTypes
,
and
a
boolean
hasAppendedInput
,
perform
the
following
steps.
The
return
value
will
be
a
non-empty
list
of
LanguageModelMessage
s
in
their
"longhand"
form.
-
If input is a string , then return « «[ "
role" → "user", "content" → « «[ "type" → "text", "value" → input ]» », "prefix" → false ]» » . -
Assert : input is a list of
LanguageModelMessages. -
If input is an empty list , then return « «[ "
role" → "user", "content" → « «[ "type" → "text", "value" → "" ]» », "prefix" → false ]» » . -
Let messages be an empty list of
LanguageModelMessages. -
For each message of input :
-
If message ["
content"] is a string , then set message to «[ "role" → message ["role"], "content" → « «[ "type" → "text", "value" → message ["content"] ]» », "prefix" → message ["prefix"] ]» . -
If message ["
prefix"] is true, then:-
If message ["
role"] is not "assistant", then throw a "SyntaxError"DOMException. -
If message is not the last item in
messagesinput , then throw a "SyntaxError"DOMException.
-
-
If message ["
role"] is "system", then:-
If hasAppendedInput is true, then throw a
"TypeError." DOMException
-
-
For each content of message ["
content"]:-
If content ["
type"] is "tool-call" and message ["role"] is not "assistant", then throw aTypeError. If content ["
type"] is "tool-response" and message ["role"] is not "user", then throw aTypeError.If message ["
role"] is "assistant" and content ["type"] isnot"" or "textimageaudio", then throw a "NotSupportedError"DOMException.-
If content ["
type"] is "text" and content ["value"] is not a string , then throw a"TypeError." DOMException -
If content ["
type"] is "image", then:-
If expectedTypes does not contain "
image", then throw a "NotSupportedError"DOMException. -
If content ["
value"] is not anImageBitmapSourceorBufferSource, then throw aTypeError.
-
If content ["
type"] is "audio", then:If expectedTypes does not contain "
audio", then throw a "NotSupportedError"DOMException.If content ["
value"] is not anAudioBuffer,BufferSource, orBlob, then throw aTypeError.
If content ["
type"] is "tool-call", then:If expectedTypes does not contain "
tool-call", then throw a "NotSupportedError"DOMException.If content ["
value"] is not aLanguageModelToolCall, then throw aTypeError.If arguments is not null, then:
If ? IsArray ( arguments ) is true, or arguments is a platform object , or arguments cannot be serialized to a JSON object (for example, due to circular references or non-JSON-serializable values such as functions or BigInts), then throw a "
DataError"DOMException.
-
If content ["
type"] is "", then:audiotool-response-
If expectedTypes does not contain "
", then throw a "audiotool-responseNotSupportedError"DOMException. -
If content ["
value"] is not aLanguageModelToolResponse, then throw aTypeError. If content ["
value"] is aLanguageModelToolSuccess, then for each resultItem of content ["value"]'s result :If resultItem ["
type"] is "image" or "audio" and the user agent does not support multimodal tool result content, then throw a "NotSupportedError"DOMException.If resultItem ["
type"] is "text" and resultItem ["value"] is not a string , then throw aTypeError.If resultItem ["
type"] is "image" and resultItem ["value"] is not anImageBitmapSourceorBufferSource, then throw aTypeError.If resultItem ["
type"] is "audio" and resultItem ["value"] is not anAudioBuffer,BufferSource, orBlob, then throw aTypeError.If resultItem ["
type"] is "object", then:If resultItem ["
value"] is not an Object , then throw aTypeError.-
If resultItem ["
value"] is a platform object , or cannot be serialized to JSON (for example, due to circular references or non-JSON-serializable values such as functions or BigInts), then throw a "DataError"DOMException.
-
-
-
Let contentWithContiguousTextCollapsed be an empty list of
LanguageModelMessageContents. -
Let lastTextContent be null.
-
For each content of message ["
content"]:-
If content ["
type"] is "text":-
If lastTextContent is null:
-
Append content to contentWithContiguousTextCollapsed .
-
Set lastTextContent to content .
-
-
Otherwise, set lastTextContent ["
value"] to the concatenation of lastTextContent ["value"] and content ["value"].No space or other character is added. Thus, « «[ "
type" → "text", "foo" ]», «[ "type" → "text", "bar" ]» » is canonicalized to « «[ "type" → "text", "foobar" ]».
-
-
Otherwise:
-
Append content to contentWithContiguousTextCollapsed .
-
Set lastTextContent to null.
-
-
Set message ["
content"] to contentWithContiguousTextCollapsed .
-
-
Append message to messages .
-
Set hasAppendedInput to true.
-
-
If messages is empty , then throw a "
SyntaxError"DOMException. -
Return messages .
3.3.4. Errors
When
prompting
fails,
the
following
possible
reasons
may
be
surfaced
to
the
web
developer.
This
table
lists
the
possible
DOMException
names
and
the
cases
in
which
an
implementation
should
use
them:
DOMException
name
|
Scenarios |
|---|---|
"
NotAllowedError
"
|
Prompting is disabled by user choice or user agent policy. |
"
NotReadableError
"
|
The model output was filtered by the user agent, e.g., because it was detected to be harmful, inaccurate, or nonsensical. |
"
NotSupportedError
"
|
The
input
to
be
processed
was
in
a
language
that
the
user
agent
does
not
support,
or
was
not
provided
properly
in
the
call
to
The model output ended up being in a language that the user agent does not support (e.g., because the user agent has not performed sufficient quality control tests on that output language). |
"
UnknownError
"
|
All other scenarios, including if the user agent believes it cannot prompt the model and also meet the requirements given in § 4 Privacy considerations or § 5 Security considerations . Or, if the user agent would prefer not to disclose the failure reason. |
This table does not give the complete list of exceptions that can be surfaced by the prompt API. It only contains those which can come from certain implementation-defined steps.
LanguageModel
model
and
a
LanguageModelCloneOptions
options
:
-
Let global be model ’s relevant global object .
-
If global ’s associated Document is not fully active , then return a promise rejected with an "
InvalidStateError"DOMException. -
Let signals be « model ’s destruction abort controller ’s signal ».
-
If options ["
signal"] exists , then append it to signals . -
Let compositeSignal be the result of creating a dependent abort signal given signals using
AbortSignaland model ’s relevant realm . -
If compositeSignal is aborted , then return a promise rejected with compositeSignal ’s abort reason .
-
Let signal be options ["
signal"] if it exists ; otherwise null. -
If signal is not null and is aborted , then return a promise rejected with signal ’s abort reason .
-
Let promise be a new promise created in model ’s relevant realm .
-
Let abortedDuringOperation be false.
This variable will be written to from the event loop , but read from in parallel .
-
Add the following abort steps to compositeSignal :
-
Set abortedDuringOperation to true.
-
Reject promise with compositeSignal ’s abort reason .
-
-
-
Queue a global task on the AI task source to perform the following steps:
-
If abortedDuringOperation is true, then return.
-
Let clonedModel be a new
LanguageModelobject with:-
initial messages set to model ’s initial messages .
-
temperature set to model ’s temperature .
-
sampling mode set to model ’s sampling mode .
-
expected inputs set to model ’s expected inputs .
-
expected outputs set to model ’s expected outputs .
-
context window size set to model ’s context window size .
-
current context usage set to model ’s current context usage .
-
-
In an implementation-defined manner, copy any other state from model to clonedModel .
-
If the copy operation fails:
-
Reject promise with a "
OperationError"DOMException. -
Return.
-
-
Resolve promise with clonedModel .
-
-
-
Return promise .
3.4.
The
LanguageModelToolCall
class
Every
LanguageModelToolCall
has
a
call
ID
,
a
string
,
set
during
creation.
Every
LanguageModelToolCall
has
a
name
,
a
string
,
set
during
creation.
Every
LanguageModelToolCall
has
an
arguments
,
an
Object
or
null,
set
during
creation.
new
LanguageModelToolCall(
init
)
constructor
steps
are:
The
callId
getter
steps
are
to
return
this
’s
call
ID
.
The
name
getter
steps
are
to
return
this
’s
name
.
The
arguments
getter
steps
are
to
return
this
’s
arguments
.
3.5.
The
LanguageModelToolSuccess
class
Every
LanguageModelToolSuccess
has
a
call
ID
,
a
string
,
set
during
creation.
Every
LanguageModelToolSuccess
has
a
name
,
a
string
,
set
during
creation.
Every
LanguageModelToolSuccess
has
a
result
,
a
,
set
during
creation.
FrozenArray
<
LanguageModelToolResultContent
>
new
LanguageModelToolSuccess(
init
)
constructor
steps
are:
The
callId
getter
steps
are
to
return
this
’s
call
ID
.
The
name
getter
steps
are
to
return
this
’s
name
.
The
result
getter
steps
are
to
return
this
’s
result
.
3.6.
The
LanguageModelToolError
class
Every
LanguageModelToolError
has
a
call
ID
,
a
string
,
set
during
creation.
Every
LanguageModelToolError
has
a
name
,
a
string
,
set
during
creation.
Every
LanguageModelToolError
has
an
error
message
,
a
string
,
set
during
creation.
new
LanguageModelToolError(
init
)
constructor
steps
are:
Set this ’s error message to init ["
errorMessage"].
The
callId
getter
steps
are
to
return
this
’s
call
ID
.
The
name
getter
steps
are
to
return
this
’s
name
.
The
errorMessage
getter
steps
are
to
return
this
’s
error
message
.
3.7. Permissions policy integration
Access
to
the
prompt
API
is
gated
behind
the
policy-controlled
feature
"
language-model
",
which
has
a
default
allowlist
of
'self'
.
4. Privacy considerations
Please see Writing Assistance APIs § 6 Privacy considerations for a discussion of privacy considerations for the prompt API. That text was written to apply to all APIs sharing the same infrastructure, as noted in § 2 Dependencies .
5. Security considerations
Please see Writing Assistance APIs § 7 Security considerations for a discussion of security considerations for the prompt API. That text was written to apply to all APIs sharing the same infrastructure, as noted in § 2 Dependencies .