Module: OpenAI::Internal::Type::Converter Private

Extended by:
Util::SorbetRuntimeSupport
Included in:
Helpers::StructuredOutput::SorbetAdapter, ArrayOf, BaseModel, Boolean, Enum, FileInput, HashOf, Union, Unknown
Defined in:
lib/openai/internal/type/converter.rb,
sig/openai/internal/type/converter.rbs

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::SorbetRuntimeSupport

const_missing, define_sorbet_constant!, sorbet_constant_defined?, to_sorbet_type, to_sorbet_type

Class Method Details

.coerce(target, value, state: OpenAI::Internal::Type::Converter.new_coerce_state) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Based on target, transform value into target, to the extent possible:

  1. if the given value conforms to target already, return the given value
  2. if it's possible and safe to convert the given value to target, then the converted value
  3. otherwise, the given value unaltered

The coercion process is subject to improvement between minor release versions. See https://docs.pydantic.dev/latest/concepts/unions/#smart-mode

when we have to decide between multiple possible conversion targets:

  • true: the conversion must be exact, with minimum coercion.
  • false: the conversion can be approximate, with some coercion.

The exactness is Hash with keys being one of yes, no, or maybe. For any given conversion attempt, the exactness will be updated based on how closely the value recursively matches the target type:

  • yes: the value can be converted to the target type with minimum coercion.
  • maybe: the value can be converted to the target type with some reasonable coercion.
  • no: the value cannot be converted to the target type.

See implementation below for more details.

@option state [Boolean] :translate_names

@option state [Boolean] :strictness

@option state [Hash{Symbol=>Object}] :exactness

@option state [Class<StandardError>] :error

@option state [Integer] :branched

Parameters:

  • target (OpenAI::Internal::Type::Converter, Class)
  • value (Object)
  • state (Hash{Symbol=>Object}) (defaults to: OpenAI::Internal::Type::Converter.new_coerce_state) —

    The strictness is one of true, false. This informs the coercion strategy

  • state: (OpenAI::Internal::Type::Converter::coerce_state) (defaults to: OpenAI::Internal::Type::Converter.new_coerce_state)

Returns:

  • (Object)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/openai/internal/type/converter.rb', line 202

def coerce(target, value, state: OpenAI::Internal::Type::Converter.new_coerce_state)
  if state[:memo] &&
      target.is_a?(OpenAI::Internal::Type::Converter) &&
      (value.is_a?(Hash) || value.is_a?(Array))
    return coerce_cached(target, value, state: state)
  end

  exactness = state.fetch(:exactness)

  case target
  in OpenAI::Internal::Type::Converter
    return target.coerce(value, state: state)
  in Class
    if value.is_a?(target)
      exactness[:yes] += 1
      return value
    end

    case target
    in -> { _1 <= NilClass }
      exactness[nil.equal?(value) ? :yes : :maybe] += 1
      return nil
    in -> { _1 <= Integer }
      case value
      in Integer
        exactness[:yes] += 1
        return value
      else
        Kernel.then do
          # Wire text is decimal. `Integer(str)` would read a leading `0` as
          # octal and `0x`/`0b` as other bases, so "010" became 8 and "08"
          # failed outright. Non-strings keep the unbased call, which accepts
          # Float and other numerics that reject an explicit base.
          coerced = value.is_a?(String) ? Integer(value, 10) : Integer(value)
          return coerced.tap { exactness[:maybe] += 1 }
        rescue ArgumentError, TypeError => e
          state[:error] = e
        end
      end

    in -> { _1 <= Float }
      if value.is_a?(Numeric)
        exactness[:yes] += 1
        return Float(value)
      else
        Kernel.then do
          return Float(value).tap { exactness[:maybe] += 1 }
        rescue ArgumentError, TypeError => e
          state[:error] = e
        end
      end

    in -> { _1 <= String }
      case value
      in String | Symbol | Numeric
        exactness[value.is_a?(Numeric) ? :maybe : :yes] += 1
        return value.to_s
      in StringIO
        exactness[:yes] += 1
        return value.string
      else
        state[:error] = TypeError.new("#{value.class} can't be coerced into #{String}")
      end

    in -> { _1 <= Symbol }
      if value.is_a?(String)
        exactness[:yes] += 1
        return value.to_sym
      end

    in -> { _1 <= Date || _1 <= Time }
      Kernel.then do
        return target.parse(value).tap { exactness[:yes] += 1 }
      rescue ArgumentError, TypeError => e
        state[:error] = e
      end

    in -> { _1 <= StringIO } if value.is_a?(String)
      exactness[:yes] += 1
      return StringIO.new(value.b)
    else
    end

  in Symbol
    case value
    in Symbol | String
      if value.to_sym == target
        exactness[:yes] += 1
        return target
      else
        exactness[:maybe] += 1
        return value
      end
    else
      message = "cannot convert non-matching #{value.class} into #{target.inspect}"
      state[:error] = ArgumentError.new(message)
    end
  else
  end

  exactness[:no] += 1
  value
