Skip to content

Add RegExp constraints and change structured output API #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,17 @@ Details:

Future extensions may include more ambitious multimodal inputs, such as video clips, or realtime audio or video. (Realtime might require a different API design, more based around events or streams instead of messages.)

### Structured output or JSON output
### Structured output with JSON schema or RegExp constraints

To help with programmatic processing of language model responses, the prompt API supports structured outputs defined by a JSON schema.
To help with programmatic processing of language model responses, the prompt API supports constraining the response with either a JSON schema object or a `RegExp` passed as the `responseConstraint` option:

```js
const session = await LanguageModel.create();

const responseJSONSchema = {
const schema = {
type: "object",
required: ["Rating"],
additionalProperties: false,
properties: {
Rating: {
rating: {
type: "number",
minimum: 0,
maximum: 5,
Expand All @@ -292,14 +290,31 @@ const responseJSONSchema = {
// Prompt the model and wait for the JSON response to come back.
const result = await session.prompt("Summarize this feedback into a rating between 0-5: "+
"The food was delicious, service was excellent, will recommend.",
{ responseJSONSchema }
{ responseConstraint: schema }
);
console.log(result);

const { rating } = JSON.parse(result);
console.log(rating);
```

If the input value is a valid JSON schema object, but uses JSON schema features not supported by the user agent, the method will error with a `"NotSupportedError"` `DOMException`.

The result value returned is a string that can be parsed with `JSON.parse()`. If the user agent is unable to produce a response that is compliant with the schema, the method will error with a `"SyntaxError"` `DOMException`.

```js
const emailRegExp = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;

const emailAddress = await session.prompt(
`Create a fictional email address for ${characterName}.`,
{ responseConstraint: emailRegExp }
);

console.log(emailAddress);
```

While processing the JSON schema, in cases where the user agent detects unsupported schema, a `"NotSupportedError"` `DOMException` will be raised with an appropriate error message.
The returned value will be a string that matches the input `RegExp`. If the user agent is unable to produce a response that matches, the method will error with a `"SyntaxError"` `DOMException`.

The result value returned is a string that can be parsed with `JSON.parse()`. If the user agent is unable to produce a response that is compliant with the schema, a `"SyntaxError"` `DOMException` will be raised.
If a value that is neither a `RegExp` object or a valid JSON schema object is given, the method will error with a `TypeError`.

### Appending messages without prompting for a response

Expand Down Expand Up @@ -692,7 +707,7 @@ dictionary LanguageModelCreateOptions : LanguageModelCreateCoreOptions {
};

dictionary LanguageModelPromptOptions {
object responseJSONSchema;
object responseConstraint;
AbortSignal signal;
};

Expand Down