From 5aa3876c6894be36e8619b66038762815540d905 Mon Sep 17 00:00:00 2001 From: Tami Takamiya Date: Tue, 29 Jul 2025 16:55:12 -0400 Subject: [PATCH] Replace json.loads with ast.literal_eval --- src/app/endpoints/streaming_query.py | 10 +++++----- tests/unit/app/endpoints/test_streaming_query.py | 9 ++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/app/endpoints/streaming_query.py b/src/app/endpoints/streaming_query.py index d322738d..eb94222d 100644 --- a/src/app/endpoints/streaming_query.py +++ b/src/app/endpoints/streaming_query.py @@ -1,9 +1,9 @@ """Handler for REST API call to provide answer to streaming query.""" +import ast import json -import logging import re -from json import JSONDecodeError +import logging from typing import Any, AsyncIterator, Iterator from cachetools import TTLCache # type: ignore @@ -362,12 +362,12 @@ def _handle_tool_execution_event( summary = summary[:newline_pos] for match in METADATA_PATTERN.findall(text_content_item.text): try: - meta = json.loads(match.replace("'", '"')) + meta = ast.literal_eval(match) if "document_id" in meta: metadata_map[meta["document_id"]] = meta - except JSONDecodeError: + except Exception: # pylint: disable=broad-except logger.debug( - "JSONDecodeError was thrown in processing %s", + "An exception was thrown in processing %s", match, ) diff --git a/tests/unit/app/endpoints/test_streaming_query.py b/tests/unit/app/endpoints/test_streaming_query.py index 87a3fba9..893d2bed 100644 --- a/tests/unit/app/endpoints/test_streaming_query.py +++ b/tests/unit/app/endpoints/test_streaming_query.py @@ -55,11 +55,13 @@ """, """Result 1 Content: ABC -Metadata: {'docs_url': 'https://example.com/doc1', 'title': 'Doc1', 'document_id': 'doc-1'} +Metadata: {'docs_url': 'https://example.com/doc1', 'title': 'Doc1', 'document_id': 'doc-1', \ +'source': None} """, """Result 2 Content: ABC -Metadata: {'docs_url': 'https://example.com/doc2', 'title': 'Doc2', 'document_id': 'doc-2'} +Metadata: {'docs_url': 'https://example.com/doc2', 'title': 'Doc2', 'document_id': 'doc-2', \ +'source': None} """, """END of knowledge_search tool results. """, @@ -67,7 +69,8 @@ # and it is not picked as a referenced document. """Result 3 Content: ABC -Metadata: {'docs_url': 'https://example.com/doc3', 'Title': 'Doc3', 'document_id': 'doc-3'} +Metadata: {'docs_url': 'https://example.com/doc3', 'Title': 'Doc3', 'document_id': 'doc-3', \ +'source': None} """, """The above results were retrieved to help answer the user\'s query: "Sample Query". Use them as supporting information only in answering this query.