parser: support Python-style implicit string concatenation - #91
Open
rnvibes wants to merge 1 commit into
Open
Conversation
Adjacent string literals ("a" "b") are folded into one string ("ab"),
matching Jinja semantics. Some model chat templates (e.g. Gemma-4-E2B) embed
multi-line error messages this way inside raise_exception(...) call args,
which previously failed to parse with 'Expected closing parenthesis in call
args'. Concatenation is only folded between string literals, so operator
expressions and identifiers are unaffected.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The chat template of
gemma-4-E2B_q4_0-it.gguf(and other Gemini/Gemma-familyGGUFs) embeds a multi-line tool-call error message using Python-style adjacent
string concatenation:
{{- raise_exception( "chat_template: tool_calls[].function.arguments must be a " "JSON object (mapping), not a string. Deserialize arguments " "before passing to the template." ) -}}Jinja (the reference implementation) accepts this — adjacent string literals
concatenate at the token level. Minja's parser rejects it with
Expected closing parenthesis in call args, so the model fails to load at all.Change
In
parseConstant(), after parsing a string literal, fold any immediatelyfollowing string literals (skipping whitespace) into the same
Value. Thismatches Jinja's semantics:
"a" "b"→"ab"operators (
+,~,|), or single literals.Why this placement
Implicit concatenation is a token-level feature of the language (only string
literals fold), so
parseConstant()is the correct single place — it is sharedby every expression context: call args, arrays, set statements,
{{ }}output.Verification
raise_exception(...)form now parsesand renders.
"a" + "b","a" ~ "b","a"|filter,identifiers) are unaffected — the fold only triggers when the next token is
a string literal.