noamgat / lm-format-enforcer

Enforce the output format (JSON Schema, Regex etc) of a language model
MIT License
994 stars 45 forks source link

Exception when `additionalProperties` is a boolean. #69

Open LorrinWWW opened 4 months ago

LorrinWWW commented 4 months ago

Boolean value will raise exception:'int' object has no attribute 'get' Expected behaviours: https://json-schema.org/understanding-json-schema/reference/object#additional-properties

noamgat commented 4 months ago

Thanks for the report! Can you please provide a reproducing example so I can fix it?

LorrinWWW commented 4 months ago

When additionalProperties is False, it should be the same to without this field. For example, given schema:

{
 "type": "object",
 "properties": {
   "number": { "type": "number" },
   "street_name": { "type": "string" },
   "street_type": { "enum": ["Street", "Avenue", "Boulevard"] }
 },
 "additionalProperties": false
}

This should be a valid output:

{ "number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue" }

When additionalProperties is True, it allows additional entries with any data types. if additionalProperties is { "type": <<data_type>> }, it allows additional entries with <<data_type>> data type. For example, given schema:

{
 "type": "object",
 "properties": {
   "number": { "type": "number" },
   "street_name": { "type": "string" },
   "street_type": { "enum": ["Street", "Avenue", "Boulevard"] }
 },
 "additionalProperties": { "type": "string" } 
}

This should be a valid output:

{ "number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue", "description": "NW"}

where "description" is an additional entry where its corresponding data type could only be "string".

@noamgat Thank you!

jamestwhedbee commented 3 hours ago

@noamgat I am running into this too, I'd really like to be able to pass / enforce "additionalProperties": false