Skip to content

Commit f7ab1da

Browse files
authored
fix(speak): correct TTS warning event field names to match API response (#653)
* fix(speak): correct TTS warning event field names to match API response Changed SpeakV1WarningEvent field names from `code`/`description` to `warn_code`/`warn_msg` to match the actual field names returned by the Deepgram API. This fixes Pydantic validation errors that were causing TTS WebSocket connections to crash instead of calling the on_warning callback when receiving 429 rate limit warnings. Updated all related tests to use the correct field names. Fixes #617 * fix(speak): update docstring and test scenarios for warn_code field - Change docstring from 'Error code' to 'Warning code' to accurately reflect warning events - Fix test scenarios to consistently use warn_msg/warn_code field names
1 parent d61ce8c commit f7ab1da

2 files changed

Lines changed: 39 additions & 39 deletions

File tree

src/deepgram/extensions/types/sockets/speak_v1_warning_event.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class SpeakV1WarningEvent(UniversalBaseModel):
1313

1414
type: typing.Literal["Warning"]
1515
"""Message type identifier"""
16-
17-
description: str
16+
17+
warn_msg: str
1818
"""A description of what went wrong"""
19-
20-
code: str
21-
"""Error code identifying the type of error"""
19+
20+
warn_code: str
21+
"""Warning code identifying the type of warning"""
2222

2323
if IS_PYDANTIC_V2:
2424
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True) # type: ignore # Pydantic v2

tests/unit/test_speak_v1_models.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,58 +157,58 @@ def test_valid_warning_event(self):
157157
"""Test creating a valid warning event."""
158158
event = SpeakV1WarningEvent(
159159
type="Warning",
160-
description="Audio quality may be degraded",
161-
code="AUDIO_QUALITY_WARNING"
160+
warn_msg="Audio quality may be degraded",
161+
warn_code="AUDIO_QUALITY_WARNING"
162162
)
163-
163+
164164
assert event.type == "Warning"
165-
assert event.description == "Audio quality may be degraded"
166-
assert event.code == "AUDIO_QUALITY_WARNING"
165+
assert event.warn_msg == "Audio quality may be degraded"
166+
assert event.warn_code == "AUDIO_QUALITY_WARNING"
167167

168168
def test_warning_event_serialization(self):
169169
"""Test warning event serialization."""
170170
event = SpeakV1WarningEvent(
171171
type="Warning",
172-
description="Audio quality may be degraded",
173-
code="AUDIO_QUALITY_WARNING"
172+
warn_msg="Audio quality may be degraded",
173+
warn_code="AUDIO_QUALITY_WARNING"
174174
)
175-
175+
176176
# Test dict conversion
177177
event_dict = event.model_dump()
178178
assert event_dict["type"] == "Warning"
179-
assert event_dict["description"] == "Audio quality may be degraded"
180-
assert event_dict["code"] == "AUDIO_QUALITY_WARNING"
181-
179+
assert event_dict["warn_msg"] == "Audio quality may be degraded"
180+
assert event_dict["warn_code"] == "AUDIO_QUALITY_WARNING"
181+
182182
# Test JSON serialization
183183
json_str = event.model_dump_json()
184184
assert '"type":"Warning"' in json_str
185-
assert '"description":"Audio quality may be degraded"' in json_str
185+
assert '"warn_msg":"Audio quality may be degraded"' in json_str
186186

187187
def test_warning_event_missing_required_fields(self):
188188
"""Test warning event with missing required fields."""
189-
# Missing description
189+
# Missing warn_msg
190190
with pytest.raises(ValidationError) as exc_info:
191191
SpeakV1WarningEvent(
192192
type="Warning",
193-
code="AUDIO_QUALITY_WARNING"
193+
warn_code="AUDIO_QUALITY_WARNING"
194194
)
195-
assert "description" in str(exc_info.value)
196-
197-
# Missing code
195+
assert "warn_msg" in str(exc_info.value)
196+
197+
# Missing warn_code
198198
with pytest.raises(ValidationError) as exc_info:
199199
SpeakV1WarningEvent(
200200
type="Warning",
201-
description="Audio quality may be degraded"
201+
warn_msg="Audio quality may be degraded"
202202
)
203-
assert "code" in str(exc_info.value)
203+
assert "warn_code" in str(exc_info.value)
204204

205205
def test_warning_event_wrong_type(self):
206206
"""Test warning event with wrong type field."""
207207
with pytest.raises(ValidationError) as exc_info:
208208
SpeakV1WarningEvent(
209209
type="Error", # Wrong type
210-
description="Audio quality may be degraded",
211-
code="AUDIO_QUALITY_WARNING"
210+
warn_msg="Audio quality may be degraded",
211+
warn_code="AUDIO_QUALITY_WARNING"
212212
)
213213
assert "Input should be 'Warning'" in str(exc_info.value)
214214

@@ -435,28 +435,28 @@ def test_warning_event_comprehensive(self):
435435
# Test common warning scenarios
436436
warning_scenarios = [
437437
{
438-
"description": "Audio quality may be degraded due to low bitrate",
439-
"code": "AUDIO_QUALITY_WARNING"
438+
"warn_msg": "Audio quality may be degraded due to low bitrate",
439+
"warn_code": "AUDIO_QUALITY_WARNING"
440440
},
441441
{
442-
"description": "Rate limit approaching",
443-
"code": "RATE_LIMIT_WARNING"
442+
"warn_msg": "Rate limit approaching",
443+
"warn_code": "RATE_LIMIT_WARNING"
444444
},
445445
{
446-
"description": "Model switching to fallback version",
447-
"code": "MODEL_FALLBACK_WARNING"
446+
"warn_msg": "Model switching to fallback version",
447+
"warn_code": "MODEL_FALLBACK_WARNING"
448448
},
449449
{
450-
"description": "Connection quality poor",
451-
"code": "CONNECTION_WARNING"
450+
"warn_msg": "Connection quality poor",
451+
"warn_code": "CONNECTION_WARNING"
452452
}
453453
]
454-
454+
455455
for scenario in warning_scenarios:
456456
event = SpeakV1WarningEvent(
457457
type="Warning",
458-
description=scenario["description"],
459-
code=scenario["code"]
458+
warn_msg=scenario["warn_msg"],
459+
warn_code=scenario["warn_code"]
460460
)
461-
assert event.description == scenario["description"]
462-
assert event.code == scenario["code"]
461+
assert event.warn_msg == scenario["warn_msg"]
462+
assert event.warn_code == scenario["warn_code"]

0 commit comments

Comments
 (0)