[Bug] Claude generates invalid Python syntax with escaped characters in code execution

Resolved 💬 4 comments Opened Mar 25, 2026 by AnMakc Closed Mar 29, 2026

Bug Description
Claude oftenly fails to execute python code due to syntax errors. E.g. if len(parts) \!= 2: does not require escaping with \. This results in unnecessary retries. Happens a lot with python code executed directly with Bash() tools which could be a tool related issue, but it seems I've also seen this in the python code that claude writes/edits in .py files.

Environment Info

  • Platform: darwin
  • Terminal: Apple_Terminal
  • Version: 2.1.83
  • Feedback ID: 23f6743b-b6d4-4584-a870-8835415286d2

Errors
Example output:

⏺ Bash(zstd -dc data/live/2026-03-25T18-45.jsonl.zst | uv run --offline --frozen python3 -c "                                                                         
      import sys, msgspec                                                                                                                                           
                                                                                                                                                                      
      dec = msgspec.json.Decoder()                                                                                                                                  
      list_sizes = {}                                                                                                                                                 
                                                                                                                                                                    
      for i, line in enumerate(sys.stdin, 1):                                                                                                                       
          parts = line.strip().split(' ', 1)
          if len(parts) != 2:                              # LINE WITH ERROR
              continue
          msg = dec.decode(parts[1].encode())
          if isinstance(msg, list):
              sz = len(msg)
              list_sizes[sz] = list_sizes.get(sz, 0) + 1

      print('List-wrapped messages by size:', list_sizes)
      "
      )
  ⎿  Error: Exit code 1
       File "<string>", line 9
         if len(parts) \!= 2:                                     
                        ^
     SyntaxError: unexpected character after line continuation character     

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