Skip to content

Commit 942f5ba

Browse files
committed
修复json错误
1 parent e9c8cf8 commit 942f5ba

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

server.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ def format(self, record):
108108

109109
# List of Gemini models
110110
GEMINI_MODELS = [
111-
"gemini-2.5-pro-preview-03-25",
112-
"gemini-2.0-flash"
111+
"gemini-2.5-flash",
112+
"gemini-2.5-pro"
113113
]
114114

115115
# Helper function to clean schema for Gemini
@@ -1320,8 +1320,27 @@ async def create_message(
13201320
if key not in error_details and key not in ['args', '__traceback__']:
13211321
error_details[key] = str(value)
13221322

1323-
# Log all error details
1324-
logger.error(f"Error processing request: {json.dumps(error_details, indent=2)}")
1323+
# Helper function to safely serialize objects for JSON
1324+
def sanitize_for_json(obj):
1325+
"""递归地清理对象使其可以JSON序列化"""
1326+
if isinstance(obj, dict):
1327+
return {k: sanitize_for_json(v) for k, v in obj.items()}
1328+
elif isinstance(obj, list):
1329+
return [sanitize_for_json(item) for item in obj]
1330+
elif hasattr(obj, '__dict__'):
1331+
return sanitize_for_json(obj.__dict__)
1332+
elif hasattr(obj, 'text'):
1333+
return str(obj.text)
1334+
else:
1335+
try:
1336+
json.dumps(obj)
1337+
return obj
1338+
except (TypeError, ValueError):
1339+
return str(obj)
1340+
1341+
# Log all error details with safe serialization
1342+
sanitized_details = sanitize_for_json(error_details)
1343+
logger.error(f"Error processing request: {json.dumps(sanitized_details, indent=2)}")
13251344

13261345
# Format error for response
13271346
error_message = f"Error: {str(e)}"

0 commit comments

Comments
 (0)