
    D_iI                        S r SSKJr  SSKrSSKrSSKJrJr  SSKJ	r	  \(       a  SSK
Jr  SS jrSS jrS	S
.SS jjr\R                  " S\R                   5      r\S.     SS jjrSr\S.     SS jjrSS jrg)zUtilities for JSON.    )annotationsN)TYPE_CHECKINGAny)OutputParserException)Callablec                0   U R                  S5      n[        R                  " SSU5      n[        R                  " SSU5      n[        R                  " SSU5      n[        R                  " SS	U5      nU R                  S
5      U-   U R                  S5      -   $ )zReplace newline characters in a regex match with escaped sequences.

Args:
    match: Regex match object containing the string to process.

Returns:
    String with newlines, carriage returns, tabs, and quotes properly escaped.
   \nz\\nz\rz\\rz\tz\\tz(?<!\\)"z\"      )groupresub)matchvalues     S/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/utils/json.py_replace_new_liner      sy     KKNEFF5&%(EFF5&%(EFF5&%(EFF;u-E;;q>E!EKKN22    c                    [        U [        [        45      (       a  U R                  5       n [        R
                  " S[        U [        R                  S9$ )aX  Custom parser for multiline strings.

The LLM response for `action_input` may be a multiline
string containing unescaped newlines, tabs or quotes. This function
replaces those characters with their escaped counterparts.
(newlines in JSON must be double-escaped: `\\n`).

Returns:
    The modified string with escaped newlines, tabs and quotes.
z("action_input"\:\s*")(.*?)("))flags)
isinstancebytes	bytearraydecoder   r   r   DOTALL)multiline_strings    r   _custom_parserr   !   sG     "UI$677+22466)ii	 r   Fstrictc               H    [         R                  " XS9$ ! [         R                   a     Of = f/ n/ nSnSnU  H  nUnU(       a1  US:X  a
  U(       d  SnOUS:X  a
  U(       d  SnOuUS:X  a  U(       + nOgSnOdUS:X  a  SnSnOYUS:X  a  UR                  S	5        OAUS
:X  a  UR                  S5        O)US;   a#  U(       a  US   U:X  a  UR	                  5         O  gUR                  U5        M     U(       a(  U(       a  UR	                  5         UR                  S5        UR                  5         U(       aZ   [         R                  " SR                  X#-   5      US9$ ! [         R                   a    UR	                  5          Of = fU(       a  MZ  [         R                  " XS9$ )zParse a JSON string that may be missing closing braces.

Args:
    s: The JSON string to parse.
    strict: Whether to use strict parsing.

Returns:
    The parsed JSON object as a Python dictionary.
r   F"
r
   \T{}[]>   r'   r%   N )jsonloadsJSONDecodeErrorappendpopreversejoin)sr   	new_charsstackis_inside_stringescapedcharnew_chars           r   parse_partial_jsonr8   ;   s   zz!++  IEG s{7#( g  %+S[#GS[LLS[LLZrd*		  	"9 @ MMO 
MMO 	::bggi&78HH## 	 MMO	 ) ::a''s    --7%E $FFz```(json)?(.*)parserc                    [        XS9$ ! [        R                   a.    [        R	                  U 5      nUc  U OUR                  S5      n Of = f[        X1S9$ )zParse a JSON string from a Markdown string.

Args:
    json_string: The Markdown string.
    parser: The parser to use. Defaults to `parse_partial_json`.

Returns:
    The parsed JSON object as a Python dictionary.
r9   r	   )_parse_jsonr*   r,   _json_markdown_researchr   )json_stringr:   r   json_strs       r   parse_json_markdownrA      s\    D;66 D!((5 #(-;U[[^D x//s    ?AAz 
	`c               R    U R                  [        5      n [        U 5      n U" U 5      $ )an  Parse a JSON string, handling special characters and whitespace.

Strips whitespace, newlines, and backticks from the start and end of the string,
then processes special characters before parsing.

Args:
    json_str: The JSON string to parse.
    parser: Optional custom parser function.

        Defaults to `parse_partial_json`.

Returns:
    Parsed JSON object.
)strip_json_strip_charsr   )r@   r:   s     r   r<   r<      s,    $ ~~/0H h'H (r   c                (    [        U 5      n[	        U[
        5      (       d"  S[        U5      R                   S3n[        XPS9eU H  nXb;  d  M
  SU SU 3n[        U5      e   U$ ! [        R                   a  nSU 3n[        U5      UeSnAff = f)ao  Parse and check a JSON string from a Markdown string.

Checks that it contains the expected keys.

Args:
    text: The Markdown string.
    expected_keys: The expected keys in the JSON string.

Returns:
    The parsed JSON object as a Python dictionary.

Raises:
    OutputParserException: If the JSON string is invalid or does not contain
        the expected keys.
z Got invalid JSON object. Error: Nz&Expected JSON object (dict), but got: z. )
llm_outputz)Got invalid return object. Expected key `z` to be present, but got )rA   r*   r,   r   r   dicttype__name__)textexpected_keysjson_objemsgerror_messagekeys          r   parse_and_check_json_markdownrQ      s     0&t, h%%4T(^5L5L4MRP 	 $MCC;C5 A**25  (,,  O!  004#C(a/0s   A' 'B;BB)r   zre.Match[str]returnstr)r   zstr | bytes | bytearrayrR   rS   )r1   rS   r   boolrR   r   )r?   rS   r:   Callable[[str], Any]rR   r   )r@   rS   r:   rU   rR   r   )rJ   rS   rK   z	list[str]rR   rG   )__doc__
__future__r   r*   r   typingr   r   langchain_core.exceptionsr   collections.abcr   r   r   r8   compiler   r=   rA   rD   r<   rQ    r   r   <module>r]      s     "  	 % ;(3$4 27 N(b JJ0"))<  9K00!5000   6H26"r   