Handle invalid XiaoAI conversation data - #2891
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves error handling in fetch_latest_message within sensor.py by returning an empty dictionary early when the response data is None or not a dictionary, and adds a corresponding unit test. The reviewer suggested handling potential JSON decoding errors explicitly to prevent falling into the outer exception block, which would otherwise clear the last valid conversation state.
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.
| if isinstance(rdt, (str, bytes, bytearray)): | ||
| rdt = json.loads(rdt) or {} |
There was a problem hiding this comment.
If json.loads(rdt) raises a JSONDecodeError (which is a subclass of ValueError), the execution falls into the outer except block. Currently, that block sets rdt = {} and continues execution, which clears the conversation and its attributes (setting them to empty/None).
To fully satisfy the PR's goal of preserving the last valid conversation on unexpected payloads or failures, we should handle JSON parsing errors gracefully by returning {} immediately. Additionally, you should consider updating the outer except block to also return {} instead of setting rdt = {} so that connection or API errors do not clear the last valid conversation.
| if isinstance(rdt, (str, bytes, bytearray)): | |
| rdt = json.loads(rdt) or {} | |
| if isinstance(rdt, (str, bytes, bytearray)): | |
| try: | |
| rdt = json.loads(rdt) or {} | |
| except (TypeError, ValueError): | |
| return {} |
Summary
datapayloads as no update.Problem
The Mina conversation endpoint may intermittently return
data: null.The sensor currently attempts to call
json.loads(None), logs aTypeErrorfor the affected poll, and replaces the current conversationattributes with empty values.
Tests
python -m pytest -q tests/test_xiaoai_conversation_sensor.pyFixes #2802