MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / parse_value

Function parse_value

tools/_utils.py:57–76  ·  view source on GitHub ↗

Convert a string value to its appropriate type (int, float, bool, None, or keep as string). Work as a more broad 'eval()

(value: str)

Source from the content-addressed store, hash-verified

55 return None
56
57def parse_value(value: str):
58 """Convert a string value to its appropriate type (int, float, bool, None, or keep as string). Work as a more broad 'eval()'"""
59 value = value.strip()
60
61 if value == "null":
62 return None
63 elif value == "true":
64 return True
65 elif value == "false":
66 return False
67 else:
68 # Try to convert to int or float
69 try:
70 if '.' in value: # If there's a dot, it might be a float
71 return float(value)
72 else:
73 return int(value)
74 except ValueError:
75 # If conversion fails, return the value as-is (likely a string)
76 return value.strip('"') # Remove surrounding quotes if they exist
77
78def extract_values_from_json(json_string, keys=["reasoning", "answer", "data"], allow_no_quotes=False):
79 """Extract key values from a non-standard or malformed JSON string, handling nested objects."""

Callers 1

extract_values_from_jsonFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected