This commit is contained in:
ruoyunbai 2025-09-29 13:59:36 +08:00
parent 22037a4309
commit 19885f5e0b

View File

@ -1,4 +1,6 @@
import json import json
import ast
import re
import logging import logging
from typing import List, Dict, Any, Optional, Union, Tuple from typing import List, Dict, Any, Optional, Union, Tuple
import requests import requests
@ -728,10 +730,26 @@ class InputParser:
# 处理JSON字符串形式 # 处理JSON字符串形式
if isinstance(candidate, str): if isinstance(candidate, str):
candidate_str = candidate.strip()
if candidate_str:
try: try:
candidate = json.loads(candidate) candidate = json.loads(candidate_str)
except json.JSONDecodeError: except json.JSONDecodeError:
self.logger.warning(f"Schema for '{name}' is a string but not valid JSON; skipping this model.") fallback_literal = candidate_str
fallback_literal = re.sub(r'\bnull\b', 'None', fallback_literal, flags=re.IGNORECASE)
fallback_literal = re.sub(r'\btrue\b', 'True', fallback_literal, flags=re.IGNORECASE)
fallback_literal = re.sub(r'\bfalse\b', 'False', fallback_literal, flags=re.IGNORECASE)
try:
candidate = ast.literal_eval(fallback_literal)
self.logger.info(
f"Schema for '{name}' parsed using ast.literal_eval fallback after JSON decode failure."
)
except (ValueError, SyntaxError):
self.logger.warning(
f"Schema for '{name}' is a string but not valid JSON; skipping this model."
)
candidate = None
else:
candidate = None candidate = None
if isinstance(candidate, dict): if isinstance(candidate, dict):
@ -741,10 +759,26 @@ class InputParser:
else: else:
model = None model = None
elif isinstance(raw_model_data, str): elif isinstance(raw_model_data, str):
raw_model_str = raw_model_data.strip()
if raw_model_str:
try: try:
model = json.loads(raw_model_data) model = json.loads(raw_model_str)
except json.JSONDecodeError: except json.JSONDecodeError:
self.logger.warning(f"Schema for '{name}' returned as string is not valid JSON; skipping this model.") fallback_literal = raw_model_str
fallback_literal = re.sub(r'\bnull\b', 'None', fallback_literal, flags=re.IGNORECASE)
fallback_literal = re.sub(r'\btrue\b', 'True', fallback_literal, flags=re.IGNORECASE)
fallback_literal = re.sub(r'\bfalse\b', 'False', fallback_literal, flags=re.IGNORECASE)
try:
model = ast.literal_eval(fallback_literal)
self.logger.info(
f"Schema for '{name}' string parsed using ast.literal_eval fallback after JSON decode failure."
)
except (ValueError, SyntaxError):
self.logger.warning(
f"Schema for '{name}' returned as string is not valid JSON; skipping this model."
)
model = None
else:
model = None model = None
if not isinstance(model, dict): if not isinstance(model, dict):