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

Allow Non-Strict mode in generate.json #1159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lapp0
Copy link
Collaborator

@lapp0 lapp0 commented Sep 17, 2024

Fixes #985

Problem

For generate.json(), Sometimes models exhaust their available token length and generate a sequence which isn't valid JSON or doesn't comply with the provided schema.

Solution

Implement strict mode, which defaults to True (same behavior as main)

If generate.json(..., strict=False) is called, an error dict will be returned rather than an error being raised.

{
    'error': 'Unterminated string starting at: line 1 column 13 (char 12)', 
    'error_type': 'JSONDecodeError', 
    'output': '{"this output obviously is not valid JSON',
}

@lapp0 lapp0 changed the title Create optional generate.json(..., strict=False) Allow Non-Strict mode in generate.json Sep 17, 2024
@lapp0 lapp0 marked this pull request as ready for review September 17, 2024 20:16
@rlouf
Copy link
Member

rlouf commented Sep 23, 2024

Pydantic does offer partial JSON parsing, is that something we'd want to use here?

@lapp0
Copy link
Collaborator Author

lapp0 commented Sep 23, 2024

Pydantic does offer partial JSON parsing, is that something we'd want to use here?

IMO, users should expect their generation to comply with their schema or an error. Perhaps an allow_partial kwarg should be implemented separately if it fits with someones use case?

@rlouf
Copy link
Member

rlouf commented Sep 23, 2024

I meant in the "output" field of the error, sorry for not being more specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a non_strict mode to deal with JSONDecodeError errors
2 participants