For bisect_left and bisect_right, key is applied to records in the sequence, not to the search value x. Pass x in the key domain. insort accepts a record and applies key during its search.
Check the failure with a small fixture
bisect_left with a key function compares an extracted key to the supplied search value.
from bisect import bisect_left, insort_left
rows = [{"score": 10}, {"score": 30}]
key = lambda row: row["score"]
try:
bisect_left(rows, {"score": 20}, key=key)
except TypeError:
print("integer key compared with dictionary")
position = bisect_left(rows, 20, key=key)
assert position == 1
insort_left(rows, {"score": 20}, key=key)
assert [r["score"] for r in rows] == [10, 20, 30]
assert bisect_left(rows, 20, key=key) == 1What the result means
The search compares integer scores from the stored dictionaries with x. Supplying a dictionary as x makes those comparisons incompatible. The insertion helper has a different input contract because it must retain the full new record in the sequence after locating a position.
Choose the correction for your caller
Keep the sequence sorted under the exact key function used for lookup. A key that depends on mutable fields becomes invalid when those fields change; reestablish ordering before searching. Use left insertion when equal keys should be placed before existing equals and right insertion for the opposite boundary. A returned insertion index is not evidence of an exact match: check the index is in range and compare the stored key when equality is required.
Limits and the next check
The key parameter requires Python 3.10 or later. Insertion still shifts list elements, so logarithmic search does not make the entire insertion logarithmic. Concurrent mutation needs separate coordination.
Related: Compare a related data or execution contract.
The embedded Python example was executed as an isolated local script on November 10, 2026 (KST), using Python 3.11.6. It demonstrates the stated boundary; it is not a test of your application or a promise of compatibility with every Python release.