Skip to content

Commit c5b4f52

Browse files
chore: validate request option coercion correctness
1 parent 8e8464e commit c5b4f52

File tree

13 files changed

+67
-59
lines changed

13 files changed

+67
-59
lines changed

lib/openai/internal/type/base_model.rb

+1-9
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,7 @@ def to_yaml(*a) = OpenAI::Internal::Type::Converter.dump(self.class, self).to_ya
390390
# Create a new instance of a model.
391391
#
392392
# @param data [Hash{Symbol=>Object}, self]
393-
def initialize(data = {})
394-
case OpenAI::Internal::Util.coerce_hash(data)
395-
in Hash => coerced
396-
@data = coerced
397-
else
398-
message = "Expected a #{Hash} or #{OpenAI::Internal::Type::BaseModel}, got #{data.inspect}"
399-
raise ArgumentError.new(message)
400-
end
401-
end
393+
def initialize(data = {}) = (@data = OpenAI::Internal::Util.coerce_hash!(data).to_h)
402394

403395
class << self
404396
# @api private

lib/openai/internal/type/request_parameters.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@ def dump_request(params)
2828
state = {can_retry: true}
2929
case (dumped = dump(params, state: state))
3030
in Hash
31-
options = OpenAI::Internal::Util.coerce_hash(dumped[:request_options])
32-
request_options =
33-
case [options, state.fetch(:can_retry)]
34-
in [Hash | nil, false]
35-
{**options.to_h, max_retries: 0}
36-
else
37-
options
38-
end
31+
options = OpenAI::Internal::Util.coerce_hash!(dumped[:request_options]).to_h
32+
request_options = state.fetch(:can_retry) ? options : {**options, max_retries: 0}
3933
[dumped.except(:request_options), request_options]
4034
else
4135
[dumped, nil]

lib/openai/internal/util.rb

+16
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ def coerce_hash(input)
128128
input.respond_to?(:to_h) ? input.to_h : input
129129
end
130130
end
131+
132+
# @api private
133+
#
134+
# @param input [Object]
135+
#
136+
# @raise [ArgumentError]
137+
# @return [Hash{Object=>Object}, nil]
138+
def coerce_hash!(input)
139+
case coerce_hash(input)
140+
in Hash | nil => coerced
141+
coerced
142+
else
143+
message = "Expected a #{Hash} or #{OpenAI::Internal::Type::BaseModel}, got #{data.inspect}"
144+
raise ArgumentError.new(message)
145+
end
146+
end
131147
end
132148

133149
class << self

lib/openai/resources/beta/assistants.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def create(params)
5252
path: "assistants",
5353
body: parsed,
5454
model: OpenAI::Models::Beta::Assistant,
55-
options: options
55+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
5656
)
5757
end
5858

@@ -72,7 +72,7 @@ def retrieve(assistant_id, params = {})
7272
method: :get,
7373
path: ["assistants/%1$s", assistant_id],
7474
model: OpenAI::Models::Beta::Assistant,
75-
options: params[:request_options]
75+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **params[:request_options].to_h}
7676
)
7777
end
7878

@@ -126,7 +126,7 @@ def update(assistant_id, params = {})
126126
path: ["assistants/%1$s", assistant_id],
127127
body: parsed,
128128
model: OpenAI::Models::Beta::Assistant,
129-
options: options
129+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
130130
)
131131
end
132132

@@ -162,7 +162,7 @@ def list(params = {})
162162
query: parsed,
163163
page: OpenAI::Internal::CursorPage,
164164
model: OpenAI::Models::Beta::Assistant,
165-
options: options
165+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
166166
)
167167
end
168168

@@ -182,7 +182,7 @@ def delete(assistant_id, params = {})
182182
method: :delete,
183183
path: ["assistants/%1$s", assistant_id],
184184
model: OpenAI::Models::Beta::AssistantDeleted,
185-
options: params[:request_options]
185+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **params[:request_options].to_h}
186186
)
187187
end
188188

lib/openai/resources/beta/threads.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def create(params = {})
3737
path: "threads",
3838
body: parsed,
3939
model: OpenAI::Models::Beta::Thread,
40-
options: options
40+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
4141
)
4242
end
4343

@@ -57,7 +57,7 @@ def retrieve(thread_id, params = {})
5757
method: :get,
5858
path: ["threads/%1$s", thread_id],
5959
model: OpenAI::Models::Beta::Thread,
60-
options: params[:request_options]
60+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **params[:request_options].to_h}
6161
)
6262
end
6363

@@ -87,7 +87,7 @@ def update(thread_id, params = {})
8787
path: ["threads/%1$s", thread_id],
8888
body: parsed,
8989
model: OpenAI::Models::Beta::Thread,
90-
options: options
90+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
9191
)
9292
end
9393

@@ -107,7 +107,7 @@ def delete(thread_id, params = {})
107107
method: :delete,
108108
path: ["threads/%1$s", thread_id],
109109
model: OpenAI::Models::Beta::ThreadDeleted,
110-
options: params[:request_options]
110+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **params[:request_options].to_h}
111111
)
112112
end
113113

@@ -178,7 +178,7 @@ def create_and_run(params)
178178
path: "threads/runs",
179179
body: parsed,
180180
model: OpenAI::Models::Beta::Threads::Run,
181-
options: options
181+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
182182
)
183183
end
184184

@@ -257,7 +257,7 @@ def stream_raw(params)
257257
body: parsed,
258258
stream: OpenAI::Internal::Stream,
259259
model: OpenAI::Models::Beta::AssistantStreamEvent,
260-
options: options
260+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
261261
)
262262
end
263263

lib/openai/resources/beta/threads/messages.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def create(thread_id, params)
3535
path: ["threads/%1$s/messages", thread_id],
3636
body: parsed,
3737
model: OpenAI::Models::Beta::Threads::Message,
38-
options: options
38+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
3939
)
4040
end
4141

@@ -66,7 +66,7 @@ def retrieve(message_id, params)
6666
method: :get,
6767
path: ["threads/%1$s/messages/%2$s", thread_id, message_id],
6868
model: OpenAI::Models::Beta::Threads::Message,
69-
options: options
69+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
7070
)
7171
end
7272

@@ -100,7 +100,7 @@ def update(message_id, params)
100100
path: ["threads/%1$s/messages/%2$s", thread_id, message_id],
101101
body: parsed,
102102
model: OpenAI::Models::Beta::Threads::Message,
103-
options: options
103+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
104104
)
105105
end
106106

@@ -141,7 +141,7 @@ def list(thread_id, params = {})
141141
query: parsed,
142142
page: OpenAI::Internal::CursorPage,
143143
model: OpenAI::Models::Beta::Threads::Message,
144-
options: options
144+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
145145
)
146146
end
147147

@@ -168,7 +168,7 @@ def delete(message_id, params)
168168
method: :delete,
169169
path: ["threads/%1$s/messages/%2$s", thread_id, message_id],
170170
model: OpenAI::Models::Beta::Threads::MessageDeleted,
171-
options: options
171+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
172172
)
173173
end
174174

lib/openai/resources/beta/threads/runs.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def create(thread_id, params)
8686
query: parsed.slice(*query_params),
8787
body: parsed.except(*query_params),
8888
model: OpenAI::Models::Beta::Threads::Run,
89-
options: options
89+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
9090
)
9191
end
9292

@@ -171,7 +171,7 @@ def create_stream_raw(thread_id, params)
171171
body: parsed.except(*query_params),
172172
stream: OpenAI::Internal::Stream,
173173
model: OpenAI::Models::Beta::AssistantStreamEvent,
174-
options: options
174+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
175175
)
176176
end
177177

@@ -202,7 +202,7 @@ def retrieve(run_id, params)
202202
method: :get,
203203
path: ["threads/%1$s/runs/%2$s", thread_id, run_id],
204204
model: OpenAI::Models::Beta::Threads::Run,
205-
options: options
205+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
206206
)
207207
end
208208

@@ -237,7 +237,7 @@ def update(run_id, params)
237237
path: ["threads/%1$s/runs/%2$s", thread_id, run_id],
238238
body: parsed,
239239
model: OpenAI::Models::Beta::Threads::Run,
240-
options: options
240+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
241241
)
242242
end
243243

@@ -275,7 +275,7 @@ def list(thread_id, params = {})
275275
query: parsed,
276276
page: OpenAI::Internal::CursorPage,
277277
model: OpenAI::Models::Beta::Threads::Run,
278-
options: options
278+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
279279
)
280280
end
281281

@@ -302,7 +302,7 @@ def cancel(run_id, params)
302302
method: :post,
303303
path: ["threads/%1$s/runs/%2$s/cancel", thread_id, run_id],
304304
model: OpenAI::Models::Beta::Threads::Run,
305-
options: options
305+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
306306
)
307307
end
308308

@@ -346,7 +346,7 @@ def submit_tool_outputs(run_id, params)
346346
path: ["threads/%1$s/runs/%2$s/submit_tool_outputs", thread_id, run_id],
347347
body: parsed,
348348
model: OpenAI::Models::Beta::Threads::Run,
349-
options: options
349+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
350350
)
351351
end
352352

@@ -393,7 +393,7 @@ def submit_tool_outputs_stream_raw(run_id, params)
393393
body: parsed,
394394
stream: OpenAI::Internal::Stream,
395395
model: OpenAI::Models::Beta::AssistantStreamEvent,
396-
options: options
396+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
397397
)
398398
end
399399

lib/openai/resources/beta/threads/runs/steps.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def retrieve(step_id, params)
4242
path: ["threads/%1$s/runs/%2$s/steps/%3$s", thread_id, run_id, step_id],
4343
query: parsed,
4444
model: OpenAI::Models::Beta::Threads::Runs::RunStep,
45-
options: options
45+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
4646
)
4747
end
4848

@@ -89,7 +89,7 @@ def list(run_id, params)
8989
query: parsed,
9090
page: OpenAI::Internal::CursorPage,
9191
model: OpenAI::Models::Beta::Threads::Runs::RunStep,
92-
options: options
92+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
9393
)
9494
end
9595

lib/openai/resources/vector_stores.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def create(params = {})
4040
path: "vector_stores",
4141
body: parsed,
4242
model: OpenAI::Models::VectorStore,
43-
options: options
43+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
4444
)
4545
end
4646

@@ -60,7 +60,7 @@ def retrieve(vector_store_id, params = {})
6060
method: :get,
6161
path: ["vector_stores/%1$s", vector_store_id],
6262
model: OpenAI::Models::VectorStore,
63-
options: params[:request_options]
63+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **params[:request_options].to_h}
6464
)
6565
end
6666

@@ -91,7 +91,7 @@ def update(vector_store_id, params = {})
9191
path: ["vector_stores/%1$s", vector_store_id],
9292
body: parsed,
9393
model: OpenAI::Models::VectorStore,
94-
options: options
94+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
9595
)
9696
end
9797

@@ -127,7 +127,7 @@ def list(params = {})
127127
query: parsed,
128128
page: OpenAI::Internal::CursorPage,
129129
model: OpenAI::Models::VectorStore,
130-
options: options
130+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
131131
)
132132
end
133133

@@ -147,7 +147,7 @@ def delete(vector_store_id, params = {})
147147
method: :delete,
148148
path: ["vector_stores/%1$s", vector_store_id],
149149
model: OpenAI::Models::VectorStoreDeleted,
150-
options: params[:request_options]
150+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **params[:request_options].to_h}
151151
)
152152
end
153153

@@ -185,7 +185,7 @@ def search(vector_store_id, params)
185185
body: parsed,
186186
page: OpenAI::Internal::Page,
187187
model: OpenAI::Models::VectorStoreSearchResponse,
188-
options: options
188+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
189189
)
190190
end
191191

lib/openai/resources/vector_stores/file_batches.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def create(vector_store_id, params)
3333
path: ["vector_stores/%1$s/file_batches", vector_store_id],
3434
body: parsed,
3535
model: OpenAI::Models::VectorStores::VectorStoreFileBatch,
36-
options: options
36+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
3737
)
3838
end
3939

@@ -60,7 +60,7 @@ def retrieve(batch_id, params)
6060
method: :get,
6161
path: ["vector_stores/%1$s/file_batches/%2$s", vector_store_id, batch_id],
6262
model: OpenAI::Models::VectorStores::VectorStoreFileBatch,
63-
options: options
63+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
6464
)
6565
end
6666

@@ -88,7 +88,7 @@ def cancel(batch_id, params)
8888
method: :post,
8989
path: ["vector_stores/%1$s/file_batches/%2$s/cancel", vector_store_id, batch_id],
9090
model: OpenAI::Models::VectorStores::VectorStoreFileBatch,
91-
options: options
91+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
9292
)
9393
end
9494

@@ -135,7 +135,7 @@ def list_files(batch_id, params)
135135
query: parsed,
136136
page: OpenAI::Internal::CursorPage,
137137
model: OpenAI::Models::VectorStores::VectorStoreFile,
138-
options: options
138+
options: {extra_headers: {"OpenAI-Beta" => "assistants=v2"}, **options}
139139
)
140140
end
141141

0 commit comments

Comments
 (0)