end

.coerce_cached(target, value, state:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reuse converter/value pairs within one parse, including recursive collections. Identity keys avoid recursively hashing model and converter definitions.

Parameters:

Returns:

  • (Object)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/openai/internal/type/converter.rb', line 309

def coerce_cached(target, value, state:)
  converters = state.fetch(:memo)
  values = converters[target] ||= {}.compare_by_identity
  outcomes = values[value] ||= {}
  key = state.values_at(:translate_names, :request_only, :strictness, :error)
  exactness = state.fetch(:exactness)
  if (cached = outcomes[key])
    parsed, delta, error, branched = cached
    delta.each { exactness[_1] += _2 }
    state[:error] = error
    state[:branched] += branched
    return parsed
  end

  before = exactness.dup
  branched = state.fetch(:branched)
  parsed = target.coerce(value, state: state)
  delta = exactness.to_h { |name, count| [name, count - before.fetch(name)] }
  outcomes[key] = [parsed, delta, state[:error], state[:branched] - branched]
  parsed
end

.coerce_with_error(target, value, state:) ⇒ Array(Object, StandardError, nil)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerces a value while isolating its error from sibling coercions.

Parameters:

Returns:

  • (Array(Object, StandardError, nil))


342
343
344
345
346
347
348
349
# File 'lib/openai/internal/type/converter.rb', line 342

def coerce_with_error(target, value, state:)
  previous_error = state.fetch(:error)
  state[:error] = nil
  coerced = coerce(target, value, state: state)
  [coerced, state.fetch(:error)]
ensure
  state[:error] = previous_error
end

.dump(target, value, state: {can_retry: true}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • target (OpenAI::Internal::Type::Converter, Class)
  • value (Object)
  • state (Hash{Symbol=>Object}) (defaults to: {can_retry: true}) —

    .

    @option state [Boolean] :can_retry

  • state: (OpenAI::Internal::Type::Converter::dump_state) (defaults to: {can_retry: true})

Returns:

  • (Object)


362
363
364
365
366
367
368
369
# File 'lib/openai/internal/type/converter.rb', line 362

def dump(target, value, state: {can_retry: true})
  case target
  in OpenAI::Internal::Type::Converter
    target.dump(value, state: state)
  else
    OpenAI::Internal::Type::Unknown.dump(value, state: state)
  end
end

.inspect(target, depth:) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • target (Object)
  • depth (Integer)
  • depth: (Integer)

Returns:

  • (String)


377
378
379
380
381
382
383
384
# File 'lib/openai/internal/type/converter.rb', line 377

def inspect(target, depth:)
  case target
  in OpenAI::Internal::Type::Converter
    target.inspect(depth: depth.succ)
  else
    target.inspect
  end
end

.meta_info(type_info, spec) ⇒ Hash{Symbol=>Object}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • type_info (Hash{Symbol=>Object}, Proc, OpenAI::Internal::Type::Converter, Class) —

    .

    @option type_info [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option type_info [Proc] :enum

    @option type_info [Proc] :union

    @option type_info [Boolean] :"nil?"

  • spec (Hash{Symbol=>Object}, Proc, OpenAI::Internal::Type::Converter, Class) —

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :"nil?"

Returns:

  • (Hash{Symbol=>Object})


124
125
126
127
128
129
# File 'lib/openai/internal/type/converter.rb', line 124

def meta_info(type_info, spec)
  [type_info, spec]
    .grep(Hash)
    .reduce({}, :merge)
    .except(:const, :enum, :union, :nil?)
end

.new_coerce_state(translate_names: true, request_only: false, memoize: false) ⇒ Hash{Symbol=>Object}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • translate_names (Boolean) (defaults to: true)
  • request_only (Boolean) (defaults to: false) —

    Exclude fields omitted by the request serializer.

  • translate_names: (Boolean) (defaults to: true)
  • request_only: (Boolean) (defaults to: false)
  • memoize: (Boolean) (defaults to: false)

Returns:

  • (Hash{Symbol=>Object})


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/openai/internal/type/converter.rb', line 138

def new_coerce_state(translate_names: true, request_only: false, memoize: false)
  {
    translate_names: translate_names,
    request_only: request_only,
    strictness: true,
    exactness: {yes: 0, no: 0, maybe: 0},
    error: nil,
    branched: 0,
    memo: memoize ? {}.compare_by_identity : nil
  }
end

.resolve_type(type_fn, state: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • state: (coerce_state, nil) (defaults to: nil)

Returns:

  • (Object)


152
153
154
155
156
# File 'lib/openai/internal/type/converter.rb', line 152

def resolve_type(type_fn, state: nil)
  return type_fn.call unless state && state[:memo]
  cache = state[:memo][:resolved_types] ||= {}.compare_by_identity
  cache.fetch(type_fn) { cache[type_fn] = type_fn.call }
end

.type_info(spec) ⇒ Proc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • spec (Hash{Symbol=>Object}, Proc, OpenAI::Internal::Type::Converter, Class) —

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :"nil?"

Returns:

  • (Proc)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openai/internal/type/converter.rb', line 86

def type_info(spec)
  case spec
  in Proc
    spec
  in Hash
    type_info(spec.slice(:const, :enum, :union).first&.last)
  in true | false
    -> { OpenAI::Internal::Type::Boolean }
  in OpenAI::Internal::Type::Converter | Class | Symbol
    -> { spec }
  in NilClass | Integer | Float
    -> { spec.class }
  end
end

Instance Method Details

#coerce(value, state:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (Object)
  • state (Hash{Symbol=>Object}) —

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class] :error

    @option state [Integer] :branched

  • state: (OpenAI::Internal::Type::Converter::coerce_state)

Returns:

  • (Object)


29
# File 'lib/openai/internal/type/converter.rb', line 29

def coerce(value, state:) = (raise NotImplementedError)

#dump(value, state:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (Object)
  • state (Hash{Symbol=>Object}) —

    .

    @option state [Boolean] :can_retry

  • state: (OpenAI::Internal::Type::Converter::dump_state)

Returns:

  • (Object)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openai/internal/type/converter.rb', line 40

def dump(value, state:)
  case value
  in Array
    value.map { OpenAI::Internal::Type::Unknown.dump(_1, state: state) }
  in Hash
    value.transform_values { OpenAI::Internal::Type::Unknown.dump(_1, state: state) }
  in OpenAI::Internal::Type::BaseModel
    value.class.dump(value, state: state)
  in StringIO
    value.string
  in Pathname | IO
    state[:can_retry] = false if value.is_a?(IO)
    OpenAI::FilePart.new(value)
  in OpenAI::FilePart
    state[:can_retry] = false if value.content.is_a?(IO)
    value
  else
    value
  end
end

#inspect(depth: 0) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • depth (Integer) (defaults to: 0)
  • depth: (Integer) (defaults to: 0)

Returns:

  • (String)


66
67
68
# File 'lib/openai/internal/type/converter.rb', line 66

def inspect(depth: 0)
  super()
end