Skip to content

Specifying set with at least 1 element #1508

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

Open
MilesCranmer opened this issue Mar 23, 2025 · 0 comments
Open

Specifying set with at least 1 element #1508

MilesCranmer opened this issue Mar 23, 2025 · 0 comments

Comments

@MilesCranmer
Copy link

Hi all,

Thanks for the great library. I'm trying to categorize publications into ML/Astro/Physics. Just curious what the right way to do this is.

I am using the following schema:

import outlines
from pydantic import BaseModel, field_validator
from enum import Enum

class Category(str, Enum):
    ML = "ML"
    Astro = "Astro" 
    Physics = "Physics"

class PublicationCategories(BaseModel):
    categories: set[Category]

    @field_validator('categories')
    @classmethod
    def ensure_foobar(cls, v):
        if len(v) == 0:
            raise ValueError('No categories found')
        return v

model = outlines.models.transformers(
    "HuggingFaceTB/SmolLM-135M-Instruct",
    device="auto",
)
category_extractor = outlines.generate.json(model, PublicationCategories)

The validator hits this error when the LLM tries to return no outputs:

  Value error, No categories found [type=value_error, input_value=[], input_type=list]
    For further information visit https://errors.pydantic.dev/2.10/v/value_error

So I'm just wondering the proper way to do this. Thanks!

So I am just wondering how I can specify a field that is a set with a minimum of 1 element?

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

No branches or pull requests

1 participant