commit 37af7f38bfbb71198b7419f75e718ef9d802b527
parent 5a24424d151c1b1e4a72afb79b145ea98ebaf066
Author: Isaac Ahouma <iahouma@google.com>
Date: Sat, 22 Nov 2025 21:12:05 +0000
Bug 2001451 [wpt PR 56155] - [Prompt API WPTs] Update JSON schema tests, a=testonly
Automatic update from web-platform-tests
[Prompt API WPTs] Update JSON schema tests
This change updates the tests for valid JSON schemas to check that the
response is a valid JSON string conforming to the provided schema. The
test involving a prefix is also adjusted to correctly parse the combined
prefix and model output.
This cl also adds an entry for this test file in the web tests
TestExpectation file to indicate that the tests are expected to fail
when run in content shell. See crbug.com/461860505 for more details on
why this is needed.
Bug: 460824429
Change-Id: Ibe0f4e1a6a12960b8645e2fc2c4c08f4fb893a98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7163336
Reviewed-by: Mike Wasserman <msw@chromium.org>
Commit-Queue: Isaac Ahouma <iahouma@google.com>
Reviewed-by: Nathan Memmott <memmott@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1548030}
--
wpt-commits: 24689f1deaa41349792eb7f40cf8e449a0a5f81d
wpt-pr: 56155
Diffstat:
1 file changed, 57 insertions(+), 56 deletions(-)
diff --git a/testing/web-platform/tests/ai/language-model/language-model-response-json-schema.tentative.https.window.js b/testing/web-platform/tests/ai/language-model/language-model-response-json-schema.tentative.https.window.js
@@ -5,86 +5,87 @@
'use strict';
+const kValidResponseSchema = {
+ type: 'object',
+ required: ['Rating'],
+ additionalProperties: false,
+ properties: {
+ Rating: {
+ type: 'number',
+ minimum: 0,
+ maximum: 5,
+ },
+ },
+};
+
+function testResponseJsonSchema(response, t) {
+ let jsonResponse;
+ try {
+ jsonResponse = JSON.parse(response);
+ } catch (e) {
+ assert_unreached(
+ `Response is not valid JSON: "${response}". Error: ${e.message}`);
+ return;
+ }
+ assert_equals(typeof jsonResponse, 'object', 'Response should be an object');
+ assert_own_property(
+ jsonResponse, 'Rating', 'JSON response should have a "Rating" property.');
+ assert_equals(
+ typeof jsonResponse.Rating, 'number', 'Rating should be a number');
+ assert_greater_than_equal(jsonResponse.Rating, 0, 'Rating should be >= 0');
+ assert_less_than_equal(jsonResponse.Rating, 5, 'Rating should be <= 5');
+}
+
promise_test(async t => {
await ensureLanguageModel();
const session = await createLanguageModel();
// Circular reference is not valid.
- const invalidRepsonseJsonSchema = {};
- invalidRepsonseJsonSchema.self = invalidRepsonseJsonSchema;
- await promise_rejects_dom(t, 'NotSupportedError',
- session.prompt(kTestPrompt, { responseConstraint: invalidRepsonseJsonSchema }),
- 'Response json schema is invalid - it should be an object that can be stringified into a JSON string.');
+ const invalidResponseJsonSchema = {};
+ invalidResponseJsonSchema.self = invalidResponseJsonSchema;
+ await promise_rejects_dom(
+ t, 'NotSupportedError',
+ session.prompt(
+ kTestPrompt, {responseConstraint: invalidResponseJsonSchema}),
+ 'Response json schema is invalid - it should be an object that can be stringified into a JSON string.');
}, 'Prompt API should fail if an invalid response json schema is provided');
promise_test(async t => {
await ensureLanguageModel();
const session = await createLanguageModel();
- const validRepsonseJsonSchema = {
- type: "object",
- required: ["Rating"],
- additionalProperties: false,
- properties: {
- Rating: {
- type: "number",
- minimum: 0,
- maximum: 5,
- },
- },
- };
- const promptPromise = session.prompt('hello', { responseConstraint : validRepsonseJsonSchema });
- // Both the prompt and schema should be present.
- assert_regexp_match(await promptPromise, /hello.*Rating/s);
+ const response =
+ await session.prompt('hello', {responseConstraint: kValidResponseSchema});
+ testResponseJsonSchema(response, t);
}, 'Prompt API should work when a valid response json schema is provided.');
promise_test(async t => {
await ensureLanguageModel();
const session = await createLanguageModel();
- const validRepsonseJsonSchema = {
- type: "object",
- required: ["Rating"],
- additionalProperties: false,
- properties: {
- Rating: {
- type: "number",
- minimum: 0,
- maximum: 5,
- },
- },
- };
- const promptPromise = session.prompt([
- {role: 'user', content: 'hello'},
- {role: 'assistant', content: 'prefix', prefix: true}
- ], { responseConstraint : validRepsonseJsonSchema });
- // Both the prompt and schema should be present, but prefix should be last.
- assert_regexp_match(await promptPromise, /hello.*Rating.*prefix/s);
+ const assistantPrefix = '{ "Rating": ';
+ const assistantResponse = await session.prompt(
+ [
+ {role: 'user', content: 'hello'},
+ {role: 'assistant', content: assistantPrefix, prefix: true}
+ ],
+ {responseConstraint: kValidResponseSchema});
+ const response = assistantPrefix + assistantResponse;
+ testResponseJsonSchema(response, t);
}, 'Prompt API should work when a valid response json schema and model prefix is provided.');
promise_test(async t => {
await ensureLanguageModel();
const session = await createLanguageModel();
- const validRepsonseJsonSchema = {
- type: "object",
- required: ["Rating"],
- additionalProperties: false,
- properties: {
- Rating: {
- type: "number",
- minimum: 0,
- maximum: 5,
- },
- },
- };
- const promptPromise = session.prompt('hello', {
- responseConstraint : validRepsonseJsonSchema,
- omitResponseConstraintInput : true
+ const response = await session.prompt('hello', {
+ responseConstraint: kValidResponseSchema,
+ omitResponseConstraintInput: true
});
- assert_regexp_match(await promptPromise, /hello$/);
+ testResponseJsonSchema(response, t);
}, 'Prompt API should omit response schema from input.');
promise_test(async t => {
await ensureLanguageModel();
const session = await createLanguageModel();
- const promptPromise = session.prompt(kTestPrompt, { responseConstraint : /hello/ });
+ const promptPromise =
+ session.prompt(kTestPrompt, {responseConstraint: /hello/});
const result = await promptPromise;
- assert_true(typeof result === "string");
+ assert_true(typeof result === 'string');
}, 'Prompt API should work when a valid regex constraint is provided.');