Pulastya B commited on
Commit
a126ee5
·
1 Parent(s): 711046d

Fix Mistral API response format: handle content as list or string

Browse files
Files changed (1) hide show
  1. src/orchestrator.py +20 -3
src/orchestrator.py CHANGED
@@ -406,6 +406,23 @@ class DataScienceCopilot:
406
  "create_binned_features": create_binned_features,
407
  }
408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
  def _build_system_prompt(self) -> str:
410
  """Build comprehensive system prompt for the copilot."""
411
  return """You are an autonomous Data Science Agent. You EXECUTE tasks, not advise.
@@ -2220,7 +2237,7 @@ You are a DOER. Complete workflows based on user intent."""
2220
 
2221
  response_message = response.choices[0].message
2222
  tool_calls = response_message.tool_calls
2223
- final_content = response_message.content
2224
 
2225
  except Exception as mistral_error:
2226
  error_str = str(mistral_error)
@@ -2250,7 +2267,7 @@ You are a DOER. Complete workflows based on user intent."""
2250
 
2251
  response_message = response.choices[0].message
2252
  tool_calls = response_message.tool_calls
2253
- final_content = response_message.content
2254
 
2255
  except Exception as groq_error:
2256
  # Check if it's a rate limit error (429)
@@ -2304,7 +2321,7 @@ You are a DOER. Complete workflows based on user intent."""
2304
 
2305
  response_message = response.choices[0].message
2306
  tool_calls = response_message.tool_calls
2307
- final_content = response_message.content
2308
  else:
2309
  # Not a rate limit error, re-raise
2310
  raise
 
406
  "create_binned_features": create_binned_features,
407
  }
408
 
409
+ def _extract_content_text(self, content) -> str:
410
+ """Extract text from message content (handles both string and list formats)"""
411
+ if content is None:
412
+ return None
413
+ if isinstance(content, str):
414
+ return content
415
+ if isinstance(content, list):
416
+ # Content is list of objects like [{'type': 'text', 'text': '...'}]
417
+ text_parts = []
418
+ for item in content:
419
+ if isinstance(item, dict) and 'text' in item:
420
+ text_parts.append(item['text'])
421
+ elif isinstance(item, str):
422
+ text_parts.append(item)
423
+ return ''.join(text_parts)
424
+ return str(content)
425
+
426
  def _build_system_prompt(self) -> str:
427
  """Build comprehensive system prompt for the copilot."""
428
  return """You are an autonomous Data Science Agent. You EXECUTE tasks, not advise.
 
2237
 
2238
  response_message = response.choices[0].message
2239
  tool_calls = response_message.tool_calls
2240
+ final_content = self._extract_content_text(response_message.content)
2241
 
2242
  except Exception as mistral_error:
2243
  error_str = str(mistral_error)
 
2267
 
2268
  response_message = response.choices[0].message
2269
  tool_calls = response_message.tool_calls
2270
+ final_content = self._extract_content_text(response_message.content)
2271
 
2272
  except Exception as groq_error:
2273
  # Check if it's a rate limit error (429)
 
2321
 
2322
  response_message = response.choices[0].message
2323
  tool_calls = response_message.tool_calls
2324
+ final_content = self._extract_content_text(response_message.content)
2325
  else:
2326
  # Not a rate limit error, re-raise
2327
  raise