Now that `ModelSettings` has `Reasoning`, a non-primitive object, `dataclasses.as_dict()` wont work. It will raise an error when you try to serialize (e.g. for tracing). This ensures the object is actually serializable.
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
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)
|