Skip to content

Commit 57f0b38

Browse files
authored
fix(core): serialize only traced fields in ModelSettings.to_traceable_dict (#5003)
1 parent 0910b46 commit 57f0b38

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎src/agents/model_settings.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,12 @@ def to_json_dict(self) -> dict[str, Any]:
293293

294294
def to_traceable_dict(self) -> dict[str, Any]:
295295
"""Serialize settings for tracing without provider-specific request extras."""
296-
payload = self.to_json_dict()
296+
payload = cast(
297+
dict[str, Any],
298+
TypeAdapter(ModelSettings).dump_python(
299+
self, mode="json", include=set(_TRACEABLE_MODEL_SETTING_FIELDS)
300+
),
301+
)
297302
return {key: payload[key] for key in _TRACEABLE_MODEL_SETTING_FIELDS if key in payload}
298303

299304

‎tests/model_settings/test_serialization.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
from dataclasses import fields
33

4+
import httpx2
45
import pytest
56
from openai.types.shared import Reasoning
67
from pydantic import TypeAdapter
@@ -210,6 +211,20 @@ def test_traceable_serialization_omits_request_extras() -> None:
210211
assert "preserve_raw_usage" not in traceable
211212

212213

214+
def test_traceable_serialization_keeps_provider_native_request_extras() -> None:
215+
"""Untraced request extras must not have to be JSON serializable."""
216+
model_settings = ModelSettings(
217+
temperature=0.5,
218+
extra_args={"timeout": httpx2.Timeout(30.0)},
219+
)
220+
221+
traceable = model_settings.to_traceable_dict()
222+
223+
assert traceable["temperature"] == 0.5
224+
assert "extra_args" not in traceable
225+
assert json.dumps(traceable)
226+
227+
213228
def test_extra_args_resolve() -> None:
214229
"""Test that extra_args are properly merged in the resolve method."""
215230
base_settings = ModelSettings(

‎tests/models/test_openai_chatcompletions.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ def __bool__(self) -> bool:
134134
assert kwargs["reasoning_effort"] == "low"
135135

136136

137+
@pytest.mark.allow_call_model_methods
138+
@pytest.mark.asyncio
139+
async def test_provider_native_extra_args_reach_the_request() -> None:
140+
kwargs = await _run_chat_completions_model_with_custom_base_url(
141+
ModelSettings(temperature=0.2, extra_args={"timeout": httpx2.Timeout(30.0)})
142+
)
143+
144+
assert kwargs["timeout"] == httpx2.Timeout(30.0)
145+
146+
137147
@pytest.mark.allow_call_model_methods
138148
@pytest.mark.asyncio
139149
async def test_get_response_with_text_message(monkeypatch) -> None:

0 commit comments

Comments
 (0)