Add handling of empty strings#325
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Handle Empty Strings in Inference Methods
Summary
This PR fixes the #316 issue. The fix ensures that empty inputs are gracefully handled by filtering them out before processing and mapping results back to the original indices.
Problem
Issue Description
When using GLiNER models (particularly
knowledgator/gliner-x-base), inference fails with anIndexErrorwhen the input list contains empty strings or whitespace-only strings.Reproduction
Error Traceback
Root Cause
Empty strings produce no tokens during preprocessing, resulting in empty token index mappings. When the decoder attempts to access these mappings, it raises an
IndexErrorbecause the indices don't exist.Solution
Implementation Approach
The fix implements a filter-process-remap strategy:
Key Changes
1. New Helper Methods in
BaseEncoderGLiNER_filter_valid_texts()(lines 1185-1204)_map_entities_to_original()(lines 1206-1250)2. Updated
inference()MethodThe
inference()method inBaseEncoderGLiNER(lines 1289-1381) now:_filter_valid_texts()_map_entities_to_original()3. Model Types Updated
The fix has been applied to all model types that inherit from
BaseEncoderGLiNER:UniEncoderSpanGLiNERUniEncoderTokenGLiNERBiEncoderSpanGLiNERBiEncoderTokenGLiNERUniEncoderSpanDecoderGLiNERUniEncoderSpanRelexGLiNER(relation extraction)For relation extraction models, a similar
_process_relations()method was added to handle relation mapping.Expected Behavior
After this fix: