LanguageModel: promptStreaming() method
Prompt API
Prompt API
The **`promptStreaming()`** method of the LanguageModel interface sends input to the language model and returns a ReadableStream that delivers the model's response incrementally as it is generated.
This is useful for displaying responses to users incrementally for outputs that take a long time to complete, or for any scenario where perceived latency should be minimized. Consume the stream using `for await...of` or by attaching a reader via ReadableStream.getReader().
## Syntax
```js-nolint
promptStreaming(input)
promptStreaming(input, options)
```
### Parameters
- `input`
- : The content to prompt the model with. This is either:
- A string — Shorthand for a single textual message.
- An array of objects, each representing a single message in a conversation with a language model.
Objects may have the following properties:
- `role`
- : A string indicating the point of view the message is phrased from. Must be one of:
- `system`
- : A system-level instruction that guides the model's overall behavior. This must be the first instruction passed to the model.
- `user`
- : A message from the user, which the API should respond to.
- `assistant`
- : An input that provides context for the AI assistant, such as its persona or the format of its responses. Such messages mainly serve to provide context/history, and further shape how the model responds.
- `content`
- : A string representing a textual prompt, or an array of objects. Each object includes the following properties:
- `type`
- : An enumerated value representing the type of content. This can be one of:
- `audio`
- : Audio content.
- `image`
- : Image content.
- `text`
- : Textual content.
- `tool-call`
- : A tool invocation issued by the model.
- `tool-response`
- : The result of a tool invocation.
- `value`
- : The content of the message. If the `type` is `text`, this is always a string. If the `type` is `audio` or `image`, the `value` can be one of several different object types; see [What data types are accepted?](/docs/Web/API/Prompt_API/Multimodal#what_data_types_are_accepted).
- `prefix`
- : A boolean, defaulting to `false`. When `true`, the message is treated as a prefix for the model's next generated response rather than a complete turn.
- `options`
- : Options for creating a prompt. Properties include:
- `responseConstraint`
- : An object following the structure defined by [JSON Schema](https://json-schema.org/) defining the precise format the model's output should be delivered in. When provided and `omitResponseConstraintInput` is `false`, any implementation-defined constraint-description message is included in the measurement.
- `omitResponseConstraintInput`
- : A boolean; when `true`, the automatic constraint-description message is excluded from the measurement.
- `signal`
- : An AbortSignal to cancel the operation.
### Return value
A ReadableStream of String chunks. Each chunk represents a piece of the model's response as it is generated. The stream closes when generation completes.
### Exceptions
Errors are surfaced as stream errors rather than as rejected promises. Consumers should handle errors using a stream's standard error-handling mechanisms.
- `AbortError` DOMException
- : Thrown if the operation was canceled via the `signal` option.
- `NotAllowedError` DOMException
- : Thrown if usage of the method is blocked by a language-model Permissions-Policy.
- `NotSupportedError` DOMException
- : Thrown if:
- A message's `role` is `assistant` and its `type` is anything other than `text`.
- A message's `type` is `text` and its `value` is not a string.
- The input or output text is in a language the user agent doesn't support for prompting.
- A message's `type` is `image` or `audio` but the type was not listed in `expectedInputs`, or the `value` is not an [accepted data type](/docs/Web/API/Prompt_API/Multimodal#what_data_types_are_accepted).
- `OperationError` DOMException
- : Thrown if the prompt fails for any other reason not listed in the other exception types.
- `QuotaExceededError` DOMException
- : Thrown if the prompt would cause the session's context usage to exceed the model's LanguageModel.contextWindow.
- `SyntaxError` DOMException
- : Thrown if:
- No messages are included in the messages array.
- A message's `prefix` property is set to `true` and:
- The message's `role` is not `assistant`.
- The message is not the last item in the messages array.
- TypeError
- : Thrown if:
- `omitResponseConstraintInput` is `true` but `responseConstraint` is not provided.
- A message's `role` is `system` but it was not the first message passed to the context.
## Description
The `promptStreaming()` method adds the provided input to the context window and generates a response. The entire response is receives incrementally as a ReadableStream.
To receive the response as one complete string, use LanguageModel.prompt() instead. To add content to the context window without generating a response, use LanguageModel.append().
Each call to `promptStreaming()` adds to the session's context. To branch from a given state without affecting the original session, call LanguageModel.clone().
## Examples
### Streaming a response to the page
This example writes out chunks from a `promptStreaming()` call's ReadableStream as they arrive.
```js
const session = await LanguageModel.create();
const output = document.querySelector("#output");
const stream = session.promptStreaming("Write a short poem about the ocean.");
for await (const chunk of stream) {
output.textContent += chunk;
}
```
See also [Using the Prompt API > Complete streaming example](/docs/Web/API/Prompt_API/Using#complete_streaming_example).
### Streaming with an abort signal
This example shows how to use an AbortController with `promptStreaming()`.
```js
const controller = new AbortController();
document
.querySelector("#stop")
.addEventListener("click", () => controller.abort());
const stream = session.promptStreaming("Tell me a long story.", {
signal: controller.signal,
});
try {
for await (const chunk of stream) {
output.textContent += chunk;
}
} catch (err) {
if (err.name === "AbortError") {
console.log("Streaming was stopped by the user.");
}
}
```
### Collecting streamed chunks into a single string
In this example, chunks from a ReadableStream are collected before the whole stream is written out.
```js
const session = await LanguageModel.create();
const stream = session.promptStreaming("Explain quantum entanglement.");
const chunks = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
const fullResponse = chunks.join("");
console.log(fullResponse);
```
## Specifications
## Browser compatibility
## See also
- LanguageModel.prompt()
- ReadableStream
- [Prompt API](/docs/Web/API/Prompt_API)
- [Using the Prompt API](/docs/Web/API/Prompt_API/Using)
- [Adding context with initial and ongoing prompt inputs](/docs/Web/API/Prompt_API/Adding_context)
Cite this
"LanguageModel: promptStreaming() method". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/LanguageModel/promptStreaming JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy