Skip to content

Commit dba441a

Browse files
committed
fix(types): make AutohighlightResult.rank Optional to tolerate missing field
Closes #148. The Auto Highlights API has been observed in production to return individual `auto_highlights_result.results` entries with no `rank` field set. The SDK currently declares `rank: float` as required, so the Pydantic validator raises a `ValidationError` while parsing the overall `TranscriptResponse` — turning a missing-field-on-one-entry into a hard failure that loses the entire transcript (text, utterances, words, sentiment, every other audio-intelligence result). Make `rank` Optional with a default of `None` so a missing rank parses through cleanly. The absence is observable to callers via `rank is None` — no silent default-value substitution. The change is narrowly scoped to the single field surfaced by the reporter. The other fields on `AutohighlightResult` (count, text, timestamps) remain required. Regression test deletes `rank` from the first highlight in the mock response, asserts the TranscriptResponse parses, the affected entry's rank is None, and other entries still have their rank set. Verified that the new test fails on master (the existing required-rank contract) and passes on this commit.
1 parent 7407e85 commit dba441a

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

assemblyai/types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,12 @@ class Timestamp(BaseModel):
21402140

21412141
class AutohighlightResult(BaseModel):
21422142
count: int
2143-
rank: float
2143+
# The Auto Highlights API has been observed in production to return
2144+
# individual `results` entries with no `rank` field set (see #148).
2145+
# Treat it as optional so a TranscriptResponse parses successfully when
2146+
# any single highlight is missing the rank; the absence is then
2147+
# observable to callers via `rank is None`.
2148+
rank: Optional[float] = None
21442149
text: str
21452150
timestamps: List[Timestamp]
21462151

tests/unit/test_auto_highlights.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,39 @@ def test_auto_highlights_enabled(httpx_mock: HTTPXMock):
9393
):
9494
assert transcript_timestamp.start == response_timestamp["start"]
9595
assert transcript_timestamp.end == response_timestamp["end"]
96+
97+
98+
def test_auto_highlights_parses_result_without_rank(httpx_mock: HTTPXMock):
99+
"""
100+
Regression for #148. The Auto Highlights API has been observed in
101+
production to return individual `results` entries with no `rank` field
102+
set, which previously raised a Pydantic ValidationError and made the
103+
entire TranscriptResponse unparseable. The field is now Optional, so a
104+
missing rank parses as None and the rest of the response is preserved.
105+
"""
106+
mock_response = factories.generate_dict_factory(
107+
AutohighlightTranscriptResponseFactory
108+
)()
109+
# Strip the `rank` from the first highlight to simulate the API's
110+
# observed behavior. Other fields (count, text, timestamps) are left
111+
# in place.
112+
assert mock_response["auto_highlights_result"]["results"], (
113+
"factory should produce at least one highlight"
114+
)
115+
del mock_response["auto_highlights_result"]["results"][0]["rank"]
116+
117+
_, transcript = unit_test_utils.submit_mock_transcription_request(
118+
httpx_mock,
119+
mock_response=mock_response,
120+
config=aai.TranscriptionConfig(auto_highlights=True),
121+
)
122+
123+
assert transcript.error is None
124+
assert transcript.auto_highlights is not None
125+
assert transcript.auto_highlights.results is not None
126+
assert transcript.auto_highlights.results[0].rank is None
127+
# The rest of the first result is still present, and other entries
128+
# are unaffected.
129+
assert transcript.auto_highlights.results[0].text is not None
130+
if len(transcript.auto_highlights.results) > 1:
131+
assert transcript.auto_highlights.results[1].rank is not None

0 commit comments

Comments
 (0)