fix: handle unhandled AttributeError on string messages#47
Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughDefensive checks in _extract_last_user_message ensure ChangesMalformed Payload Defense
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@humane_proxy/middleware/interceptor.py`:
- Around line 72-73: The extractor currently returns msg.get("content", "")
which may be non-str and can bubble into pipeline.classify; modify the block in
interceptor.py so that after confirming isinstance(msg, dict) and
msg.get("role") == "user" you explicitly fetch content = msg.get("content", "")
and then return it only if isinstance(content, str) else return "" (i.e.,
enforce/validate string type rather than returning arbitrary types) to close
malformed-payload crash paths.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5dc7c463-0f77-41ff-b272-6bb08764337b
📒 Files selected for processing (1)
humane_proxy/middleware/interceptor.py
|
Hi @Vishisht16 |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
tests/test_interceptor.py (3)
110-116: ⚡ Quick winConsider asserting the specific error message for robustness.
The test correctly verifies the 400 response and error status. However, for consistency with the enhanced assertion on line 108 and to ensure the correct code path is triggered, consider also asserting the specific error message:
assert resp.json()["message"] == "No user message found in payload."This makes the test more robust and explicit about the expected behavior.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/test_interceptor.py` around lines 110 - 116, In test_messages_is_not_a_list, after asserting resp.status_code and resp.json()["status"], add an assertion that resp.json()["message"] == "No user message found in payload." to ensure the specific error message is returned when the "messages" field is not a list; update the test function name test_messages_is_not_a_list and use the resp JSON payload to compare the "message" key for exact matching.
118-124: ⚡ Quick winConsider asserting the specific error message for robustness.
Similar to the previous test, consider adding an assertion for the specific error message to ensure the correct code path:
assert resp.json()["message"] == "No user message found in payload."🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/test_interceptor.py` around lines 118 - 124, In test_message_item_is_not_a_dict, strengthen the assertion by checking the specific error message returned in resp: after asserting resp.status_code and resp.json()["status"], add an assertion on resp.json()["message"] to equal "No user message found in payload." so the test verifies the exact error path for the malformed messages payload.
126-132: ⚡ Quick winConsider asserting the specific error message for robustness.
For consistency with the pattern established on line 108, add an assertion for the specific error message:
assert resp.json()["message"] == "No user message found in payload."This ensures the test verifies the complete expected behavior.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/test_interceptor.py` around lines 126 - 132, Update the test_message_content_is_not_a_string test to assert the specific error message returned by the endpoint: after asserting resp.status_code == 400 and resp.json()["status"] == "error", add an assertion that resp.json()["message"] == "No user message found in payload." so the test mirrors the pattern used in the other tests (e.g., the assertion on line ~108) and verifies the full error response.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/test_interceptor.py`:
- Around line 110-116: In test_messages_is_not_a_list, after asserting
resp.status_code and resp.json()["status"], add an assertion that
resp.json()["message"] == "No user message found in payload." to ensure the
specific error message is returned when the "messages" field is not a list;
update the test function name test_messages_is_not_a_list and use the resp JSON
payload to compare the "message" key for exact matching.
- Around line 118-124: In test_message_item_is_not_a_dict, strengthen the
assertion by checking the specific error message returned in resp: after
asserting resp.status_code and resp.json()["status"], add an assertion on
resp.json()["message"] to equal "No user message found in payload." so the test
verifies the exact error path for the malformed messages payload.
- Around line 126-132: Update the test_message_content_is_not_a_string test to
assert the specific error message returned by the endpoint: after asserting
resp.status_code == 400 and resp.json()["status"] == "error", add an assertion
that resp.json()["message"] == "No user message found in payload." so the test
mirrors the pattern used in the other tests (e.g., the assertion on line ~108)
and verifies the full error response.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1be5d3e7-c13f-4f34-86a7-0e4d523cbab3
📒 Files selected for processing (2)
humane_proxy/middleware/interceptor.pytests/test_interceptor.py
🚧 Files skipped from review as they are similar to previous changes (1)
- humane_proxy/middleware/interceptor.py
Fixes #44.
This PR adds type validation in
_extract_last_user_messageto ensure thatmessagesis a list and each message is a dictionary before attempting to extract the role and content. This prevents an unhandledAttributeErrorand the resulting 500 Internal Server Error when a client sends a malformed payload (e.g.,{"messages": "not a list"}).