Skip to content

Feature request: Add enum support for integer type #30

Description

@diegocostares

Problem

Currently, enum is only supported for string type fields. However, JSON Schema specification supports enum for all types, including integer.

When working with LLMs and structured outputs, it's common to need integer enums for categorical data. For example, skill levels (0=basic, 1=intermediate, 2=advanced) or categories (0=technical, 1=soft, 2=language).

Current behavior

# This works
string :status, enum: ["active", "inactive"]

# This doesn't work - enum is ignored
integer :level, enum: [0, 1, 2]

Proposed solution

Add the enum parameter to integer_schema (and optionally number_schema):

# In lib/ruby_llm/schema/dsl/schema_builders.rb

def integer_schema(description: nil, enum: nil, minimum: nil, maximum: nil, multiple_of: nil)
  {
    type: "integer",
    enum: enum,
    description: description,
    minimum: minimum,
    maximum: maximum,
    multipleOf: multiple_of
  }.compact
end

Use case

class SkillSchema < RubyLLM::Schema
  string :name, description: "Skill name"
  integer :level, description: "Proficiency level", enum: [0, 1, 2]
  integer :category, description: "Skill category", enum: [0, 1, 2, 3]
end

This is valid JSON Schema and, according to OpenAI's documentation, integer types and enums are supported in structured outputs, so this should be compatible in practice.

References

I'm happy to submit a PR if this feature is welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions