fix: Handle nested JSON structures in VLM response parsing
This commit is contained in:
@@ -242,13 +242,43 @@ Output ONLY the JSON object, no additional text."""
|
|||||||
logger.warning(f"No JSON found in response, trying direct parse")
|
logger.warning(f"No JSON found in response, trying direct parse")
|
||||||
data = json.loads(cleaned)
|
data = json.loads(cleaned)
|
||||||
|
|
||||||
|
# Handle nested JSON structures - flatten to strings
|
||||||
|
appearance = data.get("appearance_description", "")
|
||||||
|
if isinstance(appearance, dict):
|
||||||
|
# Flatten nested object to descriptive string
|
||||||
|
parts = []
|
||||||
|
for key, value in appearance.items():
|
||||||
|
if isinstance(value, dict):
|
||||||
|
# Further nested (e.g., hair: {color, length, style})
|
||||||
|
details = ", ".join(f"{k}: {v}" for k, v in value.items())
|
||||||
|
parts.append(f"{key} ({details})")
|
||||||
|
else:
|
||||||
|
parts.append(f"{key}: {value}")
|
||||||
|
appearance = "; ".join(parts)
|
||||||
|
|
||||||
|
clothing = data.get("clothing_description", "")
|
||||||
|
if isinstance(clothing, dict):
|
||||||
|
# Flatten nested clothing description
|
||||||
|
parts = []
|
||||||
|
for person, items in clothing.items():
|
||||||
|
if isinstance(items, dict):
|
||||||
|
details = ", ".join(f"{k}: {v}" for k, v in items.items())
|
||||||
|
parts.append(f"{person} ({details})")
|
||||||
|
else:
|
||||||
|
parts.append(f"{person}: {items}")
|
||||||
|
clothing = "; ".join(parts)
|
||||||
|
|
||||||
|
distinctive = data.get("distinctive_features", [])
|
||||||
|
if not isinstance(distinctive, list):
|
||||||
|
distinctive = [str(distinctive)]
|
||||||
|
|
||||||
result = CharacterAnalysisResult(
|
result = CharacterAnalysisResult(
|
||||||
appearance_description=data.get("appearance_description", ""),
|
appearance_description=appearance,
|
||||||
clothing_description=data.get("clothing_description", ""),
|
clothing_description=clothing,
|
||||||
distinctive_features=data.get("distinctive_features", []),
|
distinctive_features=distinctive,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f"Character analysis extracted: {result.appearance_description[:80]}...")
|
logger.info(f"Character analysis extracted: {result.appearance_description[:80] if result.appearance_description else 'empty'}...")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
except (json.JSONDecodeError, KeyError) as e:
|
except (json.JSONDecodeError, KeyError) as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user