File size: 402 Bytes
b0b0d13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import json

def extract_json_from_output(output_str: str) -> dict:
    """
    Extracts a JSON object from a string containing extra text.
    """
    start = output_str.find('{')
    end = output_str.rfind('}')
    if start == -1 or end == -1:
        return None
    json_str = output_str[start:end+1]
    try:
        return json.loads(json_str)
    except json.JSONDecodeError:
        return None