Repository navigation
fix(tools): harden perplexity and web search against empty queries, IndexError on empty choices, and None messages - #23530
Conversation
…rror on empty choices, and None messages
|
The hardening here is correct and well-scoped — nice first contribution; one small non-blocking gap noted at the end. Per-file findings (all verified on head d46b8c8):
Cost: empty queries no longer burn a gateway LLM call (small positive). Security: untrusted web content still wrapped via USER IMPACT: none — internal hardening; users only see fewer failed/empty web-search answers when the search backend returns a malformed response. Still open (non-blocking): in by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
…does not render literal None
|
Thanks @Git-on-my-level for the thorough review and approval! Addressed the non-blocking edge case in |
Summary
This PR hardens
perplexity_tools.pyandweb_tools.pyinbackend/utils/retrieval/tools/against unhandled runtime crashes:perplexity_tools.py:_extract_citationsIndexError defense: When a model gateway response contains'choices': [],result.get('choices', [{}])[0]previously raisedIndexError: list index out of range. Added defensive length andisinstancechecks to return an empty citations list cleanly.AttributeErroron None message: Whenchoices[0]['message']isNoneor malformed,.get('message', {}).get('citations')raisedAttributeError: 'NoneType' object has no attribute 'get'.perplexity_web_search_toolagainst empty, None, and whitespace-only queries to prevent wasted LLM gateway compute and downstream string concatenationTypeErrorin hop-2 retry logic (query + WEB_SEARCH_RETRIEVAL_APPENDIX)._model_text&_format_perplexity_response: Defensive unpacking of choices list and message dictionaries.web_tools.py:fetch_url_tool: Added input validation againsturl=None, non-string, or empty/whitespace URLs, and trims leading/trailing whitespace before protocol verification (startswith(('http://', 'https://'))).Hermetic Test Suite:
backend/tests/unit/test_search_and_web_tools_resilience.pywith 5 focused unit tests covering empty/None queries, malformed choices, nested citations, and URL whitespace trimming.Verification