Programmatically Adding New Notes

I’m trying to write an addon that automates my note making process. Here’s my method for adding a new note to my collection.

def add_to_anki(word, definition, path):
    col = mw.col
    vocab_model = col.models.by_name('vocab-import')
    deck = col.decks.by_name('master')
    col.decks.select(deck['id'])
    col.decks.current()['mid'] = vocab_model['id']
    note = col.new_note(vocab_model)
    note.fields[0] = word
    note.fields[1] = definition
    w = mw.col.media.add_file(path)
    note.fields[2] = f'<img src="{w}">'
    col.add_note(note, deck['id'])

However, when I run these lines (strings for each of the three arguments), I get this error with my last line, add_note:

  File "anki.collection", line 528, in add_note
  File "anki.notes", line 71, in _to_backend_note
TypeError: bad argument type for built-in operation

It looks like _to_backend_note needs me to make my new note in a different way. Is there a better way to make a new note?

You’re probably providing add_to_anki() with non-string arguments.

Thank you. That was it. I only thought I was passing strings—I was not.