llm_gateway 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/README.md +149 -64
  4. data/Rakefile +5 -0
  5. data/docs/migration_guide_0.10.0.md +53 -0
  6. data/docs/structured-output.md +28 -0
  7. data/lib/llm_gateway/adapter_definition.rb +64 -0
  8. data/lib/llm_gateway/adapters/adapter.rb +105 -21
  9. data/lib/llm_gateway/adapters/anthropic/messages_adapter.rb +8 -0
  10. data/lib/llm_gateway/adapters/anthropic_option_mapper.rb +29 -19
  11. data/lib/llm_gateway/adapters/groq/chat_completions_adapter.rb +8 -0
  12. data/lib/llm_gateway/adapters/groq/option_mapper.rb +7 -9
  13. data/lib/llm_gateway/adapters/normalized_stream_accumulator.rb +7 -3
  14. data/lib/llm_gateway/adapters/openai/chat_completions/option_mapper.rb +7 -9
  15. data/lib/llm_gateway/adapters/openai/chat_completions/stream_mapper.rb +1 -1
  16. data/lib/llm_gateway/adapters/openai/chat_completions_adapter.rb +6 -0
  17. data/lib/llm_gateway/adapters/openai/responses/option_mapper.rb +11 -9
  18. data/lib/llm_gateway/adapters/openai/responses/stream_mapper.rb +1 -1
  19. data/lib/llm_gateway/adapters/openai/responses_adapter.rb +6 -0
  20. data/lib/llm_gateway/adapters/openai_codex/responses_adapter.rb +8 -0
  21. data/lib/llm_gateway/adapters/stream_mapper.rb +4 -3
  22. data/lib/llm_gateway/agents/harness.rb +63 -29
  23. data/lib/llm_gateway/agents/in_memory_session_manager.rb +35 -4
  24. data/lib/llm_gateway/agents/tools/tool_utils.rb +2 -2
  25. data/lib/llm_gateway/errors.rb +3 -0
  26. data/lib/llm_gateway/models/catalog.rb +68 -0
  27. data/lib/llm_gateway/models/cost_calculator.rb +47 -0
  28. data/lib/llm_gateway/models/definition.rb +325 -0
  29. data/lib/llm_gateway/models/explicit.rb +46 -0
  30. data/lib/llm_gateway/models/generated.json +3055 -0
  31. data/lib/llm_gateway/models.rb +78 -0
  32. data/lib/llm_gateway/prompt.rb +13 -13
  33. data/lib/llm_gateway/proxy/adapter.rb +22 -12
  34. data/lib/llm_gateway/proxy/client.rb +4 -4
  35. data/lib/llm_gateway/proxy/protocol.rb +50 -0
  36. data/lib/llm_gateway/proxy/server.rb +12 -9
  37. data/lib/llm_gateway/utils.rb +12 -6
  38. data/lib/llm_gateway/version.rb +1 -1
  39. data/lib/llm_gateway.rb +4 -67
  40. data/scripts/generate_model_catalog.rb +195 -0
  41. metadata +13 -4
  42. data/lib/llm_gateway/client.rb +0 -35
  43. data/lib/llm_gateway/provider_registry.rb +0 -37
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LlmGateway
4
+ # Public, immutable description of an adapter. Definitions are useful when an
5
+ # application wants to pass an adapter choice around without using its string ID.
6
+ class AdapterDefinition
7
+ attr_reader :adapter_class, :provider, :client_class
8
+
9
+ def initialize(adapter_class)
10
+ @adapter_class = adapter_class
11
+ @provider = adapter_class.provider
12
+ @client_class = adapter_class.client_class
13
+ freeze
14
+ end
15
+
16
+ def build(**config)
17
+ adapter_class.build(**config)
18
+ end
19
+ end
20
+
21
+ module Adapters
22
+ # Adds the small public definition API to names such as
23
+ # Adapters::OpenAI::Responses without exposing adapter construction details.
24
+ module DefinitionFacade
25
+ def define_adapter(adapter_class)
26
+ raise ArgumentError, "Adapter definition already configured for #{self}" if @definition
27
+
28
+ @definition = AdapterDefinition.new(adapter_class)
29
+ end
30
+
31
+ def definition
32
+ @definition || raise("No adapter definition configured for #{self}")
33
+ end
34
+
35
+ def build(**config)
36
+ definition.build(**config)
37
+ end
38
+
39
+ def adapter_class
40
+ definition.adapter_class
41
+ end
42
+
43
+ def provider
44
+ definition.provider
45
+ end
46
+
47
+ def supports_model?(model)
48
+ adapter_class.supports_model?(model)
49
+ end
50
+
51
+ def provider_model_key(model)
52
+ adapter_class.provider_model_key(model)
53
+ end
54
+
55
+ def model_for_provider_model_key(provider_model_key)
56
+ adapter_class.model_for_provider_model_key(provider_model_key)
57
+ end
58
+
59
+ def resolve_model!(model)
60
+ adapter_class.resolve_model!(model)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -5,44 +5,134 @@ require_relative "structs"
5
5
  module LlmGateway
6
6
  module Adapters
7
7
  class Adapter
8
- attr_reader :client, :provider_key
8
+ class << self
9
+ def provider(value = nil)
10
+ if value
11
+ if instance_variable_defined?(:@provider)
12
+ raise ArgumentError, "#{name} already declares a provider"
13
+ end
9
14
 
10
- def initialize(client, provider_key: nil)
15
+ return @provider = value.to_s.freeze
16
+ end
17
+
18
+ return @provider if instance_variable_defined?(:@provider)
19
+ return superclass.provider if superclass.respond_to?(:provider)
20
+
21
+ raise ArgumentError, "#{name} does not declare a provider"
22
+ end
23
+
24
+ def client_class(value = nil)
25
+ if value
26
+ if instance_variable_defined?(:@client_class)
27
+ raise ArgumentError, "#{name} already declares a client_class"
28
+ end
29
+
30
+ return @client_class = value
31
+ end
32
+
33
+ return @client_class if instance_variable_defined?(:@client_class)
34
+ return superclass.client_class if superclass.respond_to?(:client_class)
35
+
36
+ raise ArgumentError, "#{name} does not declare a client_class"
37
+ end
38
+
39
+ def definition
40
+ @definition ||= AdapterDefinition.new(self)
41
+ end
42
+
43
+ def supports_model?(model)
44
+ model.provider == provider && !LlmGateway.models.find(provider: model.provider, id: model.id).nil?
45
+ end
46
+
47
+ def provider_model_key(model)
48
+ model.id
49
+ end
50
+
51
+ def model_for_provider_model_key(provider_model_key)
52
+ LlmGateway.models.all(provider: provider).find do |model|
53
+ supports_model?(model) && self.provider_model_key(model).to_s == provider_model_key.to_s
54
+ end
55
+ end
56
+
57
+ def resolve_model!(model)
58
+ if model.provider != provider
59
+ raise LlmGateway::Errors::ModelProviderMismatch,
60
+ "Model provider #{model.provider.inspect} does not match adapter provider #{provider.inspect}"
61
+ end
62
+
63
+ canonical = LlmGateway.models.find(provider: model.provider, id: model.id)
64
+ return canonical if canonical && supports_model?(canonical)
65
+
66
+ raise LlmGateway::Errors::UnsupportedModelForAdapter,
67
+ "Model #{model.provider}/#{model.id} is not supported by #{name}"
68
+ end
69
+
70
+ def build(**config)
71
+ config = config.transform_keys(&:to_sym)
72
+ if config.key?(:model) || config.key?(:model_key)
73
+ raise ArgumentError, "Models are supplied to Adapter#stream, not Adapter.build"
74
+ end
75
+
76
+ new(client_class.new(**config))
77
+ end
78
+ end
79
+
80
+ attr_reader :client
81
+
82
+ def initialize(client)
11
83
  @client = client
12
- @provider_key = provider_key
13
84
  end
14
85
 
15
- def raw_stream(message, tools: nil, system: nil, **options, &block)
86
+ def provider
87
+ self.class.provider
88
+ end
89
+
90
+ def raw_stream(message, model:, tools: nil, system: nil, **options, &block)
91
+ model = resolve_model!(model)
92
+ provider_model_key = self.class.provider_model_key(model)
16
93
  normalized_input = map_input({
17
- messages: sanitize_messages(normalize_messages(message), target_model: options[:model]),
94
+ messages: sanitize_messages(normalize_messages(message), target_model: provider_model_key),
18
95
  tools: tools,
19
96
  system: normalize_system(system)
20
97
  })
21
98
 
