Skip to content

Commit 88bfb18

Browse files
authored
fix: redact Realtime audio format diagnostics (#3992)
1 parent a6ce52d commit 88bfb18

2 files changed

Lines changed: 110 additions & 3 deletions

File tree

src/agents/realtime/audio_formats.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
RealtimeAudioFormats,
1111
)
1212

13+
from .. import _debug
1314
from ..logger import logger
1415

1516

@@ -25,6 +26,8 @@ def to_realtime_audio_format(
2526
format = AudioPCMU(type="audio/pcmu")
2627
elif input_audio_format in ["g711_alaw", "audio/pcma", "pcma"]:
2728
format = AudioPCMA(type="audio/pcma")
29+
elif _debug.DONT_LOG_MODEL_DATA:
30+
logger.debug("Unknown input audio format")
2831
else:
2932
logger.debug("Unknown input_audio_format: %s", input_audio_format)
3033
elif isinstance(input_audio_format, Mapping):
@@ -37,15 +40,21 @@ def to_realtime_audio_format(
3740
elif rate is None:
3841
pcm_rate = 24000
3942
else:
40-
logger.debug(
41-
"Unknown pcm rate in input_audio_format mapping: %s", input_audio_format
42-
)
43+
if _debug.DONT_LOG_MODEL_DATA:
44+
logger.debug("Unknown PCM rate in input audio format mapping")
45+
else:
46+
logger.debug(
47+
"Unknown pcm rate in input_audio_format mapping: %s",
48+
input_audio_format,
49+
)
4350
pcm_rate = 24000
4451
format = AudioPCM(type="audio/pcm", rate=pcm_rate)
4552
elif fmt_type == "audio/pcmu":
4653
format = AudioPCMU(type="audio/pcmu")
4754
elif fmt_type == "audio/pcma":
4855
format = AudioPCMA(type="audio/pcma")
56+
elif _debug.DONT_LOG_MODEL_DATA:
57+
logger.debug("Unknown input audio format mapping")
4958
else:
5059
logger.debug("Unknown input_audio_format mapping: %s", input_audio_format)
5160
else:

tests/realtime/test_audio_formats_unit.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import logging
2+
from typing import Any
3+
4+
import pytest
15
from openai.types.realtime.realtime_audio_formats import AudioPCM, AudioPCMA, AudioPCMU
26

7+
from agents import _debug
38
from agents.realtime.audio_formats import to_realtime_audio_format
49

510

@@ -51,3 +56,96 @@ def test_to_realtime_audio_format_from_mapping():
5156
assert alaw.type == "audio/pcma"
5257

5358
assert to_realtime_audio_format({"type": "audio/unknown", "rate": 8000}) is None
59+
60+
61+
@pytest.mark.parametrize("tool_data_redacted", [False, True])
62+
@pytest.mark.parametrize(
63+
("input_audio_format", "expected_message", "expected_type"),
64+
[
65+
("format-secret", "Unknown input audio format", None),
66+
(
67+
{"type": "audio/pcm", "rate": "rate-secret"},
68+
"Unknown PCM rate in input audio format mapping",
69+
AudioPCM,
70+
),
71+
(
72+
{"type": "format-secret", "nested": "mapping-secret"},
73+
"Unknown input audio format mapping",
74+
None,
75+
),
76+
],
77+
)
78+
def test_to_realtime_audio_format_redacts_unknown_values(
79+
monkeypatch: pytest.MonkeyPatch,
80+
caplog: pytest.LogCaptureFixture,
81+
tool_data_redacted: bool,
82+
input_audio_format: Any,
83+
expected_message: str,
84+
expected_type: type[AudioPCM] | None,
85+
) -> None:
86+
monkeypatch.setattr(_debug, "DONT_LOG_MODEL_DATA", True)
87+
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", tool_data_redacted)
88+
caplog.set_level(logging.DEBUG, logger="openai.agents")
89+
90+
result = to_realtime_audio_format(input_audio_format)
91+
92+
if expected_type is None:
93+
assert result is None
94+
else:
95+
assert isinstance(result, expected_type)
96+
record = caplog.records[-1]
97+
assert record.msg == expected_message
98+
assert record.args == ()
99+
assert record.exc_info is None
100+
assert record.exc_text is None
101+
assert all(value is not input_audio_format for value in record.__dict__.values())
102+
assert logging.Formatter().format(record) == expected_message
103+
104+
105+
@pytest.mark.parametrize("tool_data_redacted", [False, True])
106+
def test_to_realtime_audio_format_preserves_diagnostic_mapping(
107+
monkeypatch: pytest.MonkeyPatch,
108+
caplog: pytest.LogCaptureFixture,
109+
tool_data_redacted: bool,
110+
) -> None:
111+
input_audio_format = {"type": "format-secret", "nested": "mapping-secret"}
112+
monkeypatch.setattr(_debug, "DONT_LOG_MODEL_DATA", False)
113+
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", tool_data_redacted)
114+
caplog.set_level(logging.DEBUG, logger="openai.agents")
115+
116+
assert to_realtime_audio_format(input_audio_format) is None
117+
118+
record = caplog.records[-1]
119+
assert record.msg == "Unknown input_audio_format mapping: %s"
120+
assert record.args is input_audio_format
121+
assert record.exc_info is None
122+
assert record.exc_text is None
123+
assert "format-secret" in logging.Formatter().format(record)
124+
assert "mapping-secret" in logging.Formatter().format(record)
125+
126+
127+
def test_to_realtime_audio_format_redaction_does_not_render_hostile_mapping(
128+
monkeypatch: pytest.MonkeyPatch,
129+
caplog: pytest.LogCaptureFixture,
130+
) -> None:
131+
class HostileMapping(dict[str, object]):
132+
def __str__(self) -> str:
133+
raise AssertionError("redacted logging must not call __str__")
134+
135+
def __repr__(self) -> str:
136+
raise AssertionError("redacted logging must not call __repr__")
137+
138+
input_audio_format = HostileMapping(type="unknown")
139+
monkeypatch.setattr(_debug, "DONT_LOG_MODEL_DATA", True)
140+
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", False)
141+
caplog.set_level(logging.DEBUG, logger="openai.agents")
142+
143+
assert to_realtime_audio_format(input_audio_format) is None
144+
145+
record = caplog.records[-1]
146+
assert record.msg == "Unknown input audio format mapping"
147+
assert record.args == ()
148+
assert record.exc_info is None
149+
assert record.exc_text is None
150+
assert all(value is not input_audio_format for value in record.__dict__.values())
151+
assert logging.Formatter().format(record) == "Unknown input audio format mapping"

0 commit comments

Comments
 (0)