langchain
langchain copied to clipboard
fixed json parsing for multiple generated JSON code segments, plus co…
Fixes showstopper issue with parsing LLM-generated code that returns one or multiple code segments in JSON format
Fixes # (issue)
Before submitting
Who can review?
Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:
Can someone approve this workflow at least? This is an incredibly important fix. @hwchase17
I also have many failed runs like #2679 .
My agent sometimes returns:
```\n{\n \"action\": \"Final Answer\",\n \"action_input\": \"The latest block count is 2950616.\"\n}\n```
Instead of:
\n{\n \"action\": \"Final Answer\",\n \"action_input\": \"The latest block count is 2950616.\"\n}\n
I guess the issue with the LLM-generated code is not fixed yet. Using langchain version 0.0.176 the following error occurs when code is returned in triple backticks:
2023-05-22 14:16:40.713 Uncaught app exception Traceback (most recent call last): File "/home/user1/scratch/conda/envs/lcpy39/lib/python3.9/site-packages/langchain/agents/conversational_chat/output_parser.py", line 21, in parse cleaned_output, _ = cleaned_output.split("```") ValueError: too many values to unpack (expected 2)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/user1/scratch/conda/envs/lcpy39/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.dict)
File "/home/user1/langchain-tools/chatbot_streamlit_chat.py", line 116, in
Sorry, I just saw that the fix is not in release 0.0.176 yet.
Can we please approve the workflow - or is there something else needed to proceed? (this is my first pull request, so happy to adjust as appropriate).
Resolved Black code formatting issue.
If additional testers are needed, please advise specifics (e.g., which one to modify or a good example to copy/modify)
thanks - lets adds some tests for things that this can handle so we don't have any regressions in the future
Happy to do so. Can you point me to existing tester you'd suggest that I update?
thanks - lets adds some tests for things that this can handle so we don't have any regressions in the future
Happy to do so. Can you point me to existing tester you'd suggest that I update?
don't think any unit tests exist for this class yet, would make a new file at tests/unit_tests/agents/conversational_chat/test_output_parser.py
and add there
I have submitted the fix again, this time addressing the "ruff" code format issues.
I have no idea where to start to develop the unit tester...
Figured it out. Will deliver unit tester soon...
Okay. Unit tester is now available.
How should we handle OPEN API keys?
_ ERROR collecting tests/unit_tests/agents/conversational_chat/test_output_parser.py _
tests/unit_tests/agents/conversational_chat/test_output_parser.py:11: in OPENAI_API_KEY
which contains it, or pass openai_api_key
as a named parameter. (type=value_error)
The unit tester has been rewritten in accordance with the recommendations. Please re-run the workflow.
Not sure why the latest commit is a conflict. The entire file was rewritten to replace the prior commit.
Do the tests cover cases with nested triple backticks ? As shown below:
langchain.schema.OutputParserException: Could not parse LLM output: ```json { "action": "Final Answer", "action_input": "Here is example code\n```python\nprint('Hello World')\n```\n" } ```
I wonder how the function parse_json_markdown
will work in this case?
json_string = json_string.replace("```json", "").replace("```", "")
removes all triple backticks, whereas the backticks in action_input should be preserved as they are part of the markdown.
Yes, it covers the case where code gets returned within the JSON string, starting with triple backticks and followed by type of code... as shown in the working unit tester.
Who should resolve the conflicts that have cropped up?
Yes, it covers the case where code gets returned within the JSON string, starting with triple backticks and followed by type of code... as shown in the working unit tester.
Sorry, I could not see this nested case in the unit tester.
It was this nested case, where '''python or '''javascript get returned when code is generated that caused the original issues with the output parser. This is what was fixed and why the unit tester was developed to test the code/no-code return cases.
I actually added logging to the output parser, captured actual returned code and non-code parser examples, then included those in the unit tester, to ensure they are correctly structured.
Please let me know if further refinements are needed to merge.
Thanks Rick
It was this nested case, where '''python or '''javascript get returned when code is generated that caused the original issues with the output parser. This is what was fixed and why the unit tester was developed to test the code/no-code return cases.
I just tested with langchain version 0.0.184 and the parsing of code blocks including triple backticks fails, see error below:
File "/home/user1/scratch/conda/envs/lcpy39/lib/python3.9/site-packages/langchain/agents/conversational_chat/output_parser.py", line 24, in parse raise OutputParserException(f"Could not parse LLM output: {text}") from e langchain.schema.OutputParserException: Could not parse LLM output: ```json { "action": "Final Answer", "action_input": "Here's a Python script to remove triple backticks at the start and end of a string, allowing spaces:\n\n```python\ndef remove_triple_backticks(s):\n return s.strip().lstrip('```').rstrip('```')\n\ninput_string = ' ```example text``` '\noutput_string = remove_triple_backticks(input_string)\nprint(output_string)\n```\n\nThis script defines a function `remove_triple_backticks` that takes a string as input, removes any leading and trailing spaces using `strip()`, and then removes triple backticks from the start and end of the string using `lstrip()` and `rstrip()`. The example input string is processed and the result is printed." } ```
Clearly this needs more rework...