99
+ mapped_options = options.merge(model: provider_model_key)
100
+ if mapped_options.key?(:reasoning)
101
+ reasoning = mapped_options.delete(:reasoning)
102
+ mapped_options[:reasoning_control] = model.reasoning_control_for(reasoning) unless reasoning.nil?
103
+ end
104
+
22
105
  perform_stream(
23
106
  normalized_input[:messages],
24
107
  tools: normalized_input[:tools],
25
108
  system: normalized_input[:system],
26
- **map_options(options),
109
+ **map_options(mapped_options),
27
110
  &block
28
111
  )
29
112
  end
30
113
 
31
- def stream(message, tools: nil, system: nil, **options, &block)
114
+ def stream(message, model:, tools: nil, system: nil, **options, &block)
32
115
  raise LlmGateway::Errors::MissingMapperForProvider, "No stream_mapper configured" unless stream_mapper
33
116
 
117
+ model = resolve_model!(model)
34
118
  mapper = stream_mapper.new(
35
- provider: LlmGateway::Client.provider_id_from_client(client),
36
- api: api_name
119
+ provider: provider,
120
+ api: api_name,
121
+ model_definition: model
37
122
  )
38
123
 
39
- raw_stream(message, tools: tools, system: system, **options) do |chunk|
124
+ raw_stream(message, model: model, tools: tools, system: system, **options) do |chunk|
40
125
  mapper.map(chunk, &block)
41
126
  end
42
127
 
43
128
  mapper.result
44
129
  end
45
130
 
131
+ # Resolves a candidate to its canonical catalog definition before streaming.
132
+ def resolve_model!(model)
133
+ self.class.resolve_model!(model)
134
+ end
135
+
46
136
  def upload_file(filename:, content:, mime_type: "application/octet-stream", purpose: "assistants")
47
137
  raise LlmGateway::Errors::MissingMapperForProvider, "No file_output_mapper configured" unless file_output_mapper
48
138
 
@@ -97,6 +187,7 @@ module LlmGateway
97
187
 
98
188
  public
99
189
 
190
+ # Proxy adapters use these to normalize the target provider's raw stream.
100
191
  def stream_api_name
101
192
  api_name
102
193
  end
@@ -118,15 +209,12 @@ module LlmGateway
118
209
  def sanitize_messages(messages, target_model: nil)
119
210
  return messages unless input_sanitizer
120
211
 
121
- target_provider = LlmGateway::Client.provider_id_from_client(client)
122
- target_api = api_name
123
-
124
- return messages unless target_provider.present? && target_api.present? && target_model.present?
212
+ return messages unless provider.present? && api_name.present? && target_model.present?
125
213
 
126
214
  input_sanitizer.sanitize(
127
215
  messages,
128
- target_provider: target_provider,
129
- target_api: target_api,
216
+ target_provider: provider,
217
+ target_api: api_name,
130
218
  target_model: target_model
131
219
  )
132
220
  end
@@ -144,11 +232,7 @@ module LlmGateway
144
232
  end
145
233
 
146
234
  def normalize_messages(message)
147
- if message.is_a?(String)
148
- [ { role: "user", content: message } ]
149
- else
150
- message
151
- end
235
+ message.is_a?(String) ? [ { role: "user", content: message } ] : message
152
236
  end
153
237
  end
154
238
  end
@@ -12,8 +12,16 @@ module LlmGateway
12
12
  module Adapters
13
13
  module Anthropic
14
14
  class MessagesAdapter < Adapter
15
+ provider "anthropic"
16
+ client_class LlmGateway::Clients::Anthropic
17
+
15
18
  include ActsLikeAnthropicMessages
16
19
  end
20
+
21
+ module Messages
22
+ extend DefinitionFacade
23
+ define_adapter MessagesAdapter
24
+ end
17
25
  end
18
26
  end
19
27
  end
@@ -4,13 +4,6 @@ module LlmGateway
4
4
  module Adapters
5
5
  module AnthropicOptionMapper
6
6
  DEFAULT_MAX_TOKENS = 20_480
7
- REASONING_EFFORT_BUDGET_TOKENS = {
8
- "low" => 1024,
9
- "medium" => 5 * 1024,
10
- "high" => 10 * 1024,
11
- "xhigh" => 20 * 1024
12
- }.freeze
13
-
14
7
  # Source: https://platform.claude.com/docs/en/api/messages/create.md
15
8
  # API: Anthropic Messages Create; accessed 2026-05-18.
16
9
  # Body parameters listed by the API reference: max_tokens, messages, model,
@@ -39,7 +32,7 @@ module LlmGateway
39
32
  ].freeze
40
33
 
41
34
  MANAGED_OPTIONS = %i[
42
- reasoning
35
+ reasoning_control
43
36
  max_completion_tokens
44
37
  response_format
45
38
  cache_key
@@ -54,10 +47,11 @@ module LlmGateway
54
47
  mapped_options[:max_tokens] = options[:max_completion_tokens] || DEFAULT_MAX_TOKENS
55
48
 
56
49
  response_format = options[:response_format]
57
- mapped_options[:output_config] = normalize_output_config(response_format) unless response_format.nil?
50
+ unless response_format.nil?
51
+ mapped_options[:output_config] = (mapped_options[:output_config] || {}).merge(normalize_output_config(response_format))
52
+ end
58
53
 
59
- reasoning = options[:reasoning]
60
- mapped_options[:thinking] = normalize_reasoning(reasoning) unless reasoning.nil? || reasoning.to_s == "none"
54
+ apply_reasoning_control!(mapped_options, options[:reasoning_control])
61
55
 
62
56
  validate_options!(mapped_options)
63
57
  mapped_options
@@ -76,19 +70,35 @@ module LlmGateway
76
70
  format_type = response_format.is_a?(Hash) ? response_format[:type] || response_format["type"] : response_format
77
71
 
78
72
  case format_type.to_s
79
- when "json_object", "json_schema"
80
- { format: "json_schema" }
73
+ when "json_schema"
74
+ raise ArgumentError, "response_format json_schema requires a schema object" unless response_format.is_a?(Hash)
75
+
76
+ definition = response_format[:json_schema] || response_format["json_schema"] || response_format
77
+ schema = definition[:schema] || definition["schema"]
78
+ raise ArgumentError, "response_format json_schema requires a schema object" unless schema.is_a?(Hash)
79
+
80
+ { format: { type: "json_schema", schema: schema } }
81
+ when "text"
82
+ {}
81
83
  else
82
- { format: "text" }
84
+ raise ArgumentError, "Unsupported Anthropic response_format: #{format_type}"
83
85
  end
84
86
  end
85
87
 
86
- def normalize_reasoning(reasoning)
87
- budget_tokens = REASONING_EFFORT_BUDGET_TOKENS[reasoning.to_s] ||
88
- raise(ArgumentError,
89
- "Invalid reasoning '#{reasoning}'. Use 'none', 'low', 'medium', 'high', or 'xhigh'.")
88
+ def apply_reasoning_control!(mapped_options, control)
89
+ return if control.nil? || control[:type] == :none
90
90
 
91
- { type: "enabled", budget_tokens: budget_tokens }
91
+ case control[:type]
92
+ when :budget_tokens
93
+ mapped_options[:thinking] = { type: "enabled", budget_tokens: control.fetch(:value) }
94
+ when :effort
95
+ mapped_options[:thinking] = { type: "adaptive" }
96
+ mapped_options[:output_config] = (mapped_options[:output_config] || {}).merge(effort: control.fetch(:value))
97
+ when :toggle
98
+ mapped_options[:thinking] = { type: "adaptive" } if control[:value]
99
+ else
100
+ raise ArgumentError, "Unsupported Anthropic reasoning control: #{control.inspect}"
101
+ end
92
102
  end
93
103
  end
94
104
  end
@@ -11,6 +11,9 @@ module LlmGateway
11
11
  module Adapters
12
12
  module Groq
13
13
  class ChatCompletionsAdapter < Adapter
14
+ provider "groq"
15
+ client_class LlmGateway::Clients::Groq
16
+
14
17
  include ActsLikeOpenAIChatCompletions
15
18
 
16
19
  private
@@ -43,6 +46,11 @@ module LlmGateway
43
46
  mapped
44
47
  end
45
48
  end
49
+
50
+ module ChatCompletions
51
+ extend DefinitionFacade
52
+ define_adapter ChatCompletionsAdapter
53
+ end
46
54
  end
47
55
  end
48
56
  end
@@ -6,7 +6,6 @@ module LlmGateway
6
6
  module OptionMapper
7
7
  DEFAULT_TEMPERATURE = 0
8
8
  DEFAULT_MAX_COMPLETION_TOKENS = 20_480
9
- VALID_REASONING_LEVELS = %w[default low medium high].freeze
10
9
 
11
10
  # Source: https://console.groq.com/docs/text-chat.md and
12
11
  # https://console.groq.com/docs/api-reference.md#chat-create
@@ -60,7 +59,7 @@ module LlmGateway
60
59
  ].freeze
