fix: Enhance VLM response parsing to handle markdown code blocks
This commit is contained in:
@@ -174,13 +174,33 @@ Output ONLY the JSON object, no additional text."""
|
|||||||
|
|
||||||
def _parse_response(self, response: str) -> CharacterAnalysisResult:
|
def _parse_response(self, response: str) -> CharacterAnalysisResult:
|
||||||
"""Parse VLM response into CharacterAnalysisResult"""
|
"""Parse VLM response into CharacterAnalysisResult"""
|
||||||
|
if not response:
|
||||||
|
logger.warning("Empty VLM response")
|
||||||
|
return CharacterAnalysisResult()
|
||||||
|
|
||||||
|
# Log full response for debugging
|
||||||
|
logger.debug(f"Full VLM response:\n{response}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Remove markdown code blocks if present
|
||||||
|
cleaned = response.strip()
|
||||||
|
if cleaned.startswith("```json"):
|
||||||
|
cleaned = cleaned[7:]
|
||||||
|
elif cleaned.startswith("```"):
|
||||||
|
cleaned = cleaned[3:]
|
||||||
|
if cleaned.endswith("```"):
|
||||||
|
cleaned = cleaned[:-3]
|
||||||
|
cleaned = cleaned.strip()
|
||||||
|
|
||||||
# Try to extract JSON from response
|
# Try to extract JSON from response
|
||||||
match = re.search(r'\{[\s\S]*\}', response)
|
match = re.search(r'\{[\s\S]*\}', cleaned)
|
||||||
if match:
|
if match:
|
||||||
data = json.loads(match.group())
|
json_str = match.group()
|
||||||
|
logger.debug(f"Extracted JSON: {json_str[:200]}...")
|
||||||
|
data = json.loads(json_str)
|
||||||
else:
|
else:
|
||||||
data = json.loads(response)
|
logger.warning(f"No JSON found in response, trying direct parse")
|
||||||
|
data = json.loads(cleaned)
|
||||||
|
|
||||||
result = CharacterAnalysisResult(
|
result = CharacterAnalysisResult(
|
||||||
appearance_description=data.get("appearance_description", ""),
|
appearance_description=data.get("appearance_description", ""),
|
||||||
@@ -193,11 +213,18 @@ Output ONLY the JSON object, no additional text."""
|
|||||||
|
|
||||||
except (json.JSONDecodeError, KeyError) as e:
|
except (json.JSONDecodeError, KeyError) as e:
|
||||||
logger.warning(f"Failed to parse VLM response: {e}")
|
logger.warning(f"Failed to parse VLM response: {e}")
|
||||||
|
logger.debug(f"Response that failed to parse: {response[:500]}")
|
||||||
|
|
||||||
# Try to use the raw response as appearance description
|
# Try to use the raw response as appearance description (fallback)
|
||||||
if len(response) < 500 and len(response) > 20:
|
if response and 20 < len(response) < 500:
|
||||||
return CharacterAnalysisResult(
|
# Clean up the response
|
||||||
appearance_description=response.strip()
|
fallback = response.strip()
|
||||||
)
|
if "```" in fallback:
|
||||||
|
fallback = re.sub(r'```.*?```', '', fallback, flags=re.DOTALL).strip()
|
||||||
|
if fallback:
|
||||||
|
logger.info(f"Using raw response as appearance: {fallback[:80]}...")
|
||||||
|
return CharacterAnalysisResult(
|
||||||
|
appearance_description=fallback
|
||||||
|
)
|
||||||
|
|
||||||
return CharacterAnalysisResult()
|
return CharacterAnalysisResult()
|
||||||
|
|||||||
Reference in New Issue
Block a user