Parse output as a dictionary object#

When prompting an LLM for structured (e.g. return JSON), you might get free text in addition to the requested content, or partially structured content. To convert the LLM output to a structured format, you can either relay on the LLM provider to do the parsing for you, or parse it inside your codebase.

The json_data_parser function, will try to extract dictitonary or array content from an LLM response and raise an exception when it fails.

[4]:
from llmabstractions import json_data_parser

llm_response = '''
Here is the response
```json
{
     "key1": "value1",
     "key2": "value2"
 }
```

Is there something else?
'''
parsed_response = json_data_parser(llm_response)
parsed_response
[4]:
{'key1': 'value1', 'key2': 'value2'}

In other cases, the content might be embeded in the text response. E.g.

[5]:
llm_response = '''
Here is your array

[
    {
        "key1": "value1"
    },
    {
        "key2": "value2"
    }
]

Is there something else?
'''
parsed_response = json_data_parser(llm_response)
parsed_response
[5]:
[{'key1': 'value1'}, {'key2': 'value2'}]

The json_data_parser function, will raise a ParsingError when it fails to extract JSON content from the text.

[6]:
llm_response = '''
Here is your array

There is nothing to extract here
'''
parsed_response = json_data_parser(llm_response)
parsed_response
---------------------------------------------------------------------------
ParsingError                              Traceback (most recent call last)
Cell In[6], line 6
      1 llm_response = '''
      2 Here is your array
      3
      4 There is nothing to extract here
      5 '''
----> 6 parsed_response = json_data_parser(llm_response)
      7 parsed_response

File ~\dialectos-ai\repos\llmabstractions\llmabstractions\parsers\json_response.py:71, in json_data_parser(llm_output)
     68             parsed_response = _extract_dict_or_array_from_text(llm_output)
     70 if parsed_response is None:
---> 71     raise ParsingError('Unable to parse LLM output.')
     73 return parsed_response

ParsingError: Unable to parse LLM output.