Skip to content

Commit 905792d

Browse files
authored
Merge pull request #314 from lightspeed-core/TamiTakamiya/replace-json-loads-with-ast-literal_eval
Replace json.loads with ast.literal_eval
2 parents 4baac29 + 5aa3876 commit 905792d

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/app/endpoints/streaming_query.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Handler for REST API call to provide answer to streaming query."""
22

3+
import ast
34
import json
4-
import logging
55
import re
6-
from json import JSONDecodeError
6+
import logging
77
from typing import Any, AsyncIterator, Iterator
88

99
from cachetools import TTLCache # type: ignore
@@ -362,12 +362,12 @@ def _handle_tool_execution_event(
362362
summary = summary[:newline_pos]
363363
for match in METADATA_PATTERN.findall(text_content_item.text):
364364
try:
365-
meta = json.loads(match.replace("'", '"'))
365+
meta = ast.literal_eval(match)
366366
if "document_id" in meta:
367367
metadata_map[meta["document_id"]] = meta
368-
except JSONDecodeError:
368+
except Exception: # pylint: disable=broad-except
369369
logger.debug(
370-
"JSONDecodeError was thrown in processing %s",
370+
"An exception was thrown in processing %s",
371371
match,
372372
)
373373

tests/unit/app/endpoints/test_streaming_query.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,22 @@
5555
""",
5656
"""Result 1
5757
Content: ABC
58-
Metadata: {'docs_url': 'https://example.com/doc1', 'title': 'Doc1', 'document_id': 'doc-1'}
58+
Metadata: {'docs_url': 'https://example.com/doc1', 'title': 'Doc1', 'document_id': 'doc-1', \
59+
'source': None}
5960
""",
6061
"""Result 2
6162
Content: ABC
62-
Metadata: {'docs_url': 'https://example.com/doc2', 'title': 'Doc2', 'document_id': 'doc-2'}
63+
Metadata: {'docs_url': 'https://example.com/doc2', 'title': 'Doc2', 'document_id': 'doc-2', \
64+
'source': None}
6365
""",
6466
"""END of knowledge_search tool results.
6567
""",
6668
# Following metadata contains an intentionally incorrect keyword "Title" (instead of "title")
6769
# and it is not picked as a referenced document.
6870
"""Result 3
6971
Content: ABC
70-
Metadata: {'docs_url': 'https://example.com/doc3', 'Title': 'Doc3', 'document_id': 'doc-3'}
72+
Metadata: {'docs_url': 'https://example.com/doc3', 'Title': 'Doc3', 'document_id': 'doc-3', \
73+
'source': None}
7174
""",
7275
"""The above results were retrieved to help answer the user\'s query: "Sample Query".
7376
Use them as supporting information only in answering this query.

0 commit comments

Comments
 (0)