TheAlgorithms/Python · error · ValueError

We need some text to work with.

Error message

We need some text to work with.

What it means

Raised by Lz77._find_encoding_token() in data_compression/lz77.py when the text argument is empty (falsy). The token encoder must return at least one literal character (Token(offset, length, text[length])), which is impossible with no text, so empty text is rejected. Note the empty search_buffer is fine — only empty text raises.

Source

Thrown at data_compression/lz77.py:164

        Tests:
            >>> lz77_compressor = LZ77Compressor()
            >>> lz77_compressor._find_encoding_token("abrarrarrad", "abracad").offset
            7
            >>> lz77_compressor._find_encoding_token("adabrarrarrad", "cabrac").length
            1
            >>> lz77_compressor._find_encoding_token("abc", "xyz").offset
            0
            >>> lz77_compressor._find_encoding_token("", "xyz").offset
            Traceback (most recent call last):
                ...
            ValueError: We need some text to work with.
            >>> lz77_compressor._find_encoding_token("abc", "").offset
            0
        """

        if not text:
            raise ValueError("We need some text to work with.")

        # Initialise result parameters to default values
        length, offset = 0, 0

        if not search_buffer:
            return Token(offset, length, text[length])

        for i, character in enumerate(search_buffer):
            found_offset = len(search_buffer) - i
            if character == text[0]:
                found_length = self._match_length_from_index(text, search_buffer, 0, i)
                # if the found length is bigger than the current or if it's equal,
                # which means it's offset is smaller: update offset and length
                if found_length >= length:
                    offset, length = found_offset, found_length

        return Token(offset, length, text[length])

View on GitHub (pinned to f5988cc097)

Solutions

  1. Do not call _find_encoding_token with an empty text slice — check `if not text: break` in your loop before the call.
  2. If compressing user data, short-circuit empty inputs at the top of your compress function.
  3. Prefer the public compress/decompress API of the Lz77 class instead of the private helper.

Example fix

# before
for i in range(len(data)):
    token = self._find_encoding_token(data[i:], window)  # last call may get ''

# after
remaining = data[i:]
if not remaining:
    break
token = self._find_encoding_token(remaining, window)
Defensive patterns

Strategy: validation

Validate before calling

remaining = text[pos:]
if not remaining:
    break  # done, no more tokens
token = lz77_compressor._find_encoding_token(remaining, search_buffer)

Prevention

When it happens

Trigger: Calling _find_encoding_token('', 'xyz') as in the doctest, or feeding an empty remainder during compression when the sliding window logic advances past the end of input and calls the method with an empty slice.

Common situations: Custom integrations that reuse this private helper on their own windowing scheme and mis-handle the final iteration; empty file or empty chunk reaching the compressor's inner loop.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/dab482921ddf9c10. Report an issue: GitHub.