61
60
 
62
61
  MANAGED_OPTIONS = %i[
63
- reasoning
62
+ reasoning_control
64
63
  cache_key
65
64
  cache_retention
66
65
  ].freeze
@@ -73,9 +72,9 @@ module LlmGateway
73
72
  mapped_options[:max_completion_tokens] = options[:max_completion_tokens] || DEFAULT_MAX_COMPLETION_TOKENS
74
73
  mapped_options[:response_format] = normalize_response_format(options[:response_format] || "text")
75
74
 
76
- reasoning = options[:reasoning]
77
- unless reasoning.nil? || reasoning.to_s == "none"
78
- mapped_options[:reasoning_effort] = normalize_reasoning_effort(reasoning)
75
+ reasoning_control = options[:reasoning_control]
76
+ unless reasoning_control.nil? || reasoning_control[:type] == :none
77
+ mapped_options[:reasoning_effort] = reasoning_effort(reasoning_control)
79
78
  mapped_options[:reasoning_format] = "parsed"
80
79
  end
81
80
 
@@ -100,11 +99,10 @@ module LlmGateway
100
99
  end
101
100
  end
102
101
 
103
- def normalize_reasoning_effort(reasoning)
104
- effort = reasoning.to_s
105
- return effort if VALID_REASONING_LEVELS.include?(effort)
102
+ def reasoning_effort(control)
103
+ return control.fetch(:value) if control[:type] == :effort
106
104
 
107
- raise ArgumentError, "Invalid reasoning '#{reasoning}'. Use 'none', 'default', 'low', 'medium', or 'high'."
105
+ raise ArgumentError, "Unsupported Groq reasoning control: #{control.inspect}"
108
106
  end
109
107
  end
110
108
  end
@@ -3,6 +3,7 @@
3
3
  require "json"
4
4
 
5
5
  require_relative "../utils"
6
+ require_relative "../models/cost_calculator"
6
7
  require_relative "structs"
7
8
 
8
9
  module LlmGateway
@@ -54,7 +55,7 @@ module LlmGateway
54
55
  # The accumulator creates the public Assistant* event structs, updates its
55
56
  # accumulated message state, then yields the created event to the callback.
56
57
  attr_accessor :blocks, :message_hash, :usage_hash
57
- attr_reader :active_block_type, :final_message
58
+ attr_reader :active_block_type, :final_message, :model_definition
58
59
 
59
60
  DEFAULT_USAGE = {
60
61
  input: 0,
@@ -62,7 +63,8 @@ module LlmGateway
62
63
  cache_read: 0,
63
64
  output: 0,
64
65
  total: 0,
65
- raw: {}
66
+ raw: {},
67
+ cost: nil
66
68
  }.freeze
67
69
 
68
70
  BLOCK_EVENT_TRANSITIONS = {
@@ -80,9 +82,10 @@ module LlmGateway
80
82
  reasoning_end: { block_type: :reasoning, phase: :end }
81
83
  }.freeze
82
84
 
83
- def initialize(provider: nil, api: nil)
85
+ def initialize(provider: nil, api: nil, model_definition: nil)
84
86
  @provider = provider
85
87
  @api = api
88
+ @model_definition = model_definition
86
89
  @message_hash = {}
87
90
  @usage_hash = default_usage
88
91
  @blocks = []
@@ -322,6 +325,7 @@ module LlmGateway
322
325
  usage = default_usage.merge(usage.to_h.symbolize_keys.slice(*DEFAULT_USAGE.keys))
323
326
  usage[:total] = usage[:input] + usage[:cache_write] + usage[:cache_read] + usage[:output]
324
327
  usage[:raw] ||= {}
328
+ usage[:cost] = LlmGateway::Models::CostCalculator.calculate(model_definition, usage)
325
329
  usage
326
330
  end
327
331
 
@@ -6,7 +6,6 @@ module LlmGateway
6
6
  module ChatCompletions
7
7
  module OptionMapper
8
8
  DEFAULT_MAX_COMPLETION_TOKENS = 20_480
9
- VALID_REASONING_LEVELS = %w[low medium high xhigh].freeze
10
9
 
11
10
  # Source: https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create/index.md
12
11
  # API: OpenAI Chat Completions Create; accessed 2026-05-18.
@@ -58,7 +57,7 @@ module LlmGateway
58
57
  ].freeze
59
58
 
60
59
  MANAGED_OPTIONS = %i[
61
- reasoning
60
+ reasoning_control
62
61
  cache_key
63
62
  cache_retention
64
63
  ].freeze
@@ -85,9 +84,9 @@ module LlmGateway
85
84
  mapped_options.delete(:prompt_cache_retention)
86
85
  end
87
86
 
88
- reasoning = options[:reasoning]
89
- mapped_options[:reasoning_effort] = normalize_reasoning_effort(reasoning) \
90
- unless reasoning.nil? || reasoning.to_s == "none"
87
+ reasoning_control = options[:reasoning_control]
88
+ mapped_options[:reasoning_effort] = reasoning_effort(reasoning_control) \
89
+ unless reasoning_control.nil? || reasoning_control[:type] == :none
91
90
 
92
91
  validate_options!(mapped_options)
93
92
  mapped_options
@@ -116,11 +115,10 @@ module LlmGateway
116
115
  end
117
116
  end
118
117
 
119
- def normalize_reasoning_effort(reasoning)
120
- effort = reasoning.to_s
121
- return effort if VALID_REASONING_LEVELS.include?(effort)
118
+ def reasoning_effort(control)
119
+ return control.fetch(:value) if control[:type] == :effort
122
120
 
123
- raise ArgumentError, "Invalid reasoning '#{reasoning}'. Use 'none', 'low', 'medium', 'high', or 'xhigh'."
121
+ raise ArgumentError, "Unsupported OpenAI Chat Completions reasoning control: #{control.inspect}"
124
122
  end
125
123
  end
126
124
  end
@@ -9,7 +9,7 @@ module LlmGateway
9
9
  class StreamMapper < LlmGateway::Adapters::StreamMapper
10
10
  def map(chunk, &block)
11
11
  data = chunk[:data] || {}
12
- raise_stream_error!(data) if chunk[:event] == "error" || data[:error] || data[:type] == "error"
12
+ raise_stream_error!(data, overload_codes: [ "server_is_overloaded" ]) if chunk[:event] == "error" || data[:error] || data[:type] == "error"
13
13
 
14
14
  push_patches(patches_for(data), &block)
15
15
  end
@@ -12,8 +12,14 @@ module LlmGateway
12
12
  module Adapters
13
13
  module OpenAI
14
14
  class ChatCompletionsAdapter < Adapter
15
+ provider "openai"
16
+ client_class LlmGateway::Clients::OpenAI
17
+
15
18
  include ActsLikeOpenAIChatCompletions
16
19
  end
20
+
21
+ ChatCompletions.extend(DefinitionFacade)
22
+ ChatCompletions.define_adapter(ChatCompletionsAdapter)
17
23
  end
18
24
  end
19
25
  end
@@ -6,7 +6,6 @@ module LlmGateway
6
6
  module Responses
7
7
  module OptionMapper
8
8
  DEFAULT_MAX_OUTPUT_TOKENS = 20_480
9
- VALID_REASONING_LEVELS = %w[low medium high xhigh].freeze
10
9
 
11
10
  # Source: https://developers.openai.com/api/reference/resources/responses/methods/create/index.md
12
11
  # API: OpenAI Responses Create; accessed 2026-05-18.
@@ -49,6 +48,7 @@ module LlmGateway
49
48
  ].freeze
50
49
 
