Spaces:
Running

axeronis commited on
Commit
220c548
·
1 Parent(s): f6403d2

fix: exhaustive schema cleaning for Gemini (remove default, title, etc.)

Browse files
Files changed (1) hide show
  1. backend/app/services/gemini_client.py +28 -16
backend/app/services/gemini_client.py CHANGED
@@ -439,29 +439,41 @@ Respond with ONLY a number from 0 to 100."""
439
  """
440
 
441
  try:
442
- # Helper: Strip 'default' keys from schema (Gemini API doesn't support them)
443
- def strip_defaults_from_schema(schema: dict) -> dict:
444
- """Recursively remove 'default' keys from JSON schema."""
445
- if isinstance(schema, dict):
446
- return {
447
- k: strip_defaults_from_schema(v)
448
- for k, v in schema.items()
449
- if k != 'default'
450
- }
451
- elif isinstance(schema, list):
452
- return [strip_defaults_from_schema(item) for item in schema]
453
- return schema
 
 
 
 
 
 
 
 
454
 
455
- # Get schema and strip defaults
456
  raw_schema = AISummary.model_json_schema()
457
- clean_schema = strip_defaults_from_schema(raw_schema)
 
 
 
 
458
 
459
  # Use new google.genai SDK with JSON response schema
460
  summary_config = types.GenerateContentConfig(
461
- temperature=0.1, # Low temperature for consistent analysis
462
  max_output_tokens=4096,
463
  response_mime_type="application/json",
464
- response_schema=clean_schema # Schema without defaults
465
  )
466
 
467
  # Run in executor to avoid blocking
 
439
  """
440
 
441
  try:
442
+ # Helper: Clean schema for Gemini (it's extremely picky about standard JSON Schema keys)
443
+ def clean_schema_for_gemini(schema: dict) -> dict:
444
+ """Recursively remove keys that Gemini API rejects in standard mode."""
445
+ if not isinstance(schema, dict):
446
+ return schema
447
+
448
+ # Forbidden keys in Gemini's Controlled Generation
449
+ forbidden = {'default', 'title', 'example', 'examples', 'description_html'}
450
+
451
+ cleaned = {}
452
+ for k, v in schema.items():
453
+ if k in forbidden:
454
+ continue
455
+ if isinstance(v, dict):
456
+ cleaned[k] = clean_schema_for_gemini(v)
457
+ elif isinstance(v, list):
458
+ cleaned[k] = [clean_schema_for_gemini(item) if isinstance(item, dict) else item for item in v]
459
+ else:
460
+ cleaned[k] = v
461
+ return cleaned
462
 
463
+ # Get raw schema
464
  raw_schema = AISummary.model_json_schema()
465
+ # Clean it thoroughly
466
+ clean_schema = clean_schema_for_gemini(raw_schema)
467
+
468
+ # Debug: log the keys we are sending
469
+ logger.info(f"🧠 Sending cleaned schema to Gemini: {list(clean_schema.get('properties', {}).keys())}")
470
 
471
  # Use new google.genai SDK with JSON response schema
472
  summary_config = types.GenerateContentConfig(
473
+ temperature=0.1,
474
  max_output_tokens=4096,
475
  response_mime_type="application/json",
476
+ response_schema=clean_schema
477
  )
478
 
479
  # Run in executor to avoid blocking