[Bounty proposal] fix(apple-health-tools): guard string numeric formatting, handle None health_data, and sanitize lists ($50 proposed)
Summary
In backend/utils/retrieval/tools/apple_health_tools.py, health data synced from iOS devices or Firestore documents exhibits multiple input-type and formatting failure modes:
health_data: Nonecrashes:integration.get('health_data', {})returnsNoneif the Firestore document explicitly contains"health_data": None(or corrupted non-dict payload). Subsequently,health_data.get('steps', {})raises an unhandledAttributeError: 'NoneType' object has no attribute 'get'. This causes tools to catch generic errors and report"Failed to retrieve step data."rather than the informative"No step data found. Make sure Apple Health is synced from your iPhone.".- String numeric formatting errors (
TypeError/ValueError):total_steps:,andavg_per_day:,.0fcrash withValueError: Cannot specify ',' with 's'if values are stored as string numbers. If values areNone, they crash withTypeError: unsupported format string passed to NoneType.__format__.total_sleep_hours:.1fcrashes withValueError: Unknown format code 'f' for object of type 'str'if stored as a string, andtotal_sleep_hours / max(...)raisesTypeError: unsupported operand type(s) for /: 'str' and 'int'.- Heart rate values (
avg_hr:.0f,min_hr:.0f,max_hr:.0f) and workout metrics (duration:.0f,calories:.0f,distance:.2f) crash on string formats. - Workout timestamps:
start_ms / 1000raisesTypeErrorwhenstartDateis a string timestamp.
- Malformed / non-dict list entries:
dailysteps,dailysleep, andworkoutscontainingNoneor non-dict items cause sorting keys (lambda x: x.get('date', ''),_workout_timestamp) to crash withAttributeError. get_apple_health_datahelper: Does not guard againsthealth_databeingNoneor non-dict before indexingdata_type.
Proposed Changes
- Safe Numeric Coercion (
_safe_float,_safe_int): Add helpers to safely coerce numeric values (ints, floats, and numeric strings) while gracefully rejecting booleans, non-numeric strings,NaN,inf, andNone. - Robust
health_dataExtraction: Ensurehealth_datais guaranteed to be a dictionary across all tool endpoints andget_apple_health_data. - List Sanitization:
Filter
dailyentries andworkoutsto dictionary items only before sorting or accessing keys. - Safe Formatting:
Ensure all f-string numeric formatters (
:,,:.1f,:.0f,:.2f) use coerced numeric types with safe fallbacks. - Hermetic Unit Tests:
Add
backend/tests/unit/test_apple_health_coercion_and_guards.pyverifying:_safe_floatand_safe_intwith ints, floats, strings, negative values, booleans, NaNs, and text.get_apple_health_datawith None health data.- String
totaland daily steps formatting. - String
total_sleep_hoursand nightly average calculation. - String heart rate metrics formatting.
- String workout metrics and timestamp parsing.
- Comprehensive summary formatting with mixed types and malformed list items.
Bounty Proposal
Proposed bounty: $50 USDC
Payout address: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC Polygon/Base/Arbitrum)
PR created with comprehensive hermetic unit tests: https://github.com/BasedHardware/omi/pull/23212
Bounty proposal: $50 USDC
Payout address: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC Polygon/Base/Arbitrum)
Thanks for putting this together. We aren't doing bounties at the moment, so we're closing the bounty proposal issues for now — thanks for the fix! 🙏