From 2cca774de5a6f7f413d87a8d8ee76c780e089394 Mon Sep 17 00:00:00 2001 From: James Braza Date: Thu, 7 Mar 2024 21:27:44 -0800 Subject: [PATCH] Fixing crash if json.loads returns a string --- paperqa/docs.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/paperqa/docs.py b/paperqa/docs.py index 3f1264a99..a643c3371 100644 --- a/paperqa/docs.py +++ b/paperqa/docs.py @@ -705,20 +705,17 @@ async def process(match): # fetch from markdown ```json if present context = context.split("```json")[-1].split("```")[0] result_data = json.loads(context) + success = isinstance(result_data, dict) except json.decoder.JSONDecodeError: # fallback to string success = False if success: try: - context = result_data["summary"] - score = result_data["relevance_score"] - del result_data["summary"] - del result_data["relevance_score"] - if "question" in result_data: - del result_data["question"] + context = result_data.pop("summary") + score = result_data.pop("relevance_score") + result_data.pop("question", None) extras = result_data except KeyError: - # fallback success = False # fallback to string (or json mode not enabled) if not success or not self.prompts.summary_json: