Skip to content

feat(medcat): Add component contracting#582

Open
mart-r wants to merge 40 commits into
mainfrom
feat/medcat/add-component-contracting
Open

feat(medcat): Add component contracting#582
mart-r wants to merge 40 commits into
mainfrom
feat/medcat/add-component-contracting

Conversation

@mart-r

@mart-r mart-r commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

This PR adds component contracting.

So far, what each component needs and provides has only been implicitly defined in the existing component behaviour. But it has not really been defined anywhere.

This PR adds such capabilities to the library. More specifically, it adds per core component type contracting. This contract isn't checked at run time, but the PR also adds simple helper methods that make verifying contracts extremely easy.

For instance, the simplest way to check that all the components (for now, only NER and linker) would be:

from unittest import TestCase
from medcat.components.contracting_testing import assert_component_contracts
# implement create_model_with_my_component
class MyComponentTest(TestCase):
    def test_my_model_contract(self):
        # create or load a model with your custom component(s)
        # NOTE: This would (generally) need to be able to NER / link 1 entity in the example text
        #       The test time models in medcat would be sufficient
        cat = create_model_with_my_component()
        assert_component_contracts(cat)

What the contracting tests check:

  • Whether all the expected must-provide fields are written (per document or token)
  • Whether all the expected needs fields are read (per document or token)
  • Whether all the output entities have the required fields (e.g detected_name for NER and cui for linker)

Notably, the utility method also makes a few assumption. For instance, it assumes that for the NER step token.to_skip is called for each token (which it normally is) and ner_ents are written once. And for the linker, it assumes that ner_ents are read once and linked_ents written once. Plus, it assumes that the output of either has at least 1 entity (otherwise collection contracting cannot be checked).

Open question
Should we allow enabling this for "the first document" in production with a (defaults-to-false) config option?

github-actions Bot and others added 30 commits June 8, 2026 16:30
@alhendrickson

Copy link
Copy Markdown
Collaborator

Could you add this into the documentation too? As you've basically already written it in the PR description which explains it well

@mart-r

mart-r commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

Could you add this into the documentation too? As you've basically already written it in the PR description which explains it well

I tried to figure out where to put this and went on the docs page:
https://docs.cogstack.org/projects/nlp/en/latest/
It's a mess between actual v2 things and legacy v1 things. For instance, the quick start example code block straight up won't work on v2.
I'll address that in a separate PR.

For this one, I'll just add some bits to the README.

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.

2 participants