Fix double-brace escaping in inject_session_state#4375
Fix double-brace escaping in inject_session_state#4375ItsMacto wants to merge 1 commit intogoogle:mainfrom
Conversation
- Add check in _replace_match() to detect {{variable}} patterns
- Return unescaped content without state lookup attempt
- Add 6 regression tests covering basic escaping, mixed usage, and real-world code examples
- Fixes google#3527
Summary of ChangesHello @ItsMacto, 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 resolves a critical issue in the 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
|
There was a problem hiding this comment.
Code Review
This pull request correctly implements escaping for double-braced variables like {{variable}} in inject_session_state. The logic change is clear and accompanied by a comprehensive set of regression tests that cover various use cases. The overall code quality is good. My only concern is a subtle change in behavior for nested braces like {{{variable}}} which I've detailed in a specific comment. Otherwise, this is a solid fix.
| if matched_text.startswith("{{") and matched_text.endswith("}}"): | ||
| return matched_text[1:-1] |
There was a problem hiding this comment.
This check correctly handles the documented {{...}} escaping. However, it also matches patterns with more than two braces, like {{{variable}}}. This changes the behavior for such patterns. Previously, {{{variable}}} would be processed by lstrip/rstrip, resulting in the substitution of variable. With this change, it will be unescaped to {{variable}} and rendered literally.
While the new behavior is arguably more predictable, it's an undocumented side effect of this fix. If this change is intentional, consider adding a test case for it. If not, you might want to make this check more specific to exactly two braces to avoid altering the behavior for other patterns.
_replace_match()to detect{{variable}}patternsPlease ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
The ADK documentation states that double braces (
{{/}}) escape literal curly braces in instructions (e.g.,{{user_id}}should render as{user_id}), butinject_session_state()currently matches{{...}}and attempts template substitution from session state. This can raise aKeyErrorwhen the variable is not present and prevents including realistic code examples (Python f-strings / JS template literals) in instructions.Solution:
In
instructions_utils.py,_replace_match()now detects{{...}}matches and returns the unescaped content ({...}) without attempting state/artifact lookup. This implements the documented escaping behavior while preserving existing substitution behavior for normal{name}patterns.Testing Plan
Unit Tests:
Passed
pytestsummary:pytest tests/unittests/utils/test_instructions_utils.py -q(passed)pytest tests/unittests -q(passed)Manual End-to-End (E2E) Tests:
Not required for this change. This is a pure string-templating behavior fix covered by unit tests, and no runtime configuration or external services are involved.
Checklist
Checklist
Additional context
This change follows the behavior documented in the ADK State docs: double braces are used to include literal braces in instructions. Tests cover basic escaping, mixed escaping + substitution, and the real-world examples called out in the issue (Python f-strings and TypeScript/JavaScript template literals).