Skip to content

parse_json_markdown is unable to parse json strings with nested triple backticks #5428

Closed as not planned
@schinto

Description

@schinto

System Info

Langchain version 0.0.184, python 3.9.13
Function parse_json_markdown in langchain/output_parsers/json.py fails with input text string:
```json
{
"action": "Final Answer",
"action_input": "Here's a Python script to remove backticks at the beginning and end of a string:\n\n```python\ndef remove_backticks(s):\n return s.strip('`')\n\nstring_with_backticks = '`example string`'\nresult = remove_backticks(string_with_backticks)\nprint(result)\n```\n\nThis script defines a function called `remove_backticks` that takes a string as input and returns a new string with backticks removed from the beginning and end. It then demonstrates how to use the function with an example string."
}
```

Potential case of error:
match.group(2) in the function parse_json_markdown contains only the string up to the first occurrence of the second triple backticks:

{
"action": "Final Answer",
"action_input": "Here's a Python script to remove backticks at the beginning and end of a string:\n\n

Who can help?

No response

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Called function parse_json_markdown in langchain/output_parsers/json.py with input text string:
```json
{
"action": "Final Answer",
"action_input": "Here's a Python script to remove backticks at the beginning and end of a string:\n\n```python\ndef remove_backticks(s):\n return s.strip('`')\n\nstring_with_backticks = '`example string`'\nresult = remove_backticks(string_with_backticks)\nprint(result)\n```\n\nThis script defines a function called `remove_backticks` that takes a string as input and returns a new string with backticks removed from the beginning and end. It then demonstrates how to use the function with an example string."
}
```

Expected behavior

Function parse_json_markdown should return the following json string
{
"action": "Final Answer",
"action_input": "Here's a Python script to remove backticks at the beginning and end of a string:\n\n```python\ndef remove_backticks(s):\n return s.strip('`')\n\nstring_with_backticks = '`example string`'\nresult = remove_backticks(string_with_backticks)\nprint(result)\n```\n\nThis script defines a function called `remove_backticks` that takes a string as input and returns a new string with backticks removed from the beginning and end. It then demonstrates how to use the function with an example string."
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions