-
Notifications
You must be signed in to change notification settings - Fork 594
Recursive JSON Schemas #330
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
Comments
Thank you for opening an issue! It looks like something we should be able to support. Do you mind pasting the JSON schema for these 3 models here? |
In all cases, the schema is available through
{
"$defs": {
"Task": {
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"subtasks": {
"default": [],
"items": {
"$ref": "#/$defs/Task"
},
"title": "Subtasks",
"type": "array"
}
},
"required": [
"name"
],
"title": "Task",
"type": "object"
},
"TaskOptional": {
"properties": {
"subtask": {
"anyOf": [
{
"$ref": "#/$defs/Task"
},
{
"type": "null"
}
]
}
},
"required": [
"subtask"
],
"title": "TaskOptional",
"type": "object"
}
},
"properties": {
"task": {
"$ref": "#/$defs/TaskOptional"
}
},
"required": [
"task"
],
"title": "TaskWrapperOptional",
"type": "object"
}
{
"$defs": {
"TaskUnion": {
"properties": {
"subtask": {
"anyOf": [
{
"$ref": "#/$defs/TaskUnion"
},
{
"type": "null"
}
]
}
},
"required": [
"subtask"
],
"title": "TaskUnion",
"type": "object"
}
},
"properties": {
"task": {
"$ref": "#/$defs/TaskUnion"
}
},
"required": [
"task"
],
"title": "TaskWrapperUnion",
"type": "object"
}
{
"$defs": {
"TaskList": {
"properties": {
"subtask": {
"items": {
"$ref": "#/$defs/TaskList"
},
"title": "Subtask",
"type": "array"
}
},
"required": [
"subtask"
],
"title": "TaskList",
"type": "object"
}
},
"properties": {
"task": {
"$ref": "#/$defs/TaskList"
}
},
"required": [
"task"
],
"title": "TaskWrapperList",
"type": "object"
} |
Anyone who is interested in this feature should know that CFG-structured generation is required to truly support it. |
I saw there is a pull request that implement a beta version of CFG guided generation, which is amazing. But the request failed, is that everything thats necessary to get this functionality available? Anyway, if there is anything i can do to help please let me know. |
@hugocool To have stable CFG-based JSON generation we need
There may be other paths forward, but this is the approach immediately obvious to me. This isn't an area of focus of mine at the moment, but if you're interested in tackling either issue, please let me know what questions you have! |
Okay, i am willing to pick this up. I think i would start with generating a Lark grammer for my specific usecase, which is a specific recursive JSON model. Then if that works we can see how to generalize it so it can work for any arbitrary JSON schema.
Are there any more resources i should be aware off? Lastly, i am assuming that the second issue you mentioned, would come into play once we would like to generalize the solution to JSON more broadly, right? |
Is this working now? Did you guys fix it? If so, is there a branch or release I can pull to test it out? And does it work using CFG or a different technique? |
It should work (with fixed depth). Implementation is in |
Recursive Pydantic definitions seem unsupported for lists, unions, and optionals. My understanding is these are the basic use cases.
A reproducible example is provided below:
I'd be interested in adding this functionality but I'm unsure as to what an "unrolled" recursive definition would look like in terms of the generated regex.
The text was updated successfully, but these errors were encountered: