Skip to content

Conversation

Copy link

Copilot AI commented Oct 13, 2025

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:

json.dump(str(results), fout, ensure_ascii=False)

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 allow json.dump() to properly serialize the data structure:

json.dump(results, fout, ensure_ascii=False)

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

  • Proper JSON Structure: Saved results are now valid, well-formed JSON that can be parsed by any standard JSON library
  • Better Interoperability: Output files can be consumed by external tools, scripts, and analytics pipelines without custom parsing logic
  • Type Preservation: When loading the JSON back, the data maintains its proper structure (list of dictionaries) rather than being a single string
  • Backward Compatibility: Any code that was working around the bug by parsing the string will need to be updated to handle proper JSON

Files Modified

  • pyabsa/tasks/TextClassification/prediction/text_classifier.py
  • pyabsa/tasks/AspectPolarityClassification/prediction/sentiment_classifier.py
  • pyabsa/tasks/CodeDefectDetection/prediction/code_defect_detector.py
  • pyabsa/tasks/TextAdversarialDefense/prediction/tad_classifier.py
  • pyabsa/tasks/RNAClassification/prediction/rna_classifier.py
  • pyabsa/tasks/RNARegression/prediction/rna_regressor.py
  • pyabsa/tasks/_Archive/ProteinRegression/prediction/protein_regressor.py
  • pyabsa/tasks/_Archive/RNAClassification/prediction/rna_classifier.py
  • pyabsa/tasks/_Archive/RNARegression/prediction/rna_regressor.py

All changes are minimal and surgical - only the specific line causing the issue was modified in each file.

Original prompt

try to resolve the issue you found


💡 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.

Copilot AI changed the title [WIP] Fix issue identified during testing Fix JSON serialization bug in prediction result saving across all predictor classes Oct 13, 2025
Copilot AI requested a review from yangheng95 October 13, 2025 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants