Skip to content

[fix] Add error handling to get_top_hackernews_stories#7343

Open
lawrence3699 wants to merge 1 commit intoagno-agi:mainfrom
lawrence3699:fix/hackernews-error-handling
Open

[fix] Add error handling to get_top_hackernews_stories#7343
lawrence3699 wants to merge 1 commit intoagno-agi:mainfrom
lawrence3699:fix/hackernews-error-handling

Conversation

@lawrence3699
Copy link
Copy Markdown

@lawrence3699 lawrence3699 commented Apr 5, 2026

I noticed get_top_hackernews_stories has no error handling, while its sibling get_user_details in the same file wraps everything in try/except and returns a descriptive error string. When the HN Firebase API is unreachable or returns unexpected data, get_user_details lets the agent explain the problem to the user, but get_top_hackernews_stories just raises an unhandled exception.

There are two concrete failures:

  1. API errors go unhandled. If the Firebase API returns an error or times out, the method crashes instead of returning an error string. get_user_details handles this on line 75.

  2. Deleted stories crash the loop. The HN API returns null for deleted items (https://hacker-news.firebaseio.com/v0/item/0.jsonnull). When story_response.json() returns None, story["by"] raises TypeError: 'NoneType' object is not subscriptable. Job postings can also lack a by field, causing KeyError.

Reproduction:

>>> import httpx
>>> httpx.get("https://hacker-news.firebaseio.com/v0/item/0.json").json()
None
>>> None["by"]
TypeError: 'NoneType' object is not subscriptable

The fix adds a try/except matching get_user_details, skips null stories, and uses .get("by", "unknown") instead of direct key access.

Tests added:

  • test_get_top_stories_error_handling — API failure returns error string
  • test_get_top_stories_null_story_skipped — null items are skipped
  • test_get_top_stories_missing_by_field — missing by falls back to "unknown"

All 28 tests pass.

Fixes #7352

  • AI-assisted

get_user_details wraps its logic in try/except and returns a
descriptive error string when the HN API is unreachable, but
get_top_hackernews_stories has no error handling at all. This causes
two issues:

1. If the Firebase API returns an error or is unreachable, the method
   raises an unhandled exception instead of returning an error message.
2. story["by"] crashes with TypeError when the API returns null for
   deleted/dead items, and with KeyError if the "by" field is absent
   (e.g., job postings).

Add try/except matching the pattern in get_user_details, skip null
stories, and use .get("by", "unknown") for the username field.
@lawrence3699 lawrence3699 requested a review from a team as a code owner April 5, 2026 01:32
Copilot AI review requested due to automatic review settings April 5, 2026 01:32
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

PR Triage

Missing issue link: Please link the issue this PR addresses using fixes #<issue_number>, closes #<issue_number>, or resolves #<issue_number> in the PR description. If there is no existing issue, please create one first.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds robust error handling to get_top_hackernews_stories to prevent unhandled exceptions when the Hacker News Firebase API fails or returns unexpected/null story payloads.

Changes:

  • Wrap get_top_hackernews_stories in try/except and return a descriptive error string on failure.
  • Skip null (deleted) story payloads and safely derive username via story.get("by", "unknown").
  • Add unit tests covering API failures, null story skipping, and missing by fallback behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
libs/agno/agno/tools/hackernews.py Adds exception handling and null/missing-field resilience in top stories fetch.
libs/agno/tests/unit/tools/test_hackernews.py Adds targeted tests for error handling and edge cases introduced by HN API responses.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] get_top_hackernews_stories crashes on API errors and null/deleted stories

2 participants