|
| 1 | +import json |
| 2 | +from dataclasses import fields |
| 3 | + |
| 4 | +from openai.types.shared import Reasoning |
| 5 | + |
| 6 | +from agents.model_settings import ModelSettings |
| 7 | + |
| 8 | + |
| 9 | +def verify_serialization(model_settings: ModelSettings) -> None: |
| 10 | + """Verify that ModelSettings can be serialized to a JSON string.""" |
| 11 | + json_dict = model_settings.to_json_dict() |
| 12 | + json_string = json.dumps(json_dict) |
| 13 | + assert json_string is not None |
| 14 | + |
| 15 | + |
| 16 | +def test_basic_serialization() -> None: |
| 17 | + """Tests whether ModelSettings can be serialized to a JSON string.""" |
| 18 | + |
| 19 | + # First, lets create a ModelSettings instance |
| 20 | + model_settings = ModelSettings( |
| 21 | + temperature=0.5, |
| 22 | + top_p=0.9, |
| 23 | + max_tokens=100, |
| 24 | + ) |
| 25 | + |
| 26 | + # Now, lets serialize the ModelSettings instance to a JSON string |
| 27 | + verify_serialization(model_settings) |
| 28 | + |
| 29 | + |
| 30 | +def test_all_fields_serialization() -> None: |
| 31 | + """Tests whether ModelSettings can be serialized to a JSON string.""" |
| 32 | + |
| 33 | + # First, lets create a ModelSettings instance |
| 34 | + model_settings = ModelSettings( |
| 35 | + temperature=0.5, |
| 36 | + top_p=0.9, |
| 37 | + frequency_penalty=0.0, |
| 38 | + presence_penalty=0.0, |
| 39 | + tool_choice="auto", |
| 40 | + parallel_tool_calls=True, |
| 41 | + truncation="auto", |
| 42 | + max_tokens=100, |
| 43 | + reasoning=Reasoning(summary="auto"), |
| 44 | + metadata={"foo": "bar"}, |
| 45 | + store=False, |
| 46 | + include_usage=False, |
| 47 | + extra_query={"foo": "bar"}, |
| 48 | + extra_body={"foo": "bar"}, |
| 49 | + ) |
| 50 | + |
| 51 | + # Verify that every single field is set to a non-None value |
| 52 | + for field in fields(model_settings): |
| 53 | + assert getattr(model_settings, field.name) is not None, ( |
| 54 | + f"You must set the {field.name} field" |
| 55 | + ) |
| 56 | + |
| 57 | + # Now, lets serialize the ModelSettings instance to a JSON string |
| 58 | + verify_serialization(model_settings) |
0 commit comments