omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(perplexity-tools): validate non-empty query and guard hop-2 retry concatenation ($50 proposed)

Open Dimank92 opened this issue 1 month ago • 3 comments

Summary

In ackend/utils/retrieval/tools/perplexity_tools.py, invoking perplexity_web_search_tool with an empty, whitespace-only, or None query causes:

  1. _post_gateway_chat_completion(query) to transmit {"messages": [{"role": "user", "content": None}]}, triggering HTTP 400 Bad Request on the LLM gateway.
  2. In hop-2 retry logic: query + WEB_SEARCH_RETRIEVAL_APPENDIX triggers an unhandled TypeError: can only concatenate str (not "NoneType") to str.
  3. 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

  1. 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.).
  2. Safe Hop-2 Concatenation: Ensure clean_query is used in hop-2 retry (clean_query + WEB_SEARCH_RETRIEVAL_APPENDIX).
  3. 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.
  4. 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.

Dimank92 avatar Oct 21 '26 20:10 Dimank92

PR created with hermetic unit tests: https://github.com/BasedHardware/omi/pull/23123

Dimank92 avatar Oct 21 '26 20:10 Dimank92

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 None content gracefully.

Payout address (Base USDC): 0xb95e2AD8E9D9e9BF902c7531577Cd0E1ceB203D4

Booshdag-io avatar Oct 21 '26 20:10 Booshdag-io

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! 🙏

undivisible avatar Oct 22 '26 12:10 undivisible