-
Notifications
You must be signed in to change notification settings - Fork 21
[codex] harden today top news AI response parsing #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
datehoer
wants to merge
1
commit into
main
Choose a base branch
from
codex/fix-today-top-news-ai-response
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import unittest | ||
| import sys | ||
| import types | ||
| from unittest.mock import patch | ||
|
|
||
| config = types.ModuleType("config") | ||
| config.REDIS_PASSWORD = "" | ||
| config.REDIS_HOST = "localhost" | ||
| config.REDIS_PORT = 6379 | ||
| config.REDIS_DB = 0 | ||
| config.PG_HOST = "localhost" | ||
| config.PG_PORT = 5432 | ||
| config.PG_USER = "user" | ||
| config.PG_PASSWORD = "password" | ||
| config.PG_DB = "db" | ||
| config.api_url = "https://example.com" | ||
| config.api_headers = {} | ||
| config.news_sites = ["澎湃新闻"] | ||
| config.EMAIL = { | ||
| "user": "test@example.com", | ||
| "host": "smtp.example.com", | ||
| "port": 587, | ||
| "password": "password", | ||
| } | ||
| sys.modules["config"] = config | ||
|
|
||
| import app | ||
|
|
||
|
|
||
| class FakeRedis: | ||
| async def get(self, key): | ||
| return None | ||
|
|
||
| async def set(self, *args, **kwargs): | ||
| return True | ||
|
|
||
| async def delete(self, *args, **kwargs): | ||
| return 1 | ||
|
|
||
| async def setex(self, *args, **kwargs): | ||
| return True | ||
|
|
||
|
|
||
| class TodayTopNewsTest(unittest.IsolatedAsyncioTestCase): | ||
| async def test_empty_model_response_returns_clear_error(self): | ||
| async def fake_get_data(item_id): | ||
| return { | ||
| "code": 200, | ||
| "msg": "success", | ||
| "data": [ | ||
| { | ||
| "name": "澎湃新闻", | ||
| "data": [ | ||
| { | ||
| "hot_label": "A", | ||
| "hot_url": "https://example.com", | ||
| "hot_value": "1", | ||
| } | ||
| ], | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| async def fake_chat_with_model(messages, response_format): | ||
| return "" | ||
|
|
||
| with patch.object(app, "redis_client", FakeRedis()), patch.object( | ||
| app, "get_data", fake_get_data | ||
| ), patch.object(app, "chatWithModel", fake_chat_with_model), patch.object( | ||
| app.logging, "error" | ||
| ): | ||
| result = await app.getTodayTopNews() | ||
|
|
||
| self.assertEqual(result["code"], 500) | ||
| self.assertIn("AI response is empty", result["msg"]) | ||
| self.assertNotIn("object has no attribute", result["msg"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To further harden the AI response parsing and prevent unhandled exceptions:
repair_json(text)produces invalid JSON,json.loadswill raise ajson.JSONDecodeError. Wrapping this in atry-exceptblock and raising a clearValueErrorkeeps the error messages consistent and informative.KeyError: IngetTodayTopNews(),needKnow['hot_label']is accessed directly (line 408). If the AI response contains a topic object that is missing thehot_labelkey, this will raise aKeyError. Validating thathot_labelis present in each topic dictionary prevents this cascading failure.