Skip to content

fix(medcat-trainer): Fix remote model service errors on cache_project…#363

Merged
alhendrickson merged 3 commits intomainfrom
fix/medcat-trainer/fix-remote-models
Mar 9, 2026
Merged

fix(medcat-trainer): Fix remote model service errors on cache_project…#363
alhendrickson merged 3 commits intomainfrom
fix/medcat-trainer/fix-remote-models

Conversation

@alhendrickson
Copy link
Collaborator

Discussion

  • I've fixed this for now - would like to merge in to then use the created image in k8s
  • Some of the changes are formatting/whitespace. Apologies if this confuses it
  • The service payload that existed in the code doesnt match what medcat-service used. As I'm open to there being some API it was calling instead, I've kept some of this change behind an env var, but would like to only support one at a later date. Would like to merge this PR in, and check at a later date with @tomolopolis which API format is supported.
  • Note - looks like there is another bug with SOLR, affecting both types of project, will look into next

Current behavior

  • Create a project with remote model service
  • Open the project
  • See this error
image

Underlying call is this. Service prints no logs.

**GET** /api/cache-project-model/3/

**HTTP 500 Internal Server Error**
**Allow:** GET, OPTIONS, DELETE
**Content-Type:** application/json
**Vary:** Accept

{
    "message": "'NoneType' object has no attribute 'id'"
}

Fixed behavior

image

Details of each line

Add logging

  • After adding exception handling logging the errors
cache_project_model failed for project_id=3: 'NoneType' object has no attribute 'id'
...
File "/home/api/api/views.py", line 839, in cache_project_model
  get_medcat(project)
File "/home/api/api/model_cache.py", line 186, in get_medcat
  cat = get_cached_medcat(project, cat_map)
File "/home/api/api/model_cache.py", line 203, in get_cached_medcat
  cdb_id = project.concept_db.id
AttributeError: 'NoneType' object has no attribute 'id'
  • So I fixed get_cached_medcat

Fix service payload

  • After fixing the cache_project_model method, it hits 422 calling the service. The payload is wrong
image

Fix metatask2obj error

[medcattrainer] WARNING 2026-03-09 12:26:21,012 views.py l:347:Error preparing documents for project 4
[medcattrainer] Traceback (most recent call last):
[medcattrainer]   File "/home/api/api/views.py", line 313, in prepare_documents
[medcattrainer]     add_annotations(spacy_doc=spacy_doc,
[medcattrainer]   File "/home/api/api/utils.py", line 215, in add_annotations
[medcattrainer]     metatask2obj = {task_name: MetaTask.objects.get(name=task_name)
[medcattrainer]                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[medcattrainer]   File "/home/.venv/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
[medcattrainer]     return getattr(self.get_queryset(), name)(*args, **kwargs)
[medcattrainer]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[medcattrainer]   File "/home/.venv/lib/python3.12/site-packages/django/db/models/query.py", line 635, in get
[medcattrainer]     raise self.model.DoesNotExist(
[medcattrainer] api.models.MetaTask.DoesNotExist: MetaTask matching query does not exist.
[medcattrainer] Internal Server Error: /api/prepare-documents/
[medcattrainer] ERROR 2026-03-09 12:26:21,015 log.py l:253:Internal Server Error: /api/prepare-documents/
[medcattrainer] [09/Mar/2026 12:26:21] "POST /api/prepare-documents/ HTTP/1.0" 500 70

I commented out the unused code in utils.py to get metatask2obj , instead of fixing it. The variable was unused

Copy link
Collaborator

@mart-r mart-r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall.

Clearly Cursor made this original implementatio nin #264 . My best guess is that it didn't check the actual API it was targetting in medcat-service and Tom didn't run a full end to end integration test.
But you're right, it's best to have him comment on it once he's back and at the same time allow you to continue your stuff.

raise Exception(f"Failed to process remote model service response: {str(e)}") from e


def call_remote_model_service_medcat(service_url, text):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very similar to the call_remote_model_service_spacy method above. The only difference in the setup is the payload and then when reading output the entities are read differently. So there's a bunch of duplicate code other than that.

I suppose your purpose was to keep both and (probably) just remove the old / incorrect implementation later down the line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I 100% planned to just delete one of them later.

As a side note - the original payload it tried to use is much closer to the one I'd also want medcat-service to have, so I'm open to there being some other service api going around... eg I think cohort has built its own.

Copy link
Collaborator Author

@alhendrickson alhendrickson Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers - I've added a test for this too, it hits the same APIs the UI will. As I've used cursor to fix cursor it feels like an infinite loop / infinite revenue for them . This is until the planet runs out of resources anyway.

@alhendrickson alhendrickson merged commit 6dff370 into main Mar 9, 2026
10 checks passed
@alhendrickson alhendrickson deleted the fix/medcat-trainer/fix-remote-models branch March 9, 2026 14:29
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