Open AlbanPerli opened 3 weeks ago
Hi @AlbanPerli sorry for the late reply here :)
I think that part of what you are encountering here is that lists aren't forced to be non-empty by default (I think your custom grammar definitions enforce a minimum length of one). If you want to enforce this behavior with TypeAdapters
, you can use typing.Annotated
and annotated_types.MinLen
like so:
from typing import Annotated
from annotated_types import MinLen
from pydantic import TypeAdapter
ta = TypeAdapter(Annotated[list[int], MinLen(1)])
ta.json_schema()
# Output: {'items': {'type': 'integer'}, 'minItems': 1, 'type': 'array'}
You can of course get this behavior by just writing the JSON schema directly, or if you're using a pydantic.BaseModel
, you can do these annotations a bit more ergonomically with the pydantic.Field
descriptor.
If this doesn't address the core issue you're seeing, just let me know and we can figure it out :)
Hi @hudson-ai , my turn to apologize for the response time! :)
The point was indeed the minimum length, I wasn't aware of this parameter for the TypeAdapter.
Thank you!
Ok, good to know that works for you! Let us know if you hit any other unexpected or unintuitive behaviors :)
Hi @hudson-ai!
Concerning the TypeAdapter constrained generation, here are some example of the issue mentioned here:
The file containing custom CFG is here.
This is just a workaround but it helps to produce a non empty list.
Concerning the JSON:
I'm not sure to understand what the expected generation is, but it seems that characters from the format are interfering with the generated content.