Skip to content

[codex] harden today top news AI response parsing - #8

Draft
datehoer wants to merge 1 commit into
mainfrom
codex/fix-today-top-news-ai-response
Draft

[codex] harden today top news AI response parsing#8
datehoer wants to merge 1 commit into
mainfrom
codex/fix-today-top-news-ai-response

Conversation

@datehoer

Copy link
Copy Markdown
Owner

Summary

  • harden /todayTopNews against empty or malformed AI selection payloads by validating the top-level hot_topics response before detail parsing
  • return a clear error message like AI response is empty instead of cascading into attribute errors when the model returns bad output
  • add a regression test covering the empty-response case

Why

The endpoint previously assumed the model always returned a JSON object with a hot_topics list. When the model returned an empty string, the code fell through to .get("hot_topics", []) on a non-dict value and produced an unhelpful failure path.

Validation

  • python3 -m py_compile app.py tests/test_today_top_news.py
  • . .venv/bin/activate && python -m unittest tests/test_today_top_news.py

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new helper function parse_hot_topics_response in app.py to parse and validate the AI response for hot topics, and integrates it into getTodayTopNews. It also adds a unit test suite in tests/test_today_top_news.py to verify the error handling when the AI response is empty. The reviewer suggests improving the robustness of parse_hot_topics_response by gracefully handling JSON decoding errors and validating the presence of the hot_label key in each topic to prevent downstream KeyError exceptions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread app.py
Comment on lines +63 to +79
def parse_hot_topics_response(text):
if not text or not text.strip():
raise ValueError("AI response is empty")

parsed = json.loads(repair_json(text))
if not isinstance(parsed, dict):
raise ValueError("AI response is invalid: expected object with hot_topics")

topics = parsed.get("hot_topics")
if not isinstance(topics, list):
raise ValueError("AI response is invalid: hot_topics must be a list")

for index, topic in enumerate(topics):
if not isinstance(topic, dict):
raise ValueError(f"AI response is invalid: hot_topics[{index}] must be an object")

return topics

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To further harden the AI response parsing and prevent unhandled exceptions:

  1. Handle JSON decoding errors gracefully: If repair_json(text) produces invalid JSON, json.loads will raise a json.JSONDecodeError. Wrapping this in a try-except block and raising a clear ValueError keeps the error messages consistent and informative.
  2. Prevent potential KeyError: In getTodayTopNews(), needKnow['hot_label'] is accessed directly (line 408). If the AI response contains a topic object that is missing the hot_label key, this will raise a KeyError. Validating that hot_label is present in each topic dictionary prevents this cascading failure.
def parse_hot_topics_response(text):
    if not text or not text.strip():
        raise ValueError("AI response is empty")

    try:
        parsed = json.loads(repair_json(text))
    except json.JSONDecodeError as e:
        raise ValueError(f"AI response is not valid JSON: {e}")

    if not isinstance(parsed, dict):
        raise ValueError("AI response is invalid: expected object with hot_topics")

    topics = parsed.get("hot_topics")
    if not isinstance(topics, list):
        raise ValueError("AI response is invalid: hot_topics must be a list")

    for index, topic in enumerate(topics):
        if not isinstance(topic, dict):
            raise ValueError(f"AI response is invalid: hot_topics[{index}] must be an object")
        if "hot_label" not in topic:
            raise ValueError(f"AI response is invalid: hot_topics[{index}] is missing 'hot_label'")

    return topics

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