File tree Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -20,10 +20,14 @@ def llm_parse_json(text: str) -> dict:
20
20
def escape_newlines (match : re .Match ) -> str :
21
21
return match .group (0 ).replace ("\n " , "\\ n" )
22
22
23
+ def escape_double_backslashes (match : re .Match ) -> str :
24
+ return match .group (0 ).replace ("\\ " , "\\ \\ " )
25
+
23
26
# Match anything between double quotes
24
27
# including escaped quotes and other escaped characters.
25
28
# https://regex101.com/r/VFcDmB/1
26
29
pattern = r'"(?:[^"\\]|\\.)*"'
30
+ ptext = re .sub (pattern , escape_double_backslashes , ptext )
27
31
ptext = re .sub (pattern , escape_newlines , ptext )
28
32
try :
29
33
return json .loads (ptext )
Original file line number Diff line number Diff line change @@ -376,35 +376,44 @@ def test_extract_score() -> None:
376
376
377
377
378
378
@pytest .mark .parametrize (
379
- "example" ,
379
+ ( "example" , "expected" ) ,
380
380
[
381
- """Sure here is the json you asked for!
381
+ (
382
+ """Sure here is the json you asked for!
382
383
383
384
{
384
385
"example": "json"
385
386
}
386
387
387
388
Did you like it?""" ,
388
- '{"example": "json"}' ,
389
- """
389
+ {"example" : "json" },
390
+ ),
391
+ (
392
+ """
390
393
```json
391
394
{
392
395
"example": "json"
393
396
}
394
397
```
395
398
396
399
I have written the json you asked for.""" ,
397
- """
400
+ {"example" : "json" },
401
+ ),
402
+ (
403
+ """
398
404
399
405
{
400
406
"example": "json"
401
407
}
402
408
403
409
""" ,
410
+ {"example" : "json" },
411
+ ),
412
+ ('{"example": "\\ json"}' , {"example" : "\\ json" }),
404
413
],
405
414
)
406
- def test_llm_parse_json (example : str ) -> None :
407
- assert llm_parse_json (example ) == { "example" : "json" }
415
+ def test_llm_parse_json (example : str , expected : dict ) -> None :
416
+ assert llm_parse_json (example ) == expected
408
417
409
418
410
419
def test_llm_parse_json_newlines () -> None :
You can’t perform that action at this time.
0 commit comments