-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathtest_serialization.py
59 lines (46 loc) · 1.74 KB
/
test_serialization.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
import json
from dataclasses import fields
from openai.types.shared import Reasoning
from agents.model_settings import ModelSettings
def verify_serialization(model_settings: ModelSettings) -> None:
"""Verify that ModelSettings can be serialized to a JSON string."""
json_dict = model_settings.to_json_dict()
json_string = json.dumps(json_dict)
assert json_string is not None
def test_basic_serialization() -> None:
"""Tests whether ModelSettings can be serialized to a JSON string."""
# First, lets create a ModelSettings instance
model_settings = ModelSettings(
temperature=0.5,
top_p=0.9,
max_tokens=100,
)
# Now, lets serialize the ModelSettings instance to a JSON string
verify_serialization(model_settings)
def test_all_fields_serialization() -> None:
"""Tests whether ModelSettings can be serialized to a JSON string."""
# First, lets create a ModelSettings instance
model_settings = ModelSettings(
temperature=0.5,
top_p=0.9,
frequency_penalty=0.0,
presence_penalty=0.0,
tool_choice="auto",
parallel_tool_calls=True,
truncation="auto",
max_tokens=100,
reasoning=Reasoning(),
metadata={"foo": "bar"},
store=False,
include_usage=False,
extra_query={"foo": "bar"},
extra_body={"foo": "bar"},
extra_headers={"foo": "bar"},
)
# Verify that every single field is set to a non-None value
for field in fields(model_settings):
assert getattr(model_settings, field.name) is not None, (
f"You must set the {field.name} field"
)
# Now, lets serialize the ModelSettings instance to a JSON string
verify_serialization(model_settings)