51
50
  MANAGED_OPTIONS = %i[
51
+ reasoning_control
52
52
  max_completion_tokens
53
53
  response_format
54
54
  cache_key
@@ -80,9 +80,9 @@ module LlmGateway
80
80
  response_format = options[:response_format]
81
81
  mapped_options[:text] = text_with_response_format(mapped_options[:text], response_format) unless response_format.nil?
82
82
 
83
- reasoning = mapped_options.delete(:reasoning)
84
- mapped_options[:reasoning] = normalize_reasoning(reasoning) \
85
- unless reasoning.nil? || reasoning.to_s == "none"
83
+ reasoning_control = options[:reasoning_control]
84
+ mapped_options[:reasoning] = normalize_reasoning(reasoning_control) \
85
+ unless reasoning_control.nil? || reasoning_control[:type] == :none
86
86
 
87
87
  validate_options!(mapped_options)
88
88
  mapped_options
@@ -111,16 +111,18 @@ module LlmGateway
111
111
  end
112
112
  end
113
113
 
114
- def normalize_reasoning(reasoning)
115
- effort = reasoning.to_s
116
- return { effort: effort, summary: "detailed" } if VALID_REASONING_LEVELS.include?(effort)
114
+ def normalize_reasoning(control)
115
+ return { effort: control.fetch(:value), summary: "detailed" } if control[:type] == :effort
117
116
 
118
- raise ArgumentError, "Invalid reasoning '#{reasoning}'. Use 'none', 'low', 'medium', 'high', or 'xhigh'."
117
+ raise ArgumentError, "Unsupported OpenAI Responses reasoning control: #{control.inspect}"
119
118
  end
120
119
 
121
120
  def text_with_response_format(text, response_format)
122
121
  text_options = text ? text.dup : {}
123
- text_options[:format] = response_format.is_a?(String) ? { type: response_format } : response_format
122
+ format = response_format.is_a?(String) ? { type: response_format } : response_format
123
+ definition = format[:json_schema] || format["json_schema"]
124
+ format = definition.merge(type: "json_schema") if definition
125
+ text_options[:format] = format
124
126
  text_options
125
127
  end
126
128
  end
@@ -12,7 +12,7 @@ module LlmGateway
12
12
  def map(chunk, &block)
13
13
  event_type = chunk[:event]
14
14
  data = chunk[:data] || {}
15
- raise_stream_error!(data) if event_type == "error" || data[:error] || data[:type] == "error"
15
+ raise_stream_error!(data, overload_codes: [ "server_is_overloaded" ]) if event_type == "error" || data[:error] || data[:type] == "error"
16
16
 
17
17
  push_patches(patches_for(event_type, data), &block)
18
18
  end
@@ -12,8 +12,14 @@ module LlmGateway
12
12
  module Adapters
13
13
  module OpenAI
14
14
  class ResponsesAdapter < Adapter
15
+ provider "openai"
16
+ client_class LlmGateway::Clients::OpenAI
17
+
15
18
  include ActsLikeOpenAIResponses
16
19
  end
20
+
21
+ Responses.extend(DefinitionFacade)
22
+ Responses.define_adapter(ResponsesAdapter)
17
23
  end
18
24
  end
19
25
  end
@@ -12,6 +12,9 @@ module LlmGateway
12
12
  module Adapters
13
13
  module OpenAICodex
14
14
  class ResponsesAdapter < Adapter
15
+ provider "openai"
16
+ client_class LlmGateway::Clients::OpenAI
17
+
15
18
  include ActsLikeOpenAIResponses
16
19
 
17
20
  private
@@ -28,6 +31,11 @@ module LlmGateway
28
31
  client.stream_codex(messages, tools: tools, system: system, **options, &block)
29
32
  end
30
33
  end
34
+
35
+ module Responses
36
+ extend DefinitionFacade
37
+ define_adapter ResponsesAdapter
38
+ end
31
39
  end
32
40
  end
33
41
  end
@@ -5,9 +5,10 @@ require_relative "normalized_stream_accumulator"
5
5
  module LlmGateway
6
6
  module Adapters
7
7
  class StreamMapper
8
- def initialize(provider:, api:)
8
+ def initialize(provider:, api:, model_definition: nil)
9
9
  @provider = provider
10
10
  @api = api
11
+ @model_definition = model_definition
11
12
  end
12
13
 
13
14
  def result
@@ -16,10 +17,10 @@ module LlmGateway
16
17
 
17
18
  private
18
19
 
19
- attr_reader :provider, :api
20
+ attr_reader :provider, :api, :model_definition
20
21
 
21
22
  def accumulator
22
- @accumulator ||= LlmGateway::Adapters::NormalizedStreamAccumulator.new(provider:, api:)
23
+ @accumulator ||= LlmGateway::Adapters::NormalizedStreamAccumulator.new(provider:, api:, model_definition:)
23
24
  end
24
25
 
25
26
  def push_patches(patches, &block)