141141function _M .read_response (ctx , res )
142142 local body_reader = res .body_reader
143143 if not body_reader then
144- core .log .error (" AI service sent no response body" )
144+ core .log .warn (" AI service sent no response body" )
145145 return 500
146146 end
147147
@@ -152,11 +152,14 @@ function _M.read_response(ctx, res)
152152 while true do
153153 local chunk , err = body_reader () -- will read chunk by chunk
154154 if err then
155- core .log .error (" failed to read response chunk: " , err )
156- break
155+ core .log .warn (" failed to read response chunk: " , err )
156+ if core .string .find (err , " timeout" ) then
157+ return 504
158+ end
159+ return 500
157160 end
158161 if not chunk then
159- break
162+ return
160163 end
161164
162165 ngx_print (chunk )
@@ -192,7 +195,8 @@ function _M.read_response(ctx, res)
192195
193196 -- usage field is null for non-last events, null is parsed as userdata type
194197 if data and data .usage and type (data .usage ) ~= " userdata" then
195- core .log .info (" got token usage from ai service: " , core .json .delay_encode (data .usage ))
198+ core .log .info (" got token usage from ai service: " ,
199+ core .json .delay_encode (data .usage ))
196200 ctx .ai_token_usage = {
197201 prompt_tokens = data .usage .prompt_tokens or 0 ,
198202 completion_tokens = data .usage .completion_tokens or 0 ,
@@ -208,7 +212,7 @@ function _M.read_response(ctx, res)
208212
209213 local raw_res_body , err = res :read_body ()
210214 if not raw_res_body then
211- core .log .error (" failed to read response body: " , err )
215+ core .log .warn (" failed to read response body: " , err )
212216 if core .string .find (err , " timeout" ) then
213217 return 504
214218 end
0 commit comments