ansible/ansible · error · TypeError
Key of type {type(k).__name__!r} is not JSON serializable by
Error message
Key of type {type(k).__name__!r} is not JSON serializable by the {cls.profile_name!r} profile. What it means
Error "Key of type {type(k).__name__!r} is not JSON serializable by the {cls.profile_name!r} profile." thrown in ansible/ansible.
Source
Thrown at lib/ansible/module_utils/_internal/_json/_profiles/__init__.py:212
@classmethod
def post_init(cls) -> None:
pass
@classmethod
def maybe_wrap(cls, o: t.Any) -> t.Any:
if type(o) in cls._unwrapped_json_types:
return o
return _WrappedValue(o)
@classmethod
def handle_key(cls, k: t.Any) -> t.Any:
"""Validation/conversion hook before a dict key is serialized. The default implementation only accepts str-typed keys."""
# NOTE: Since JSON requires string keys, there is no support for preserving tags on dictionary keys during serialization.
if not isinstance(k, str): # DTFIX-FUTURE: optimize this to use all known str-derived types in type map / allowed types
raise TypeError(f'Key of type {type(k).__name__!r} is not JSON serializable by the {cls.profile_name!r} profile.')
return k
@classmethod
def _handle_key_str_fallback(cls, k: t.Any) -> t.Any:
"""Legacy implementations should use this key handler for backward compatibility with stdlib JSON key conversion quirks."""
# DTFIX-FUTURE: optimized exact-type table lookup first
if isinstance(k, str):
return k
if k is None or isinstance(k, (int, float)):
return json.dumps(k)
raise TypeError(f'Key of type {type(k).__name__!r} is not JSON serializable by the {cls.profile_name!r} profile.')
@classmethod
def default(cls, o: t.Any) -> t.Any:View on GitHub (pinned to 9cf16a4aca)
Solutions
- Use string keys in dictionaries that will be JSON serialized.
- Convert non-string keys to strings before returning.
When it happens
Trigger: Thrown at lib/ansible/module_utils/_internal/_json/_profiles/__init__.py:212 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of ansible/ansible@9cf16a4aca (2026-08-15).
Data as JSON: /api/errors/72dc82207a301edd.
Report an issue: GitHub.