From 54c51dd3d889ea326c3d6103709252443b56b55c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 12:31:10 +0000 Subject: [PATCH] fix(types): tolerate null `end` in sentiment analysis results The API can return a sentiment item with `"end": null` (e.g. when a word gets 0/0 timestamps), which makes `Transcript.get_by_id` fail to parse the whole transcript. Extend the existing `start` null workaround on `Word` to `end`, converting null to 0. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_013m8gQdRBqzz5kme9fBgo63 --- assemblyai/types.py | 6 +++--- tests/unit/test_sentiment_analysis.py | 31 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/assemblyai/types.py b/assemblyai/types.py index 92377f68..111e0c74 100644 --- a/assemblyai/types.py +++ b/assemblyai/types.py @@ -2161,16 +2161,16 @@ class Word(BaseModel): channel: Optional[str] = None # This is a workaround to address an issue where sentiment_analysis_results - # may return contains sentiments where `start` is null. + # may return contains sentiments where `start` or `end` is null. if pydantic_v2: - @field_validator("start", mode="before") + @field_validator("start", "end", mode="before") def set_start_default(cls, v): return 0 if v is None else v else: - @validator("start", pre=True) + @validator("start", "end", pre=True) def set_start_default(cls, v): return 0 if v is None else v diff --git a/tests/unit/test_sentiment_analysis.py b/tests/unit/test_sentiment_analysis.py index 2d86c013..125eb123 100644 --- a/tests/unit/test_sentiment_analysis.py +++ b/tests/unit/test_sentiment_analysis.py @@ -106,3 +106,34 @@ def test_sentiment_analysis_null_start(httpx_mock: HTTPXMock): transcript.sentiment_analysis, ): assert transcript_sentiment_result.start == 0 + + +def test_sentiment_analysis_null_end(httpx_mock: HTTPXMock): + """ + Tests that `end` converts null values to 0. + """ + mock_response = { + "audio_url": "https://example/audio.mp3", + "status": "completed", + "sentiment_analysis_results": [ + { + "text": "hi", + "start": 100, + "end": None, + "confidence": 0.99, + "sentiment": "POSITIVE", + } + ], + } + request_body, transcript = unit_test_utils.submit_mock_transcription_request( + httpx_mock, + mock_response=mock_response, + config=aai.TranscriptionConfig(sentiment_analysis=True), + ) + + for response_sentiment_result, transcript_sentiment_result in zip( + mock_response["sentiment_analysis_results"], + transcript.sentiment_analysis, + ): + assert transcript_sentiment_result.start == 100 + assert transcript_sentiment_result.end == 0