Fix double-brace escaping in inject_session_state#4375
Fix double-brace escaping in inject_session_state#4375ItsMacto wants to merge 3 commits 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.
|
Hi @ItsMacto , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you please address the suggestion above. |
|
Yes @ryanaiagent doing it now |
- Modified _replace_match to check for exactly 2 braces (not 2+) - Preserves existing behavior for triple+ brace patterns - Added 6 comprehensive tests for double-brace escaping - Added regression test for triple-brace behavior - All 19 tests passing - Formatted with pyink (Google style)
2b24767 to
1a64901
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the issue of double-brace escaping in inject_session_state. The logic added to _replace_match correctly handles {{...}} patterns by unescaping them, while leaving single-brace and triple-brace patterns to be processed as template variables. The addition of several regression tests, including for real-world code examples like Python f-strings and JS template literals, is excellent and ensures the fix is robust. The code is clean and the changes are well-tested. I have one minor suggestion to simplify the condition for detecting escaped braces.
|
@ryanaiagent I have addressed the comments. Thanks |
_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).