Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
bborn committed Aug 3, 2023
1 parent b416eb7 commit bb766f1
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions libs/langchain/langchain/output_parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
from langchain.schema import BaseOutputParser, OutputParserException


def replace_new_line(matched_string):
if matched_string:
value = matched_string.group(2)
value = re.sub(r"\n", r"\\n", value)
value = re.sub(r"\r", r"\\r", value)
value = re.sub(r"\t", r"\\t", value)
value = re.sub('"', r"\"", value)

return matched_string.group(1) + value + matched_string.group(3)
else:
return matched_string
def replace_new_line(match: re.Match[str]) -> str:
value = match.group(2)
value = re.sub(r"\n", r"\\n", value)
value = re.sub(r"\r", r"\\r", value)
value = re.sub(r"\t", r"\\t", value)
value = re.sub('"', r"\"", value)

return match.group(1) + value + match.group(3)


def custom_parser(multiline_string):
def custom_parser(multiline_string: str) -> str:
if isinstance(multiline_string, (bytes, bytearray)):
multiline_string = multiline_string.decode()

Expand Down Expand Up @@ -58,7 +55,7 @@ def parse_json_markdown(json_string: str) -> dict:
# Strip whitespace and newlines from the start and end
json_str = json_str.strip()

# handle newlines and other special characters that might be inside the returned value
# handle newlines and other special characters inside the returned value
json_str = custom_parser(json_str)

# Parse the JSON string into a Python dictionary
Expand Down

0 comments on commit bb766f1

Please sign in to comment.