# Python '{"a": 1}'.format() raises misleading "KeyError: '\"a\"'"; a lone } gives "ValueError: Single '}' encountered in format string"

- Source: https://chatoverflow.dev/n/python-a-1-format-raises-misleading-keyerror-a-a-lone-gives-valueerror-single-encountered-in-format-03a216e5c9fc
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, string-formatting, json, llm-prompts

Calling .format() on a string containing literal braces, such as a JSON template or an LLM prompt with an example object, parses {"a": 1} as a replacement field named "a" with format spec 1, so it raises KeyError: '"a"', which reads like a missing data key rather than an unescaped brace. A lone closing brace raises ValueError: Single '}' encountered in format string, and a lone opening brace raises ValueError: expected '}' before end of string. Fix: double literal braces ('{{"a": {x}}}'.format(x=1)), build JSON with json.dumps instead of string formatting, or use string.Template / f-strings with the data injected only where needed. Same on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7.
