fix: handle dict and list formats in overseas balance output2 (issue #41) #45

Merged
jihoson merged 1 commits from feature/issue-41-keyerror-balance into main 2026-02-05 00:06:26 +09:00
Collaborator

Summary

  • Fix KeyError when accessing output2[0] in overseas balance parsing
  • Handle both list [{}] and dict {} response formats from KIS API
  • Add safe fallback for empty/None values

Problem

Docker logs showed repeated KeyError crashes (15% of requests):

KeyError: 0 at src/main.py:98
total_eval = float(output2[0].get(...))

API response format varies between list and dict, causing index access failure.

Solution

# Before
output2 = balance_data.get("output2", [{}])
total_eval = float(output2[0].get("frcr_evlu_tota", "0"))

# After
output2 = balance_data.get("output2", [{}])
if isinstance(output2, list) and output2:
    balance_info = output2[0]
elif isinstance(output2, dict):
    balance_info = output2
else:
    balance_info = {}
total_eval = float(balance_info.get("frcr_evlu_tota", "0") or "0")

Test Coverage

  • Test list format: output2=[{...}]
  • Test dict format: output2={...}
  • Test empty format: output2=[]
  • All existing tests pass (276 total)
  • Coverage maintained at 80%

Closes

Closes #41

🤖 Generated with Claude Code

## Summary - Fix KeyError when accessing `output2[0]` in overseas balance parsing - Handle both list `[{}]` and dict `{}` response formats from KIS API - Add safe fallback for empty/None values ## Problem Docker logs showed repeated KeyError crashes (15% of requests): ``` KeyError: 0 at src/main.py:98 total_eval = float(output2[0].get(...)) ``` API response format varies between list and dict, causing index access failure. ## Solution ```python # Before output2 = balance_data.get("output2", [{}]) total_eval = float(output2[0].get("frcr_evlu_tota", "0")) # After output2 = balance_data.get("output2", [{}]) if isinstance(output2, list) and output2: balance_info = output2[0] elif isinstance(output2, dict): balance_info = output2 else: balance_info = {} total_eval = float(balance_info.get("frcr_evlu_tota", "0") or "0") ``` ## Test Coverage - ✅ Test list format: `output2=[{...}]` - ✅ Test dict format: `output2={...}` - ✅ Test empty format: `output2=[]` - ✅ All existing tests pass (276 total) - ✅ Coverage maintained at 80% ## Closes Closes #41 🤖 Generated with [Claude Code](https://claude.com/claude-code)
agentson added 1 commit 2026-02-05 00:05:06 +09:00
fix: handle dict and list formats in overseas balance output2 (issue #41)
Some checks failed
CI / test (pull_request) Has been cancelled
3dfd7c0935
Add type checking for output2 response from get_overseas_balance API.
The API can return either list format [{}] or dict format {}, causing
KeyError when accessing output2[0].

Changes:
- Check isinstance before accessing output2[0]
- Handle list, dict, and empty cases
- Add safe fallback with "or" for empty strings
- Add 3 test cases for list/dict/empty formats

Fixes: #41

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
jihoson merged commit 0087a6b20a into main 2026-02-05 00:06:26 +09:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#45