Skip to content

Commit 8fc4844

Browse files
Update client_v2.py with handling JSON Decode error
## What This PR improves error handling in the `LLMWhispererClientV2.whisper_status` function. The update ensures empty or non-JSON API responses are handled gracefully: errors are now logged using `self.logger` and clear, actionable exceptions are raised, preventing uninformative or misleading `JSONDecodeError` stack traces. ## Why Previously, when the API failed and returned an empty or malformed response, the client attempted to parse it as JSON, resulting in cryptic errors and poor diagnostics for downstream consumers. This change makes error messages clear and actionable, improving maintainability and debuggability for both developers and users. ## How - Adds a defensive check for empty (`None` or blank) response bodies. - Wraps `json.loads(response.text)` in a try/except; logs errors and response content with `self.logger`. - Raises `LLMWhispererClientException` using the correct constructor signature (`value, status_code`). - All raised exceptions now include the original status code and a clear error message. ## Relevant Docs N/A ## Related Issues or PRs - Fixes: LW-158 ## Notes on Testing - Manually simulated API failures with empty and non-JSON responses to verify clear logging and correct exception behavior. - Ensured that all existing integration tests for client error handling continue to pass. ## Checklist I have read and understood the [Contribution Guidelines](). Signed-off-by: ali <[email protected]>
1 parent 764a0b6 commit 8fc4844

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/unstract/llmwhisperer/client_v2.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,15 @@ def whisper_status(self, whisper_hash: str) -> Any:
446446
s = requests.Session()
447447
response = s.send(prepared, timeout=self.api_timeout)
448448
if response.status_code != 200:
449-
err = json.loads(response.text)
450-
err["status_code"] = response.status_code
451-
raise LLMWhispererClientException(err)
449+
if not (response.text or "").strip():
450+
self.logger.error(f"Empty response body from API, status code: {response.status_code}")
451+
raise LLMWhispererClientException("Empty response body from API", response.status_code)
452+
try:
453+
err = json.loads(response.text)
454+
except json.JSONDecodeError as e:
455+
self.logger.error(f"JSON decode error: {e}; Response text: {response.text!r}")
456+
raise LLMWhispererClientException(f"Non-JSON response: {response.text}", response.status_code) from e
457+
raise LLMWhispererClientException(err, response.status_code)
452458
message = json.loads(response.text)
453459
message["status_code"] = response.status_code
454460
return message

0 commit comments

Comments
 (0)