import ast import xml.etree.ElementTree as ET def append_root_tags(string): if not string.strip().startswith(''): string = '\n' + string if not string.strip().endswith(''): string += '\n' return string def parse_xml_to_dict(xml_string): final_answer_element = '' reasoning_element = '' try: # Parse the XML string root = ET.fromstring(xml_string) # Find the 'final_answer' tag final_answer_element = root.find('final_answer').text # Find the 'reasoning' tag reasoning_element = root.find('reasoning').text except Exception: try: assert '' in xml_string assert '' in xml_string assert '' in xml_string assert '' in xml_string final_answer_start = xml_string.index('') + len('') final_answer_end = xml_string.index('') reasoning_start = xml_string.index('') + len('') reasoning_end = xml_string.index('') final_answer_element = xml_string[final_answer_start:final_answer_end] reasoning_element = xml_string[reasoning_start:reasoning_end] except Exception: final_answer_element = '' reasoning_element = '' final_answer_element = ast.literal_eval(final_answer_element.strip()) return final_answer_element, reasoning_element