Skip to content

Make sure we correctly support additionalProperties set to True #150

Open
@rlouf

Description

@rlouf

Setting the additionalProperties keyword to True should allow generation of properties whose name is not listed in properties, but it is currently not the case:

import json
import re
from outlines_core.fsm.json_schema import build_regex_from_schema

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
      "a": {
        "type": "string",
      }
    },
    "required": [
      "marathonJson"
    ],
    "additionalProperties": True
}

data = json.dumps({
    "a": "aaa",
})


regex_str = build_regex_from_schema(json.dumps(schema))
print(regex_str)
# '\\{([ ]?"a"[ ]?:[ ]?"([^"\\\\\\x00-\\x1F\\x7F-\\x9F]|\\\\["\\\\])*")?[ ]?\\}'

print(re.match(regex_str, data)
# <re.Match object; span=(0, 12), match='{"a": "aaa"}'>

But:

data = json.dumps({
    "a": "aaa",
    "id": "1"
})

print(re.match(regex_str, data)
# None

The immediate solution is to return an error when additionalProperties is set to True, to avoid surprises. Once that's done we can open an issue to add it to the implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions