@@ -185,12 +185,58 @@ def generate(
185185 if self .seed is not None :
186186 request_params ["seed" ] = self .seed
187187
188- response = self .client .chat .completions .create (** request_params )
189-
190- response_time = time .time () - start_time
191-
192- choice = response .choices [0 ]
193- content = choice .message .content or ""
188+ try :
189+ response = self .client .chat .completions .create (** request_params )
190+ response_time = time .time () - start_time
191+
192+ choice = response .choices [0 ]
193+ content = choice .message .content or ""
194+ except (ValueError , TypeError ) as json_error :
195+ # Catch JSON decode errors and other parsing issues
196+ response_time = time .time () - start_time
197+ error_msg = f"JSON decode error from OpenRouter: { str (json_error )} "
198+ print (f"🔴 DEBUG: { error_msg } " )
199+
200+ # Try to get the raw response if possible
201+ try :
202+ if hasattr (json_error , 'response' ):
203+ raw_response = json_error .response
204+ if hasattr (raw_response , 'text' ):
205+ print (f"🔴 DEBUG: Raw response text: { str (raw_response .text )[:1000 ]} ..." )
206+ elif hasattr (raw_response , 'content' ):
207+ print (f"🔴 DEBUG: Raw response content: { str (raw_response .content )[:1000 ]} ..." )
208+ except Exception :
209+ print ("🔴 DEBUG: Could not extract raw response" )
210+
211+ return ModelResponse (
212+ content = "" ,
213+ model = self .model ,
214+ response_time = response_time ,
215+ error = error_msg ,
216+ )
217+ except Exception as api_error :
218+ # Handle other API errors including JSON decode issues
219+ response_time = time .time () - start_time
220+ error_msg = f"API error from OpenRouter: { str (api_error )} "
221+ print (f"🔴 DEBUG: { error_msg } " )
222+
223+ # Try to extract response details
224+ if hasattr (api_error , 'response' ):
225+ try :
226+ resp = api_error .response
227+ if hasattr (resp , 'text' ):
228+ print (f"🔴 DEBUG: Error response text: { str (resp .text )[:1000 ]} ..." )
229+ elif hasattr (resp , 'content' ):
230+ print (f"🔴 DEBUG: Error response content: { str (resp .content )[:1000 ]} ..." )
231+ except Exception :
232+ print ("🔴 DEBUG: Could not extract error response details" )
233+
234+ return ModelResponse (
235+ content = "" ,
236+ model = self .model ,
237+ response_time = response_time ,
238+ error = error_msg ,
239+ )
194240
195241 # Debug logging for empty responses
196242 if not content .strip ():
@@ -253,12 +299,58 @@ def chat(
253299 if self .seed is not None :
254300 request_params ["seed" ] = self .seed
255301
256- response = self .client .chat .completions .create (** request_params )
257-
258- response_time = time .time () - start_time
259-
260- choice = response .choices [0 ]
261- content = choice .message .content or ""
302+ try :
303+ response = self .client .chat .completions .create (** request_params )
304+ response_time = time .time () - start_time
305+
306+ choice = response .choices [0 ]
307+ content = choice .message .content or ""
308+ except (ValueError , TypeError ) as json_error :
309+ # Catch JSON decode errors and other parsing issues
310+ response_time = time .time () - start_time
311+ error_msg = f"JSON decode error from OpenRouter: { str (json_error )} "
312+ print (f"🔴 DEBUG: { error_msg } " )
313+
314+ # Try to get the raw response if possible
315+ try :
316+ if hasattr (json_error , 'response' ):
317+ raw_response = json_error .response
318+ if hasattr (raw_response , 'text' ):
319+ print (f"🔴 DEBUG: Raw response text: { str (raw_response .text )[:1000 ]} ..." )
320+ elif hasattr (raw_response , 'content' ):
321+ print (f"🔴 DEBUG: Raw response content: { str (raw_response .content )[:1000 ]} ..." )
322+ except Exception :
323+ print ("🔴 DEBUG: Could not extract raw response" )
324+
325+ return ModelResponse (
326+ content = "" ,
327+ model = self .model ,
328+ response_time = response_time ,
329+ error = error_msg ,
330+ )
331+ except Exception as api_error :
332+ # Handle other API errors including JSON decode issues
333+ response_time = time .time () - start_time
334+ error_msg = f"API error from OpenRouter: { str (api_error )} "
335+ print (f"🔴 DEBUG: { error_msg } " )
336+
337+ # Try to extract response details
338+ if hasattr (api_error , 'response' ):
339+ try :
340+ resp = api_error .response
341+ if hasattr (resp , 'text' ):
342+ print (f"🔴 DEBUG: Error response text: { str (resp .text )[:1000 ]} ..." )
343+ elif hasattr (resp , 'content' ):
344+ print (f"🔴 DEBUG: Error response content: { str (resp .content )[:1000 ]} ..." )
345+ except Exception :
346+ print ("🔴 DEBUG: Could not extract error response details" )
347+
348+ return ModelResponse (
349+ content = "" ,
350+ model = self .model ,
351+ response_time = response_time ,
352+ error = error_msg ,
353+ )
262354
263355 # Debug logging for empty responses
264356 if not content .strip ():
0 commit comments