Skip to content

Commit ab8566a

Browse files
authored
Merge pull request #28 from sczheng189/main
Best effort sanitize error objects to ensure serialization
2 parents 0877809 + 942f5ba commit ab8566a

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
@@ -111,8 +111,8 @@ def format(self, record):
111111

112112
# List of Gemini models
113113
GEMINI_MODELS = [
114-
"gemini-2.5-pro-preview-03-25",
115-
"gemini-2.0-flash"
114+
"gemini-2.5-flash",
115+
"gemini-2.5-pro"
116116
]
117117

118118
# Helper function to clean schema for Gemini
@@ -1337,8 +1337,27 @@ async def create_message(
13371337
if key not in error_details and key not in ['args', '__traceback__']:
13381338
error_details[key] = str(value)
13391339

1340-
# Log all error details
1341-
logger.error(f"Error processing request: {json.dumps(error_details, indent=2)}")
1340+
# Helper function to safely serialize objects for JSON
1341+
def sanitize_for_json(obj):
1342+
"""递归地清理对象使其可以JSON序列化"""
1343+
if isinstance(obj, dict):
1344+
return {k: sanitize_for_json(v) for k, v in obj.items()}
1345+
elif isinstance(obj, list):
1346+
return [sanitize_for_json(item) for item in obj]
1347+
elif hasattr(obj, '__dict__'):
1348+
return sanitize_for_json(obj.__dict__)
1349+
elif hasattr(obj, 'text'):
1350+
return str(obj.text)
1351+
else:
1352+
try:
1353+
json.dumps(obj)
1354+
return obj
1355+
except (TypeError, ValueError):
1356+
return str(obj)
1357+
1358+
# Log all error details with safe serialization
1359+
sanitized_details = sanitize_for_json(error_details)
1360+
logger.error(f"Error processing request: {json.dumps(sanitized_details, indent=2)}")
13421361

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

0 commit comments

Comments
 (0)