3b1b/manim · error · ValueError

Missing '}' inserted

Error message

Missing '}' inserted

What it means

Raised at the end of the same brace-matching pass (tex_mobject.py:127) when the LaTeX string ends with opening braces that were never closed — the open_stack is non-empty after scanning. It is the mirror of 'Missing {': the string has more '{' than matching '}'.

Source

Thrown at manimlib/mobject/svg/tex_mobject.py:127

                    (open_start, open_end), index = open_stack.pop()
                    n = min(open_end - open_start, close_end - close_start)
                    result.insert(index, pattern.fullmatch(
                        string, pos=open_end - n, endpos=open_end
                    ))
                    result.append(pattern.fullmatch(
                        string, pos=close_start, endpos=close_start + n
                    ))
                    close_start += n
                    if close_start < close_end:
                        continue
                    open_end -= n
                    if open_start < open_end:
                        open_stack.append(((open_start, open_end), index))
                    break
            else:
                result.append(match_obj)
        if open_stack:
            raise ValueError("Missing '}' inserted")
        return result

    @staticmethod
    def get_command_flag(match_obj: re.Match) -> int:
        if match_obj.group("open"):
            return 1
        if match_obj.group("close"):
            return -1
        return 0

    @staticmethod
    def replace_for_content(match_obj: re.Match) -> str:
        return match_obj.group()

    @staticmethod
    def replace_for_matching(match_obj: re.Match) -> str:
        if match_obj.group("command"):
            return match_obj.group()

View on GitHub (pinned to dee01804d4)

Solutions

  1. Close every opened brace in the tex string before constructing MathTex
  2. If isolating substrings, make each isolated fragment internally brace-balanced
  3. Render templates then assert balance (string.count('{') == string.count('}')) before passing to MathTex

Example fix

# before
MathTex(r'\sqrt{2')  # unclosed group -> raises

# after
MathTex(r'\sqrt{2}')
Defensive patterns

Strategy: validation

Validate before calling

assert tex_str.count('{') == tex_str.count('}'), 'unclosed brace group in tex string'

Try / catch

try:
    tex = MathTex(s)
except ValueError as e:
    if "Missing '}'" in str(e):
        s = s + '}' * (s.count('{') - s.count('}'))
        tex = MathTex(s)

Prevention

When it happens

Trigger: MathTex(r'{') or MathTex(r'\sqrt{a') raises; also substring isolation like MathTex(r'\frac{a}{b}', substrings_to_isolate=['\frac{']) where the isolated fragment opens a group it never closes within the string.

Common situations: Truncating LaTeX templates mid-group; string concatenation that drops a closing brace; passing half of a LaTeX command pair as an isolated substring.

Related errors


AI-assisted analysis of 3b1b/manim@dee01804d4 (2026-08-14). Data as JSON: /api/errors/afa6096c3c15fa4d. Report an issue: GitHub.