Output Parsers#
- llmabstractions.parsers.json_response.json_data_parser(llm_output: str | dict | list) dict#
Extracts dictionary or array content from an LLM response.
There are cases where you ask from the LLMs to return an array or a dictionary, but you don’t get a structured output. You can either relay on models supporting structured output, or parse the output yourself using this function.
Parameters#
- llm_output: str, dict, list
Raw LLM response. Expected as string. However, if a dictionary or list is passed, the same content will be returned as long as they can be JSON serialized.
Returns#
- dict or list:
A dictionary or list content in the message.
Raises#
- ParsingError:
If for any reason, the content cannot be extracted from the response.
Examples#
Extract a dictionary embedded in text:
>>> llm_response = ''' ... Here is the response ... ... { ... "key1": "value1", ... "key2": "value2" ... } ... ... Is there something else? ... ''' >>> json_data_parser(llm_response) {'key1': 'value1', 'key2': 'value2'}