Skip to content

feat(instructions_utils): add Jinja2-based templating with use_jinja2 flag - #6593

Open
guptaishaan wants to merge 1 commit into
google:mainfrom
guptaishaan:fix/issue-2942-642
Open

feat(instructions_utils): add Jinja2-based templating with use_jinja2 flag#6593
guptaishaan wants to merge 1 commit into
google:mainfrom
guptaishaan:fix/issue-2942-642

Conversation

@guptaishaan

Copy link
Copy Markdown
Contributor

The existing regex-based substitution in inject_session_state cannot express
conditionals, loops, or filters, which limits how dynamic agent instructions
can be.

This change extracts the current regex logic into a private
_render_with_regex helper and adds a new private _render_with_jinja2
helper that sets up a Jinja2 async environment, exposes all session state
variables as top-level template variables, and provides an artifact()
async callable so templates can load artifact content inline.

A new use_jinja2: bool = False parameter is added to the public
inject_session_state function. When False (the default) the function
delegates to _render_with_regex, preserving full backward compatibility.
When True, it delegates to _render_with_jinja2, enabling Jinja2 syntax
such as {{ var }}, {% if … %}, {% for … %}, and artifact access via
{{ artifact('name') | await }}.

Unit tests covering basic variable substitution, conditionals, for-loops,
artifact loading, and undefined-variable errors are added to
tests/unittests/utils/test_instructions_utils.py.

Fixes #2942

… flag

The existing regex-based substitution in `inject_session_state` cannot express

Signed-off-by: Ishaan <ishaangupta0408@gmail.com>
@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Aug 5, 2026
copybara-service Bot pushed a commit that referenced this pull request Aug 6, 2026
Merge #6593

The existing regex-based substitution in `inject_session_state` cannot express
conditionals, loops, or filters, which limits how dynamic agent instructions
can be.

This change extracts the current regex logic into a private
`_render_with_regex` helper and adds a new private `_render_with_jinja2`
helper that sets up a Jinja2 async environment, exposes all session state
variables as top-level template variables, and provides an `artifact()`
async callable so templates can load artifact content inline.

A new `use_jinja2: bool = False` parameter is added to the public
`inject_session_state` function. When `False` (the default) the function
delegates to `_render_with_regex`, preserving full backward compatibility.
When `True`, it delegates to `_render_with_jinja2`, enabling Jinja2 syntax
such as `{{ var }}`, `{% if … %}`, `{% for … %}`, and artifact access via
`{{ artifact('name') }}`.

Unit tests covering basic variable substitution, conditionals, for-loops,
artifact loading, and undefined-variable errors are added to
`tests/unittests/utils/test_instructions_utils.py`.

Fixes #2942

PiperOrigin-RevId: 960497250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feat]: Introduce Jinja2-based Instruction Templating with a Feature Flag

3 participants