Module: OpenAI::Helpers::StructuredOutput::JsonSchemaConverter
- Defined in:
- lib/openai/helpers/structured_output/json_schema_converter.rb
Overview
To customize the JSON schema conversion for a type, implement the JsonSchemaConverter interface.
Constant Summary collapse
- POINTERS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Object .new .tap do _1.define_singleton_method(:inspect) do "#<#{OpenAI::Helpers::StructuredOutput::JsonSchemaConverter}::POINTERS>" end end .freeze
- NO_REF =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Object .new .tap do _1.define_singleton_method(:inspect) do "#<#{OpenAI::Helpers::StructuredOutput::JsonSchemaConverter}::NO_REF>" end end .freeze
- RECURSIVE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Object .new .tap do _1.define_singleton_method(:inspect) do "#<#{OpenAI::Helpers::StructuredOutput::JsonSchemaConverter}::RECURSIVE>" end end .freeze
Class Method Summary collapse
- .assoc_meta!(schema, meta:) ⇒ Object private
- .cache_def!(state, type:, &blk) ⇒ Object private
- .to_json_schema(type) ⇒ Hash{Symbol=>Object} private
- .to_json_schema_inner(type, state:) ⇒ Hash{Symbol=>Object} private
- .to_nilable(schema) ⇒ Hash{Symbol=>Object} private
Instance Method Summary collapse
-
#to_json_schema_inner(state:) ⇒ Hash{Symbol=>Object}
The exact JSON schema produced is subject to improvement between minor release versions.
Class Method Details
.assoc_meta!(schema, meta:) ⇒ 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.
83 84 85 86 87 88 89 90 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 83 def (schema, meta:) xformed = .transform_keys(doc: :description) if schema.key?(:$ref) && !xformed.empty? schema.merge!(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::NO_REF => true) end schema.merge!(xformed) end |
.cache_def!(state, type:, &blk) ⇒ 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 104 def cache_def!(state, type:, &blk) defs, path = state.fetch_values(:defs, :path) if (stored = defs[type]) pointers = stored.fetch(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTERS) if pointers.first.fetch(:$ref).empty? stored[OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::RECURSIVE] = true end pointers.first.slice(:$ref).tap do pointers << _1 end else ref_path = String.new ref = {"$ref": ref_path} stored = { OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTERS => [ref] } defs.store(type, stored) schema = blk.call definition_name = path.map { _1.gsub("~", "~0").gsub("/", "~1") }.join("/") pointer_token = definition_name.gsub("~", "~0").gsub("/", "~1") escaped_name = URI::RFC2396_PARSER.escape(pointer_token, /[^A-Za-z0-9._~-]/) ref_path.replace("#/$defs/#{escaped_name}") stored.update(schema) ref end end |
.to_json_schema(type) ⇒ 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.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 137 def to_json_schema(type) defs = {} state = {defs: defs, path: []} schema = OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_json_schema_inner( type, state: state ) reused_defs = {} defs.each_value do |acc| recursive = acc.key?(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::RECURSIVE) sch = acc.except( OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTERS, OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::RECURSIVE ) pointers = acc.fetch(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTERS) no_refs, refs = pointers.partition do _1.delete(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::NO_REF) end if recursive reused_defs.store(pointers.first.fetch(:$ref), sch) else case refs in [ref] ref.replace(ref.except(:$ref).merge(sch)) in [_, ref, *] reused_defs.store(ref.fetch(:$ref), sch) refs.each do unless ( = _1.except(:$ref)).empty? _1.replace(allOf: [_1.slice(:$ref), ]) end end else end end no_refs.each { _1.replace(_1.except(:$ref).merge(sch)) } unless recursive end xformed = reused_defs.transform_keys do pointer_token = URI::RFC2396_PARSER.unescape(_1.delete_prefix("#/$defs/")) pointer_token.gsub("~1", "/").gsub("~0", "~") end xformed.empty? ? schema : {"$defs": xformed}.update(schema) end |
.to_json_schema_inner(type, state:) ⇒ 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.
196 197 198 199 200 201 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 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 196 def to_json_schema_inner(type, state:) case type in {"$ref": String} return type in OpenAI::Helpers::StructuredOutput::JsonSchemaConverter return type.to_json_schema_inner(state: state) in Class case type in -> { _1 <= NilClass } return {type: "null"} in -> { _1 <= Integer } return {type: "integer"} in -> { _1 <= Float } return {type: "number"} in -> { _1 <= Symbol || _1 <= String } return {type: "string"} else end in _ if OpenAI::Internal::Util.primitive?(type) return {const: type.is_a?(Symbol) ? type.to_s : type} else end models = %w[ NilClass String Symbol Integer Float OpenAI::Boolean OpenAI::ArrayOf OpenAI::EnumOf OpenAI::UnionOf OpenAI::BaseModel ] = "#{type} does not implement the #{OpenAI::Helpers::StructuredOutput::JsonSchemaConverter} interface. Please use one of the supported types: #{models}" raise ArgumentError.new() end |
.to_nilable(schema) ⇒ 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 58 def to_nilable(schema) null = "null" case schema in {"$ref": String} { anyOf: [ schema.merge!(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::NO_REF => true), {type: null} ] } in {const: _} {anyOf: [schema, {type: null}]} in {anyOf: schemas} null = {type: null} schemas.any? { _1 == null || _1 == {type: ["null"]} } ? schema : {anyOf: [*schemas, null]} in {type: String => type} type == null ? schema : schema.update(type: [type, null]) in {type: Array => types} types.include?(null) ? schema : schema.update(type: [*types, null]) end end |
Instance Method Details
#to_json_schema_inner(state:) ⇒ Hash{Symbol=>Object}
The exact JSON schema produced is subject to improvement between minor release versions.
47 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 47 def to_json_schema_inner(state:) = (raise NotImplementedError) |