Fix KeyError 'text' by checking if key exists before json.loads #77

Merged
luizslvr merged 1 commit from fix-keyerror-text into main 2025-11-26 02:10:13 -05:00
luizslvr commented 2025-11-25 19:52:31 -05:00 (Migrated from github.com)

Issue Description for Pull Request

Title: Fix KeyError: 'text' in client.py when parsing SSE messages

Description:

When using the search method in perplexity/client.py, a KeyError: 'text' occurs during the parsing of Server-Sent Events (SSE) responses. This happens because the code assumes that every content_json dictionary from the SSE stream contains a 'text' key, but some messages do not include this field.

Steps to Reproduce:

  1. Install perplexity-api version 1.0.5.
  2. Run a search query using Client.search() with valid cookies.
  3. Observe the KeyError: 'text' traceback in the parsing logic.

Root Cause:
In client.py, the code does:

content_json = json.loads(content[len('event: message\r\ndata: '):])
content_json['text'] = json.loads(content_json['text'])

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:

if 'text' in content_json:
    content_json['text'] = json.loads(content_json['text'])

This prevents the error and allows processing of messages that do not contain 'text'.

Impact:

  • Fixes the crash, making the library more robust.
  • No breaking changes; the fix is backward-compatible.
  • Tested with a sample query that previously failed.

Files Changed:

  • perplexity/client.py: Added checks in both stream_response and non-stream parsing sections.
### Issue Description for Pull Request **Title:** Fix KeyError: 'text' in client.py when parsing SSE messages **Description:** When using the `search` method in `perplexity/client.py`, a `KeyError: 'text'` occurs during the parsing of Server-Sent Events (SSE) responses. This happens because the code assumes that every `content_json` dictionary from the SSE stream contains a `'text'` key, but some messages do not include this field. **Steps to Reproduce:** 1. Install `perplexity-api` version 1.0.5. 2. Run a search query using `Client.search()` with valid cookies. 3. Observe the `KeyError: 'text'` traceback in the parsing logic. **Root Cause:** In `client.py`, the code does: ```python content_json = json.loads(content[len('event: message\r\ndata: '):]) content_json['text'] = json.loads(content_json['text']) ``` 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: ```python if 'text' in content_json: content_json['text'] = json.loads(content_json['text']) ``` This prevents the error and allows processing of messages that do not contain `'text'`. **Impact:** - Fixes the crash, making the library more robust. - No breaking changes; the fix is backward-compatible. - Tested with a sample query that previously failed. **Files Changed:** - `perplexity/client.py`: Added checks in both `stream_response` and non-stream parsing sections.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
helallao/perplexity-ai!77
No description provided.