Sanchit7 commited on
Commit
22e811b
·
1 Parent(s): ed355ce

$(cat <<EOF

Browse files

Use Gemini JSON mode and add debug logging

- Enable response_mime_type=application/json for clean JSON output
- Add debug logging for raw Gemini responses
- Add error logging with problematic JSON on parse failures
- Should eliminate control character issues in Gemini synthesis

:robot: Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
EOF
)

Files changed (1) hide show
  1. src/agents/synthesis_agent.py +21 -3
src/agents/synthesis_agent.py CHANGED
@@ -234,15 +234,27 @@ IMPORTANT: Return ONLY valid JSON. No markdown, no code blocks, no extra text. U
234
  }
235
  """
236
 
237
- # Call Gemini
238
  logger.info(f"Calling Gemini 2.0 Flash for {request.ticker} synthesis...")
239
- response = self.model.generate_content(prompt)
 
 
 
 
 
 
 
 
 
240
 
241
  # Parse response
242
  import json
243
  import re
244
  response_text = response.text.strip()
245
 
 
 
 
246
  # Extract JSON from markdown code blocks if present
247
  if "```json" in response_text:
248
  response_text = response_text.split("```json")[1].split("```")[0].strip()
@@ -264,7 +276,13 @@ IMPORTANT: Return ONLY valid JSON. No markdown, no code blocks, no extra text. U
264
  response_text = re.sub(r'"\s*\n\s*', '" ', response_text)
265
  response_text = re.sub(r'\s*\n\s*"', ' "', response_text)
266
 
267
- result = json.loads(response_text)
 
 
 
 
 
 
268
 
269
  # Convert to InvestmentRecommendation
270
  risks = [
 
234
  }
235
  """
236
 
237
+ # Call Gemini with JSON response mode
238
  logger.info(f"Calling Gemini 2.0 Flash for {request.ticker} synthesis...")
239
+
240
+ # Configure for JSON output
241
+ generation_config = {
242
+ "response_mime_type": "application/json"
243
+ }
244
+
245
+ response = self.model.generate_content(
246
+ prompt,
247
+ generation_config=generation_config
248
+ )
249
 
250
  # Parse response
251
  import json
252
  import re
253
  response_text = response.text.strip()
254
 
255
+ # Log raw response for debugging
256
+ logger.debug(f"Gemini raw response (first 500 chars): {response_text[:500]}")
257
+
258
  # Extract JSON from markdown code blocks if present
259
  if "```json" in response_text:
260
  response_text = response_text.split("```json")[1].split("```")[0].strip()
 
276
  response_text = re.sub(r'"\s*\n\s*', '" ', response_text)
277
  response_text = re.sub(r'\s*\n\s*"', ' "', response_text)
278
 
279
+ try:
280
+ result = json.loads(response_text)
281
+ except json.JSONDecodeError as e:
282
+ # Log the problematic JSON for debugging
283
+ logger.error(f"JSON decode error: {e}")
284
+ logger.error(f"Problematic JSON (first 1000 chars): {response_text[:1000]}")
285
+ raise
286
 
287
  # Convert to InvestmentRecommendation
288
  risks = [