-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathtest_pydantic_field_issue.py
91 lines (75 loc) · 3.38 KB
/
test_pydantic_field_issue.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from pydantic import BaseModel, Field
from typing import Dict, Any, Optional, List, get_type_hints
from SimplerLLM.tools.json_helpers import generate_json_example_from_pydantic, example_value_for_type
# Model with Field descriptions (similar to the one causing issues)
class ModelWithFieldDesc(BaseModel):
tool_name: str = Field(..., description="Name of the tool to call")
parameters: Dict[str, Any] = Field(..., description="Parameters to pass to the tool")
optional_field: Optional[str] = Field(None, description="An optional field")
# Similar model without Field descriptions
class ModelWithoutFieldDesc(BaseModel):
tool_name: str
parameters: Dict[str, Any]
optional_field: Optional[str] = None
# Debug type hints and example values
print("Type hints for ModelWithFieldDesc:")
for field_name, field_type in get_type_hints(ModelWithFieldDesc).items():
print(f" {field_name}: {field_type}")
try:
example = example_value_for_type(field_type)
print(f" Example value: {example}")
except Exception as e:
print(f" Error generating example: {type(e).__name__}: {e}")
print("\nType hints for ModelWithoutFieldDesc:")
for field_name, field_type in get_type_hints(ModelWithoutFieldDesc).items():
print(f" {field_name}: {field_type}")
try:
example = example_value_for_type(field_type)
print(f" Example value: {example}")
except Exception as e:
print(f" Error generating example: {type(e).__name__}: {e}")
# Create a modified version of generate_json_example_from_pydantic to debug the issue
def debug_generate_json_example(model_class):
example_data = {}
for field_name, field_type in get_type_hints(model_class).items():
example_data[field_name] = example_value_for_type(field_type)
print("Raw example_data dictionary:")
import json
print(json.dumps(example_data, indent=2))
print("Parameters dictionary:")
print(json.dumps(example_data['parameters'], indent=2))
model_instance = model_class(**example_data)
model_dict = model_instance.model_dump()
print("Model dict after model_dump:")
print(json.dumps(model_dict, indent=2))
json_str = json.dumps(model_dict)
print("JSON after json.dumps:")
print(json_str)
# Write to file to verify
with open(f"test_{model_class.__name__}.json", "w") as f:
f.write(json_str)
print(f"Wrote JSON to test_{model_class.__name__}.json")
# Read back from file
with open(f"test_{model_class.__name__}.json", "r") as f:
file_content = f.read()
print(f"Read from file: {file_content}")
return json_str
# Test both models
print("\nTesting model with Field descriptions:")
try:
json_with_desc = debug_generate_json_example(ModelWithFieldDesc)
# Parse the JSON string to verify it's valid JSON
import json
parsed_json = json.loads(json_with_desc)
print(f"Parsed JSON: {json.dumps(parsed_json, indent=2)}")
except Exception as e:
print(f"Error: {type(e).__name__}: {e}")
print("\nTesting model without Field descriptions:")
try:
json_without_desc = debug_generate_json_example(ModelWithoutFieldDesc)
# Parse the JSON string to verify it's valid JSON
import json
parsed_json = json.loads(json_without_desc)
print(f"Parsed JSON: {json.dumps(parsed_json, indent=2)}")
except Exception as e:
print(f"Error: {type(e).__name__}: {e}")