-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_usage.py
More file actions
56 lines (48 loc) · 1.81 KB
/
basic_usage.py
File metadata and controls
56 lines (48 loc) · 1.81 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
"""Basic outputguard usage — validate and repair LLM structured output."""
import outputguard
# Define your expected schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"hobbies": {
"type": "array",
"items": {"type": "string"},
},
},
"required": ["name", "age"],
}
# Typical LLM output with common issues:
# - Wrapped in markdown code fences
# - Trailing comma
# - Single quotes
llm_output = """```json
{'name': 'Alice', 'age': 30, 'hobbies': ['reading', 'hiking',]}
```"""
# Option 1: Validate only (will fail because of formatting issues)
result = outputguard.validate(llm_output, schema)
print(f"Valid without repair: {result.valid}") # False
# Option 2: Validate and auto-repair (handles all the issues)
result = outputguard.validate_and_repair(llm_output, schema)
print(f"Valid after repair: {result.valid}") # True
print(f"Repaired: {result.repaired}") # True
print(f"Strategies used: {result.strategies_applied}")
print(f"Clean data: {result.data}")
# {'name': 'Alice', 'age': 30, 'hobbies': ['reading', 'hiking']}
# Option 3: Repair without schema validation
repair_result = outputguard.repair(llm_output)
print(f"\nRepair-only result: {repair_result.text}")
# YAML, TOML, Python literals, and forced-JSON-off outputs use the same API.
yaml_output = """```yaml
name: Alice
age: 30
hobbies:
- reading
- hiking
```"""
yaml_result = outputguard.validate_and_repair(yaml_output, schema, format="yaml")
print(f"\nYAML valid after repair: {yaml_result.valid}") # True
python_output = "{'name': 'Alice', 'age': 30, 'hobbies': ['reading', 'hiking']}"
python_result = outputguard.validate_and_repair(python_output, schema, format="forced-json-off")
print(f"Forced-JSON-off valid: {python_result.valid}") # True