@@ -104,7 +104,6 @@ def create(params)
104104 raise ArgumentError . new ( message )
105105 end
106106
107- # rubocop:disable Layout/LineLength
108107 model = nil
109108 tool_models = { }
110109 case parsed
@@ -157,11 +156,16 @@ def create(params)
157156 else
158157 end
159158
159+ # rubocop:disable Metrics/BlockLength
160160 unwrap = -> ( raw ) do
161161 if model . is_a? ( OpenAI ::StructuredOutput ::JsonSchemaConverter )
162162 raw [ :choices ] &.each do |choice |
163163 message = choice . fetch ( :message )
164- parsed = JSON . parse ( message . fetch ( :content ) , symbolize_names : true )
164+ begin
165+ parsed = JSON . parse ( message . fetch ( :content ) , symbolize_names : true )
166+ rescue JSON ::ParserError => e
167+ parsed = e
168+ end
165169 coerced = OpenAI ::Internal ::Type ::Converter . coerce ( model , parsed )
166170 message . store ( :parsed , coerced )
167171 end
@@ -171,15 +175,19 @@ def create(params)
171175 func = tool_call . fetch ( :function )
172176 next if ( model = tool_models [ func . fetch ( :name ) ] ) . nil?
173177
174- parsed = JSON . parse ( func . fetch ( :arguments ) , symbolize_names : true )
178+ begin
179+ parsed = JSON . parse ( func . fetch ( :arguments ) , symbolize_names : true )
180+ rescue JSON ::ParserError => e
181+ parsed = e
182+ end
175183 coerced = OpenAI ::Internal ::Type ::Converter . coerce ( model , parsed )
176184 func . store ( :parsed , coerced )
177185 end
178186 end
179187
180188 raw
181189 end
182- # rubocop:enable Layout/LineLength
190+ # rubocop:enable Metrics/BlockLength
183191
184192 @client . request (
185193 method : :post ,
0 commit comments