Class: Raif::Llms::Anthropic::StrictSchemaTransformer

Inherits:
Object
  • Object
show all
Defined in:
app/models/raif/llms/anthropic/strict_schema_transformer.rb

Constant Summary collapse

UNSUPPORTED_KEYS =

Anthropic's strict schema subset rejects these validation constraints. They are removed from the wire schema and folded into the property's description so the model still sees them. The caller's original schema remains unchanged and can still be used locally.

minLength, maxLength, and pattern are deliberately absent: the API accepts all three on both the strict-tool and native structured-output paths, so removing them would trade real constrained-decoding enforcement for a description the model may ignore. Verified against claude-opus-4-8.

%w[
  minimum
  maximum
  exclusiveMinimum
  exclusiveMaximum
  multipleOf
  minItems
  maxItems
  uniqueItems
  minProperties
  maxProperties
  patternProperties
].freeze
WIRE_MIN_ITEMS_MAX =

The strict subset does support minItems values of 0 and 1, so instead of dropping the constraint entirely, larger bounds are clamped to 1 -- the API then rejects empty arrays via constrained decoding, and the real bound rides in the description and the caller's local validation.

1
DATA_VALUE_KEYS =
%w[enum const default examples].freeze
SCHEMA_NAME_MAP_KEYS =
%w[properties $defs definitions].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ StrictSchemaTransformer

Returns a new instance of StrictSchemaTransformer.



40
41
42
# File 'app/models/raif/llms/anthropic/strict_schema_transformer.rb', line 40

def initialize(schema)
  @schema = schema
end

Class Method Details

.call(schema) ⇒ Object



36
37
38
# File 'app/models/raif/llms/anthropic/strict_schema_transformer.rb', line 36

def self.call(schema)
  new(schema).call
end

Instance Method Details

#callObject



44
45
46
# File 'app/models/raif/llms/anthropic/strict_schema_transformer.rb', line 44

def call
  transform(@schema)
end