Skip to content

Fix srsly.read_jsonl.v1 patterns in [initialize] for entity_ruler/span_ruler - #14091

Open
AsadullahMaliyawala wants to merge 1 commit into
explosion:masterfrom
AsadullahMaliyawala:fix/init-settings-generator
Open

AsadullahMaliyawala wants to merge 1 commit into
explosion:masterfrom
AsadullahMaliyawala:fix/init-settings-generator

Conversation

@AsadullahMaliyawala

Copy link
Copy Markdown

Description

Since the Pydantic v2 migration (#13940), the documented way of loading ruler patterns from a JSONL file fails:

[initialize.components.span_ruler.patterns]
@readers = "srsly.read_jsonl.v1"
path = "patterns.jsonl"
Error validating initialization settings in [initialize.components]
span_ruler -> patterns	Input should be an instance of Sequence
{'patterns': <generator object read_jsonl at 0x...>}

srsly.read_jsonl.v1 returns a generator, and EntityRuler.initialize / SpanRuler.initialize annotate patterns as Optional[Sequence[PatternType]]. Pydantic v1 consumed a generator into a list for Sequence fields; Pydantic v2 rejects it. This affects entity_ruler, future_entity_ruler and span_ruler (the config snippet from the EntityRuler/SpanRuler API docs). attribute_ruler is not affected because it uses Iterable.

The fix materializes generator values in validate_init_settings before 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_ruler one by @sentiasm). The first report in that issue (list -> set[str] / str -> Path in factory default_config) comes from confection'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

  • New test_entity_ruler_init_patterns_from_jsonl_reader (both entity ruler factories) and test_span_ruler_init_patterns_from_jsonl_reader: write the patterns with srsly.write_jsonl and initialize through the srsly.read_jsonl.v1 reader. 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 format clean 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

  • I confirm that I have the right to submit this contribution under the project's MIT license.
  • I ran the tests, and all new and existing tests passed.
  • My changes don't require a change to the documentation, or if they do, I've added all required information.

…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

No deployments
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