Categories: ai

Langchain JsonOutputParser Invalid json output Error

Sometimes json output from llm like this : {“key”: “value”}“` that langchain JsonOutputParser regex cant parse. The regex validation function is in langchain_core.output_parsers.json.parse_json_markdown(). So we just monkey patch that function

# monkey_patch.py
from typing import Callable, Any
from langchain_core.output_parsers.json import _custom_parser, parse_partial_json
import langchain_core.output_parsers.json as lc_op_j
import re

def parse_json_markdown(
    json_string: str, *, parser: Callable[[str], Any] = parse_partial_json
) -> dict:
    """
    Parse a JSON string from a Markdown string.

    Args:
        json_string: The Markdown string.

    Returns:
        The parsed JSON object as a Python dictionary.
    """
    # Try to find JSON string within triple backticks
    match = re.search(r"(?:```)?(json)?(.*)", json_string, re.DOTALL) # --------> patch

    # If no match found, assume the entire string is a JSON string
    if match is None:
        json_str = json_string
    else:
        # If match found, use the content within the backticks
        json_str = match.group(2)

    # Strip whitespace and newlines from the start and end
    json_str = json_str.strip().strip("`")

    # handle newlines and other special characters inside the returned value
    json_str = _custom_parser(json_str)

    # Parse the JSON string into a Python dictionary
    parsed = parser(json_str)


    return parsed

lc_op_j.parse_json_markdown = parse_json_markdown

Here the test

import unittest
import monkey_patch as _

from langchain_core.output_parsers.json import JsonOutputParser

class TestMonkeyPatch(unittest.TestCase):
    def test_json_output_parse(self):
        results = map(lambda x: JsonOutputParser().parse(x), [
            '{"key": "value"',
            '```{"key": "value"}```',
            '```{"key": "value"}',
            '```{"key": "value"',
            '```{"key": "value"```',
            '```json{"key": "value"}```',
            '```json{"key": "value"}',
            '```json{"key": "value"',
            '```json{"key": "value"```',
            '```{"key": "value", "key2": "value2"```',
            '{"key": "value"}```', # ----> monkeypatch for langchain
        ])

        for result in results:
            self.assertEqual("value", result["key"])

Herbert Abdillah

Test

Share
Published by
Herbert Abdillah

Recent Posts

Disk 1 tb sisa 100 gb

Berikut ini detail breakdown nya Docker

2 weeks ago

Tontonan Bagus

https://www.youtube.com/watch?v=ZAqIoDhornk (more…)

2 months ago

Ada Apa Didalam Sistem Operasi (Linux). Apa yang membedakan distribusi Linux seperti Ubuntu, Redhat, Alpine?

#include <stdio.h> int main() { FILE *f_ptr; char f_content[256]; char output[512]; f_ptr = fopen("/tmp/1337", "r");…

5 months ago

Convert OpenSSL 1 in Ruby Gem to OpenSSL 3

Version 3 have different interface Example: https://github.com/herbertabdillah/fabric-gateway-ruby/commit/c7377aaf2e62de1e2ac309965a09b5c7c72a2c7e (more…)

2 years ago

Catatan Belajar Ruby on Rails dari aliran Java dan Php

Telah di edit. Sumber Asli : https://twitter.com/nateberkopec/status/1250603032523370496/photo/1 Ruby on Rails merupakan framework web MVC menggunakan…

2 years ago

Deploy Spring Boot di Tomcat dan Berbagai Application Server Lain

Sebenernya spring boot sudah di embedd applicatoin server tomcat (hanya berisi web container, tidak bisa…

2 years ago