fix: restore double-brace escaping in instruction templates#4613
fix: restore double-brace escaping in instruction templates#4613stakeswky wants to merge 2 commits intogoogle:mainfrom
Conversation
Double braces (e.g. {{city}}) should produce literal {city} in the
rendered instruction, not trigger a session state lookup. This escaping
worked in v1.25.0 but regressed in v1.25.1 because the regex
r'{+[^{}]*}+' matches {{city}} and _replace_match strips all braces
before looking up the variable name.
The fix adds an early return in _replace_match: when the matched text
starts with {{ and ends with }}, peel one layer of braces and return
the literal string without any state/artifact lookup.
Fixes google#4606
|
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. |
Summary of ChangesHello @stakeswky, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical bug that caused instruction templates to misinterpret double-braced strings as dynamic variables, leading to incorrect rendering. The fix ensures that such patterns are now correctly escaped to their literal form, restoring expected behavior and improving the reliability of instruction generation. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Response from ADK Triaging Agent Hello @stakeswky, thank you for creating this PR! Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). You can visit https://cla.developers.google.com/ to see your current agreements or to sign a new one. This information will help reviewers to review your PR more efficiently. Thanks! |
There was a problem hiding this comment.
Code Review
This pull request addresses a regression where double-braced expressions like {{city}} were not being correctly escaped. The fix introduces an early return to handle {{...}} syntax as an escape sequence, correctly converting it to {...}. The change is logical, and the accompanying new tests in test_instructions_utils.py provide good coverage for the fix, including cases with mixed variables and multiple layers of braces.
I have one suggestion regarding a pre-existing issue in the parsing logic that could misinterpret malformed syntax. Addressing this would further improve the robustness of the template rendering.
| # Double (or more) braces are escape sequences: {{x}} → {x} | ||
| if raw.startswith('{{') and raw.endswith('}}'): | ||
| return raw[1:-1] | ||
| var_name = raw.lstrip('{').rstrip('}').strip() |
There was a problem hiding this comment.
The use of lstrip('{').rstrip('}') can lead to incorrect parsing for malformed brace syntax that isn't a valid escape sequence. For example, an input like '{{var}}}' would not be handled by the if condition for escaped sequences, and this line would incorrectly parse it as the variable 'var'. This is because lstrip and rstrip remove all occurrences of the specified characters from the ends of the string.
This appears to be a pre-existing issue, but it would be good to address it to make parsing more robust. A safer approach for handling single-brace variables would be to use slicing (e.g., raw[1:-1].strip()) after validating that there is exactly one opening and one closing brace.
There was a problem hiding this comment.
Good catch! Applied in 302c575 — switched to raw[1:-1].strip() slicing for safer single-brace parsing.
3399ab9 to
c160b7b
Compare
Per review suggestion, replace lstrip('{').rstrip('}') with raw[1:-1]
to avoid stripping extra braces from malformed inputs.
|
Hi closing out based on comment from issue. We are updating docs Thank you for raising! |
Summary
Fixes #4606
Double braces (e.g.
{{city}}) should produce literal{city}in the rendered instruction, not trigger a session state lookup. This escaping worked in v1.25.0 but regressed in v1.25.1.Root Cause
The regex
r'{+[^{}]*}+'ininstructions_utils.pymatches{{city}}, and_replace_matchstrips all braces before looking up the variable name, treating it as a state variablecity.Fix
Added an early return in
_replace_match: when the matched text starts with{{and ends with}}, peel one layer of braces and return the literal string without any state/artifact lookup.This is a minimal 3-line change in
_replace_match:Changes
src/google/adk/utils/instructions_utils.py: Added double-brace escape handlingtests/unittests/utils/test_instructions_utils.py: 3 new regression testsTesting
All 17 tests in
test_instructions_utils.pypass (14 existing + 3 new):test_double_braces_escape_to_literal:{{city}}→{city}test_double_braces_mixed_with_state_variable:{user_name}resolves while{{placeholder}}escapestest_triple_braces_peel_one_layer:{{{example}}}→{{example}}