noamgat / lm-format-enforcer

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

Purely Recursive JSON Schema Broken #98

Closed NJordan72 closed 1 month ago

NJordan72 commented 1 month ago

When working with a JSON Schema (generated from Pydantic v2) I get an error when creating a parser.

from typing import List
from pydantic import BaseModel

from lmformatenforcer import JsonSchemaParser

class TreeNode(BaseModel):
    name: str
    children: List["TreeNode"]

parser = JsonSchemaParser(TreeNode.model_json_schema())

results in raise Exception("Unsupported type " + str(value_schema.type)) Exception: Unsupported type None

If I wrap the TreeNode in a simple response object everything works fine:

from typing import List
from pydantic import BaseModel

from lmformatenforcer import JsonSchemaParser

class TreeNode(BaseModel):
    name: str
    children: List["TreeNode"]

class Result(BaseModel):
    tree: TreeNode

parser = JsonSchemaParser(Result.model_json_schema())
noamgat commented 1 month ago

Interesting, I will have a look in the next bug squash (unless you want to dive in and submit a PR :) )

On Tue, May 14, 2024 at 3:24 PM NJordan72 @.***> wrote:

When working with a JSON Schema (generated from Pydantic v2) I get an error when creating a parser.

from typing import Listfrom pydantic import BaseModel from lmformatenforcer import JsonSchemaParser class TreeNode(BaseModel): name: str children: List["TreeNode"] parser = JsonSchemaParser(TreeNode.model_json_schema())

results in raise Exception("Unsupported type " + str(value_schema.type)) Exception: Unsupported type None

If I wrap the TreeNode in a simple response object everything works fine:

from typing import Listfrom pydantic import BaseModel from lmformatenforcer import JsonSchemaParser class TreeNode(BaseModel): name: str children: List["TreeNode"] class Result(BaseModel): tree: TreeNode parser = JsonSchemaParser(Result.model_json_schema())

— Reply to this email directly, view it on GitHub https://github.com/noamgat/lm-format-enforcer/issues/98, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKFA2HEZ66AHQYGCG2QWSTZCH675AVCNFSM6AAAAABHWCIJEOVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI4TKMRYHE2TCMI . You are receiving this because you are subscribed to this thread.Message ID: @.***>