Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion assemblyai/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,12 @@ class Timestamp(BaseModel):

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

Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_auto_highlights.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,39 @@ def test_auto_highlights_enabled(httpx_mock: HTTPXMock):
):
assert transcript_timestamp.start == response_timestamp["start"]
assert transcript_timestamp.end == response_timestamp["end"]


def test_auto_highlights_parses_result_without_rank(httpx_mock: HTTPXMock):
"""
Regression for #148. The Auto Highlights API has been observed in
production to return individual `results` entries with no `rank` field
set, which previously raised a Pydantic ValidationError and made the
entire TranscriptResponse unparseable. The field is now Optional, so a
missing rank parses as None and the rest of the response is preserved.
"""
mock_response = factories.generate_dict_factory(
AutohighlightTranscriptResponseFactory
)()
# Strip the `rank` from the first highlight to simulate the API's
# observed behavior. Other fields (count, text, timestamps) are left
# in place.
assert mock_response["auto_highlights_result"]["results"], (
"factory should produce at least one highlight"
)
del mock_response["auto_highlights_result"]["results"][0]["rank"]

_, transcript = unit_test_utils.submit_mock_transcription_request(
httpx_mock,
mock_response=mock_response,
config=aai.TranscriptionConfig(auto_highlights=True),
)

assert transcript.error is None
assert transcript.auto_highlights is not None
assert transcript.auto_highlights.results is not None
assert transcript.auto_highlights.results[0].rank is None
# The rest of the first result is still present, and other entries
# are unaffected.
assert transcript.auto_highlights.results[0].text is not None
if len(transcript.auto_highlights.results) > 1:
assert transcript.auto_highlights.results[1].rank is not None