Skip to content

fix: harden NewsService toJSON() against malformed LLM output#152

Open
shivv23 wants to merge 1 commit into
c2siorg:mainfrom
shivv23:fix/newsservice-json-parsing
Open

fix: harden NewsService toJSON() against malformed LLM output#152
shivv23 wants to merge 1 commit into
c2siorg:mainfrom
shivv23:fix/newsservice-json-parsing

Conversation

@shivv23

@shivv23 shivv23 commented Mar 23, 2026

Copy link
Copy Markdown

Description

Improve the toJSON() method in NewsService to handle malformed LLM outputs gracefully.

Changes:

  1. Remove debug print statements - Eliminated unnecessary logging statements
  2. Add try/except block - Catch IndexError and AttributeError for malformed data
  3. Handle empty/partial responses - Return empty list instead of crashing
  4. Validate data length - Skip items with less than 4 fields

Related Issues

Motivation and Context

LLM outputs can be unpredictable. The previous implementation would crash on malformed data or leave debug prints in production.

How Has This Been Tested

  • Syntax verification passed
  • All changes are defensive and non-breaking

Checklist

  • My code follows the code style of this project
  • I have read the CONTRIBUTING document

- Remove debug print statements
- Add try/except for IndexError and AttributeError
- Handle empty and partial responses gracefully
- Return empty list instead of empty dict for empty data

Closes c2siorg#118
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.

1 participant