Repository navigation
Fix srsly.read_jsonl.v1 patterns in [initialize] for entity_ruler/span_ruler - #14091
Open
AsadullahMaliyawala wants to merge 1 commit into
Open
AsadullahMaliyawala wants to merge 1 commit into
AsadullahMaliyawala wants to merge 1 commit into
Conversation
…tterns) Readers such as srsly.read_jsonl.v1 return a generator. Since the Pydantic v2 migration, validate_init_settings rejects a generator for fields annotated Sequence[...] (Pydantic v1 consumed it into a list), so the documented [initialize.components.entity_ruler.patterns] / span_ruler config fails with 'Input should be an instance of Sequence'. Materialize generators before validating. Fixes the span_ruler part of explosion#13944.
This branch has not been deployed
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.
Description
Since the Pydantic v2 migration (#13940), the documented way of loading ruler patterns from a JSONL file fails:
srsly.read_jsonl.v1returns a generator, andEntityRuler.initialize/SpanRuler.initializeannotatepatternsasOptional[Sequence[PatternType]]. Pydantic v1 consumed a generator into a list forSequencefields; Pydantic v2 rejects it. This affectsentity_ruler,future_entity_rulerandspan_ruler(the config snippet from the EntityRuler/SpanRuler API docs).attribute_ruleris not affected because it usesIterable.The fix materializes generator values in
validate_init_settingsbefore validation, which restores the v1 behaviour for every[initialize]block rather than changing individual component annotations.This addresses the second report in #13944 (the
span_rulerone by @sentiasm). The first report in that issue (list->set[str]/str->Pathin factorydefault_config) comes fromconfection's validation and is not changed here.Reproduced on spaCy 3.8.20 (current master), confection 1.3.4, pydantic 2.14.4, Python 3.12.
Tests
test_entity_ruler_init_patterns_from_jsonl_reader(both entity ruler factories) andtest_span_ruler_init_patterns_from_jsonl_reader: write the patterns withsrsly.write_jsonland initialize through thesrsly.read_jsonl.v1reader. They fail before the change and pass after.spacy/tests/pipeline,spacy/tests/training,spacy/tests/test_misc.py,test_language.py,serialize/test_serialize_config.py: all pass (617 passed in pipeline/training/misc; same as before plus the 3 new tests).ruff formatclean on the changed files.I used an AI coding assistant (Claude) while investigating this; I reproduced the bug and ran the tests myself.
Types of change
Bug fix (regression from 3.8.11).
Checklist