Skip to content

Avoid adding empty words list to segments when word_timestamps is False#2780

Open
LeSingh1 wants to merge 1 commit into
openai:mainfrom
LeSingh1:fix-spurious-words-key
Open

Avoid adding empty words list to segments when word_timestamps is False#2780
LeSingh1 wants to merge 1 commit into
openai:mainfrom
LeSingh1:fix-spurious-words-key

Conversation

@LeSingh1

Copy link
Copy Markdown

When word_timestamps is False, segments do not have a words key. The cleanup pass for instantaneous or empty segments was unconditionally assigning segment['words'] = [], introducing the key only on those empty segments. This makes the segment schema inconsistent: some segments get a words key, others do not.

SubtitlesWriter (used by both WriteVTT and WriteSRT) decides which iteration path to take by checking 'words' in result['segments'][0]. If the first segment happens to be empty or instantaneous, that spurious key flips the writer into the word-level path, which then raises KeyError: 'words' on the next non-empty segment that lacks the key.

Minimal reproducer:

from whisper.utils import WriteSRT
import io

result = {
    'segments': [
        {'id': 0, 'seek': 0, 'start': 0.0, 'end': 0.0, 'text': '',
         'tokens': [], 'words': [],  # would be set by transcribe.py
         'temperature': 0.0, 'avg_logprob': -0.5,
         'compression_ratio': 1.5, 'no_speech_prob': 0.1},
        {'id': 1, 'seek': 0, 'start': 1.0, 'end': 3.0, 'text': 'Hello',
         'tokens': [1, 2, 3],  # no 'words' key, word_timestamps=False
         'temperature': 0.0, 'avg_logprob': -0.5,
         'compression_ratio': 1.5, 'no_speech_prob': 0.1},
    ],
}
WriteSRT('/tmp').write_result(result, file=io.StringIO())
# KeyError: 'words'

Fix: only reset words when the key is already present, matching how add_word_timestamps populates it. Behavior when word_timestamps=True is unchanged (the key is present, gets cleared to [] as before).

When word_timestamps is False, segments do not have a 'words' key.
However, the cleanup loop for instantaneous/empty segments was
unconditionally assigning segment['words'] = [], introducing the key
only for empty segments. This produces an inconsistent schema where
some segments have a 'words' key and others do not.

The SubtitlesWriter (used by WriteVTT and WriteSRT) checks whether
the first segment has a 'words' key to decide which iteration path
to take. If the first segment happened to be empty/instantaneous, the
spurious key triggered the word-based path, which then raised KeyError
on subsequent non-empty segments that lacked the key.

Only reset 'words' when it already exists so the schema stays
consistent with how add_word_timestamps populates it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant