Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ending quote parsing error in qstring #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lmql/language/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,15 @@ def transform_Constant(self, constant, constraints=None):
return constant

# transform expressions embedded in f-strings
# in case ending with quote, add a trailing space to avoid SyntaxError: unterminated string literal
# check issue #324
if compiled_qstring.endswith('"'):
compiled_qstring += " "
qstring_as_fstring: ast.JoinedStr = ast.parse(f'f"""{compiled_qstring}"""').body[0].value

function_call_transformer = FunctionCallTransformation()
# remove the added space
qstring_as_fstring.values[-1].value = qstring_as_fstring.values[-1].value[:-1]
qstring_as_fstring.values = [function_call_transformer.visit(v) for v in qstring_as_fstring.values]

# check and collect extra args for decoder, type and decorators
Expand Down