Fix JSON serialization bug in prediction result saving across all predictor classes #419
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Multiple predictor classes across the PyABSA framework were incorrectly serializing prediction results to JSON files. The code was converting the results list to a string before dumping to JSON:
This produced malformed JSON output that contained a Python string representation rather than proper JSON structures:
Before (Incorrect Output):
"[{'ex_id': 0, 'text': 'Sample text', 'label': 'positive', 'confidence': 0.95}]"Note the outer quotes and Python-style single quotes - this is a JSON string containing the string representation of a Python list, not an actual JSON array.
Solution
Removed the
str()wrapper to allowjson.dump()to properly serialize the data structure:After (Correct Output):
[{"ex_id": 0, "text": "Sample text", "label": "positive", "confidence": 0.95}]This is now a proper JSON array with correctly formatted objects.
Impact
Files Modified
pyabsa/tasks/TextClassification/prediction/text_classifier.pypyabsa/tasks/AspectPolarityClassification/prediction/sentiment_classifier.pypyabsa/tasks/CodeDefectDetection/prediction/code_defect_detector.pypyabsa/tasks/TextAdversarialDefense/prediction/tad_classifier.pypyabsa/tasks/RNAClassification/prediction/rna_classifier.pypyabsa/tasks/RNARegression/prediction/rna_regressor.pypyabsa/tasks/_Archive/ProteinRegression/prediction/protein_regressor.pypyabsa/tasks/_Archive/RNAClassification/prediction/rna_classifier.pypyabsa/tasks/_Archive/RNARegression/prediction/rna_regressor.pyAll changes are minimal and surgical - only the specific line causing the issue was modified in each file.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.