# How to change the type of a note?

**URL:** <https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906>\
**Category:** Development\
**Created:** [July 10, 2024, 2:33pm UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906 "2024-07-10T14:33:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 10, 2024, 2:33pm UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/1 "2024-07-10T14:33:57Z")

</div>

What is the proper way to change the note type of a card from within an add-on? Is there an example for that?

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [July 13, 2024, 5:35pm UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/2 "2024-07-13T17:35:46Z")

</div>

See how it’s done by Anki here:

> <https://github.com/ankitects/anki/blob/main/qt/aqt/changenotetype.py>

---

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 14, 2024, 10:40am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/3 "2024-07-14T10:40:46Z")

</div>

Thanks! It pushed me in the right direction!

---

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 14, 2024, 2:48pm UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/4 "2024-07-14T14:48:30Z")

</div>

I still can’t figure it out. This is my current code:

```auto
change_notetype_info = mw.col.models.change_notetype_info(
	old_notetype_id=old_notetype_id,
	new_notetype_id=new_notetype_id,
)

note_ids: Sequence[NoteId] = []
# Fill the list ...

req = ChangeNotetypeRequest()
req.ParseFromString(change_notetype_info.SerializeToString())
req.note_ids.extend(note_ids)

mw.col.models.change_notetype_of_notes(req)

```

But the last line triggers an error: anki.errors.BackendError: ProtoError { info: “failed to decode Protobuf message: ChangeNotetypeRequest.old\_notetype\_id: invalid wire type: LengthDelimited (expected Varint)” }

Any ideas?

By the way, the docstring for `change_notetype_request()` does not mention that you have to serialize the argument to `ParseFromString()`.

---

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 14, 2024, 9:29pm UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/5 "2024-07-14T21:29:25Z")

</div>

Looks like this is my bad. The `note_ids` I pass look suspicious. I’ll come back, when I’ve found the reason.

---

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 15, 2024, 6:56am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/6 "2024-07-15T06:56:57Z")

</div>

The `note_ids` are okay. The “suspicious” ones were were added by the `ParseFromString()` call. I have checked both notetype ids. They both belong to existing models/note types. I have checked every note in `note_ids`. They exist and are using the model with `old_notetype_id`.

Why does this fail then?

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [July 15, 2024, 8:36am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/7 "2024-07-15T08:36:51Z")

</div>

It appears that the example given in the docstring no longer works after some changes to the protobuf structure. This example works for me:

```python
def new_names_to_indices(new_names: list[str], old_names: list[str]) -> list[int]:
    indices = []
    for name in new_names:
        try:
            indices.append(old_names.index(name))
        except ValueError:
            indices.append(-1)
    return indices

basic = mw.col.models.by_name("Basic")
basic_reversed = mw.col.models.by_name("Basic (and reversed card)")
info = mw.col.models.change_notetype_info(
    old_notetype_id=basic["id"], new_notetype_id=basic_reversed["id"]
)
req = ChangeNotetypeRequest()
req.new_fields.extend(
    new_names_to_indices(list(info.new_field_names), list(info.old_field_names))
)
req.new_templates.extend(
    new_names_to_indices(
        list(info.new_template_names), list(info.old_template_names)
    )
)
req.old_notetype_id = basic["id"]
req.new_notetype_id = basic_reversed["id"]
req.current_schema = mw.col.db.scalar("select scm from col")
req.old_notetype_name = basic["name"]
req.is_cloze = basic["type"] == 1 or basic_reversed["type"] == 1
req.note_ids.extend([1720992551032])
mw.col.models.change_notetype_of_notes(req)

```

I’ll look into sending a PR to fix the docs and maybe add a helper function to do the conversion.

---

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 15, 2024, 10:26am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/8 "2024-07-15T10:26:20Z")

</div>

Thank you! That is very much appreciated.

Was that maybe this commit? [Merging Notetypes on Import (#2612) · ankitects/anki@14de845 · GitHub](https://github.com/ankitects/anki/commit/14de8451dcecc8a08d222674c55cc2714deb4cbd)

I guess the minimum required version of my add-on will be the release containing the change.

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [July 15, 2024, 10:56am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/9 "2024-07-15T10:56:16Z")

</div>

My guess is that it’s this commit: [Change Notetype UI Rework (#1499) · ankitects/anki@6809208 · GitHub](https://github.com/ankitects/anki/commit/68092082f2d2f7d9ec91aae4129801d0f3da820b)

It adds an additional field to ChangeNotetypeInfo.

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [July 15, 2024, 10:58am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/10 "2024-07-15T10:58:15Z")

</div>

My example above can be simplified a lot:

```python
basic = mw.col.models.by_name("Basic")
basic_reversed = mw.col.models.by_name("Basic (and reversed card)")
info = mw.col.models.change_notetype_info(
    old_notetype_id=basic["id"], new_notetype_id=basic_reversed["id"]
)
req = info.input
req.note_ids.extend([1720992551032])
mw.col.models.change_notetype_of_notes(req)

```

---

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 15, 2024, 11:03am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/11 "2024-07-15T11:03:28Z")

</div>

I can confirm that the complicated version works like a charm. But I will try the simplification, too, and then mark one of your replies as the solution. Thanks a lot again!

---

<div class="post-metadata">

**Author:** ![guido.flohr](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@guido.flohr](https://forums.ankiweb.net/u/guido.flohr)\
**Post date:** [July 15, 2024, 11:22am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/12 "2024-07-15T11:22:53Z")

</div>

The simplified version is also slightly better than the long version because it also fills the “Sort Field”.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [August 14, 2024, 11:22am UTC](https://forums.ankiweb.net/t/how-to-change-the-type-of-a-note/46906/13 "2024-08-14T11:22:56Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
