Spaces:
Running
Running
axeronis commited on
Commit ·
220c548
1
Parent(s): f6403d2
fix: exhaustive schema cleaning for Gemini (remove default, title, etc.)
Browse files
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:
|
| 443 |
-
def
|
| 444 |
-
"""Recursively remove
|
| 445 |
-
if isinstance(schema, dict):
|
| 446 |
-
return
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
-
# Get
|
| 456 |
raw_schema = AISummary.model_json_schema()
|
| 457 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
# Use new google.genai SDK with JSON response schema
|
| 460 |
summary_config = types.GenerateContentConfig(
|
| 461 |
-
temperature=0.1,
|
| 462 |
max_output_tokens=4096,
|
| 463 |
response_mime_type="application/json",
|
| 464 |
-
response_schema=clean_schema
|
| 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
|