Skip to content
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

Empty list cannot be closed after a newline #63

Closed
AriX opened this issue Jan 15, 2024 · 2 comments
Closed

Empty list cannot be closed after a newline #63

AriX opened this issue Jan 15, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@AriX
Copy link
Contributor

AriX commented Jan 15, 2024

Thanks for your awesome work on this project!

I'm seeing an issue with JSON like the following:

{
  "num" : 1,
  "list_of_strings" : [
  ]
}

In particular, if a newline is generated after the array's opening [, lm-format-enforcer will not allow the list to be closed with a ]. It appears that:

  • When the [ character is parsed, a UnionParser is added to the stack with a StringParsingState and a ForceStopParser
  • When the newline character is parsed, the UnionParser decides that only StringParsingState can accept newlines, and therefore dissolves itself, returning only the StringParsingState onto the stack and removing the ForceStopParser
  • Without ForceStopParser on the stack, JsonSchemaParser's allowedCharacters implementation does not evaluate any parsers on the stack above StringParsingState, because StringParsingState returns False for canEnd()
  • Therefore, allowedCharacters does not include ] and the list cannot be closed

This can be verified by adding this test to test_jsonschemaparser.py:

def test_empty_list_with_newline():
    class EmptyListOKModel(BaseModel):
        num: int
        list_of_strings: Optional[List[str]] = Field(None, min_length=0, max_length=1)
    
    no_strings = '{"num":1,"list_of_strings":[\n]}'
    _test_json_schema_parsing_with_string(no_strings, EmptyListOKModel.model_json_schema(), True)

I'm not sure what the best solution here is, but some ideas I have are:

  • Have ForceStopParser allow newlines/whitespace
  • Prevent UnionParser from dissolving itself if one of its parsers is a ForceStopParser

Any input on what solution would be most idiomatic would be greatly appreciated.

@noamgat noamgat added the bug Something isn't working label Jan 20, 2024
@noamgat
Copy link
Owner

noamgat commented Jan 20, 2024

Thanks for the report! I hope to fix this in the near future.

@noamgat
Copy link
Owner

noamgat commented Feb 2, 2024

Solved in v0.8.3

@noamgat noamgat closed this as completed Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants