-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path06_json_schema_enforcer.py
More file actions
66 lines (51 loc) · 1.64 KB
/
Copy path06_json_schema_enforcer.py
File metadata and controls
66 lines (51 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
import json
import os
import sys
from google import genai
from dotenv import load_dotenv
load_dotenv()
def enforce_json(file_path):
if not os.path.exists(file_path):
print(f"Error: File {file_path} not found.")
return
with open(file_path, 'r', encoding='utf-8') as f:
dirty_json = f.read()
try:
# Check if it's already valid
json.loads(dirty_json)
print("JSON is already valid.")
return
except json.JSONDecodeError:
print("JSON is invalid. Asking Gemini to fix it...")
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
prompt = f"""
The following JSON is malformed or "dirty". Fix all structural errors and return ONLY the valid, strictly typed JSON string.
Dirty JSON:
{dirty_json}
"""
try:
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=prompt,
config={
'response_mime_type': 'application/json',
}
)
clean_json = response.text
# Validate the output from Gemini
json.loads(clean_json)
print("=== Cleaned JSON ===")
print(clean_json)
except Exception as e:
print(f"Error: {e}")
def main():
parser = argparse.ArgumentParser(description="JSON Schema Enforcer CLI")
parser.add_argument("file", help="Path to the dirty JSON log file")
args = parser.parse_args()
if not os.getenv("GEMINI_API_KEY"):
print("Error: GEMINI_API_KEY not found.")
sys.exit(1)
enforce_json(args.file)
if __name__ == "__main__":
main()