Skip to content

Commit

Permalink
Add log message on unparsable JSON (#28)
Browse files Browse the repository at this point in the history
* add json parse failures to log

* log json parsing errors
  • Loading branch information
darthtrevino authored Apr 3, 2024
1 parent fde2044 commit 77f9dba
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion python/graphrag/graphrag/llm/openai/json_parsing_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""An LLM that unpacks cached JSON responses."""

import json
import logging
from typing import cast

from typing_extensions import Unpack
Expand All @@ -16,6 +17,10 @@
LLMOutput,
)

log = logging.getLogger(__name__)

UNPARSABLE_JSON = "Failed to parse JSON data"


class JsonParsingLLM(LLM[CompletionInput, CompletionOutput]):
"""An OpenAI History-Tracking LLM."""
Expand All @@ -33,5 +38,9 @@ async def __call__(
"""Call the LLM with the input and kwargs."""
result = await self._delegate(input, **kwargs)
if kwargs.get("json") and result.json is None and result.output is not None:
result.json = cast(dict, json.loads(result.output))
try:
result.json = cast(dict, json.loads(result.output))
except json.JSONDecodeError as e:
log.error("Failed to parse JSON output:\n%s", result.output) # noqa TRY400
raise ValueError(UNPARSABLE_JSON) from e
return result

0 comments on commit 77f9dba

Please sign in to comment.