Fix KeyError 'text' by checking if key exists before json.loads #77
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix-keyerror-text"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Issue Description for Pull Request
Title: Fix KeyError: 'text' in client.py when parsing SSE messages
Description:
When using the
searchmethod inperplexity/client.py, aKeyError: 'text'occurs during the parsing of Server-Sent Events (SSE) responses. This happens because the code assumes that everycontent_jsondictionary from the SSE stream contains a'text'key, but some messages do not include this field.Steps to Reproduce:
perplexity-apiversion 1.0.5.Client.search()with valid cookies.KeyError: 'text'traceback in the parsing logic.Root Cause:
In
client.py, the code does:This directly accesses
content_json['text']without checking if the key exists, leading to a KeyError when the response message lacks the'text'field.Proposed Fix:
Add a conditional check before attempting to parse the
'text'field:This prevents the error and allows processing of messages that do not contain
'text'.Impact:
Files Changed:
perplexity/client.py: Added checks in bothstream_responseand non-stream parsing sections.