pythonpython-3.xstringsyntax-error

"Unterminated string literal" (when it is terminated) - Why does Python parsing differ from syntax highlighting on VSCode


yield f"data: {json.dumps({
                    'status': 'success',
                    'message': 'Success',
                    'data': {
                        'response': response,
                        'id': id,
                        'credits_used': credits_used,
                        'tool_calls': None,
                        'iteration': iter,
                        'file_searches': file_search_calls if file_search_calls else None
                    },
                    'error_details': None
                })}"

Consider this snippet. I get the following error: SyntaxError: unterminated string literal (detected at line 2080). This was very easily fixable by putting the dict outside in a seperate variable, but I still have one question:

Why does the syntax highlighting show as if the string was terminated, but the Python parser didn't? Could it be different versions of Python? (this is running in a container) And why would Python treat it as an open string when there is a closing quote?


Solution

  • The problem is the newlines in the string. Before Python 3.12 you can't have newlines in a string delimited by a single quote or doublequote, even inside the expression part of an f-string.

    The simplest fix is to use triple quotes.

    yield f"""data: {json.dumps({
                        'status': 'success',
                        'message': 'Success',
                        'data': {
                            'response': response,
                            'id': id,
                            'credits_used': credits_used,
                            'tool_calls': None,
                            'iteration': iter,
                            'file_searches': file_search_calls if file_search_calls else None
                        },
                        'error_details': None
                    })}"""