Crash when metadata is not a dict
The helper _inject_chat_id_as_user calls metadata.get('chat_id') without checking that metadata is a dict. If a client sends a non-dict value for metadata (e.g. an empty list [], a string, a number), metadata.get(...) raises AttributeError, resulting in a 500 error. The condition only uses truthiness (if metadata), which passes for non-empty lists and other truthy non-dict objects. This introduces a new crash path compared to the previous code that just popped and ignored metadata.
if ENABLE_FORWARD_CHAT_ID and isinstance(metadata, dict) and metadata.get('chat_id') and 'user' not in payload: