omi
omi copied to clipboard
[Bounty proposal] fix(perplexity-tools): validate non-empty query and guard hop-2 retry concatenation ($50 proposed)
Summary
In ackend/utils/retrieval/tools/perplexity_tools.py, invoking perplexity_web_search_tool with an empty, whitespace-only, or None query causes:
- _post_gateway_chat_completion(query) to transmit {"messages": [{"role": "user", "content": None}]}, triggering HTTP 400 Bad Request on the LLM gateway.
- In hop-2 retry logic: query + WEB_SEARCH_RETRIEVAL_APPENDIX triggers an unhandled TypeError: can only concatenate str (not "NoneType") to str.
- In _format_perplexity_response and _extract_citations: content = result['choices'][0]['message']['content'] can yield string literal "None" or raise IndexError when choices is an empty list ( esult.get('choices', [{}])[0]), and malformed/non-list citations or missing title/URL attributes fail to render cleanly.
Proposed Changes
- Query Validation: Validate clean_query = str(query or '').strip() early in perplexity_web_search_tool and _perplexity_gateway_search. If empty, return a graceful error message (Error: Search query cannot be empty.).
- Safe Hop-2 Concatenation: Ensure clean_query is used in hop-2 retry (clean_query + WEB_SEARCH_RETRIEVAL_APPENDIX).
- Safe Response & Citation Extraction:
- Safely extract choices and message content without assuming non-empty lists.
- Convert None content to empty string instead of "None".
- Safely parse dictionary or string citations with proper strip and fallback defaults.
- Hermetic Unit Tests: Add ackend/tests/unit/test_perplexity_tools.py verifying empty query rejection, successful search formatting, hop-2 primary-source retry on thin miss, and gateway error handling.
Bounty: proposed.
PR created with hermetic unit tests: https://github.com/BasedHardware/omi/pull/23123
Fix submitted via #23126 with comprehensive unit tests (5/5 passing).
- Early validation for clean_query preventing HTTP 400 on gateway.
- Safe hop-2 retry concatenation.
- Hardened choice and citation extraction handling
Nonecontent gracefully.
Payout address (Base USDC): 0xb95e2AD8E9D9e9BF902c7531577Cd0E1ceB203D4
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! 🙏