`prefix` combined with `responseConstraint`

8 views
Skip to first unread message

Scott Fortmann-Roe

unread,
Jul 25, 2025, 10:58:05 AMJul 25
to chrome-ai-dev-...@chromium.org
It appears the `prefix` property is ignored when a `responseConstraint` is also specified.

My understanding is that in the example below, it should always return a rating of 2. However, it does not.

I guess allowing both `prefix` and `responseConstraint` would allow you to construct impossible to complete scenarios where the prefix is simply incompatible with what the responseConstraint expects? Maybe in scenarios like that, an error could be thrown.

Example:

const session = await LanguageModel.create();
const schema = {
  type: "object",
  required: ["rating"],
  additionalProperties: false,
  properties: {
    rating: {
      type: "number",
      minimum: 0,
      maximum: 5,
    },
  },
};

// Prompt the model and wait for the JSON response to come back.
const result = await session.prompt([{
      role: "user",
      content: "Summarize this feedback into a rating between 0-5: The food was delicious, service was excellent, will recommend."
    },
    {
      role: "assistant",
      content: '{"rating":2',
      prefix: true
    }],
  { responseConstraint: schema }
);

console.log(result);

Die4Ever2005

unread,
Jul 25, 2025, 11:02:56 AMJul 25
to Chrome Built-in AI Early Preview Program Discussions, Scott Fortmann-Roe

Scott Fortmann-Roe

unread,
Jul 25, 2025, 11:18:41 AMJul 25
to Die4Ever2005, Chrome Built-in AI Early Preview Program Discussions
Thanks! I'll follow up there.
Reply all
Reply to author
Forward
0 new messages