Skip to content

Conversation

@Jainish-S
Copy link

Add comprehensive support for popular Python templating engines (Jinja2, Mako, Mustache, Django) to ADK Community, enabling developers to use familiar templating syntax for dynamic agent instructions.

What Changed

Core Implementation:

  • Created BaseTemplateProvider abstract class with common functionality
  • Implemented 4 templating providers:
    • Jinja2InstructionProvider - Full-featured with custom filters/tests/globals
    • MakoInstructionProvider - Python-centric with inline expressions
    • MustacheInstructionProvider - Logic-less templating via chevron
    • DjangoInstructionProvider - Django template syntax with auto-config
  • All providers expose ADK session metadata (adk_session_id, adk_user_id, adk_app_name)
  • Added templating optional dependency group to pyproject.toml

Testing:

  • Created 51 comprehensive unit tests across 4 test suites
  • Tests cover: basic rendering, nested access, control structures, filters, custom extensions, error handling, and import errors
  • All tests passing (79/79 total including existing tests)

Examples:

  • Created 4 working sample agents demonstrating real-world use cases:
    • jinja2_devops_bot - Server monitoring with system metrics
    • mako_data_analyst - Sales analysis with Python expressions
    • mustache_customer_support - Customer support with logic-less templates
    • django_content_moderator - Content moderation with Django filters
  • Updated README.md with feature highlight, installation guide, and comparison table
  • Added comprehensive docstrings to all classes and methods

Resolves: #6

Add comprehensive support for popular Python templating engines (Jinja2, Mako,
Mustache, Django) to ADK Community, enabling developers to use familiar
templating syntax for dynamic agent instructions.
## What Changed

Core Implementation:

- Created BaseTemplateProvider abstract class with common functionality
- Implemented 4 templating providers:
  * Jinja2InstructionProvider - Full-featured with custom filters/tests/globals
  * MakoInstructionProvider - Python-centric with inline expressions
  * MustacheInstructionProvider - Logic-less templating via chevron
  * DjangoInstructionProvider - Django template syntax with auto-config
- All providers expose ADK session metadata (adk_session_id, adk_user_id, adk_app_name)
- Added templating optional dependency group to pyproject.toml

Testing:
- Created 51 comprehensive unit tests across 4 test suites
- Tests cover: basic rendering, nested access, control structures, filters,
  custom extensions, error handling, and import errors
- All tests passing (79/79 total including existing tests)

Examples:
- Created 4 working sample agents demonstrating real-world use cases:
  * jinja2_devops_bot - Server monitoring with system metrics
  * mako_data_analyst - Sales analysis with Python expressions
  * mustache_customer_support - Customer support with logic-less templates
  * django_content_moderator - Content moderation with Django filters
- Updated README.md with feature highlight, installation guide, and comparison table
- Added comprehensive docstrings to all classes and methods

Resolves: google#6
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Jainish-S, 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 significantly enhances the ADK Community framework by integrating robust templating capabilities for agent instructions. This allows developers to create highly dynamic and context-aware agent behaviors using popular Python templating engines, making agent instruction generation more flexible and powerful.

Highlights

  • Comprehensive Templating Support: Introduced robust templating capabilities for InstructionProvider in ADK Community, enabling dynamic agent instructions using popular Python templating engines.
  • Multiple Templating Providers: Implemented four distinct templating providers: Jinja2InstructionProvider, MakoInstructionProvider, MustacheInstructionProvider (using the Chevron library), and DjangoInstructionProvider.
  • ADK Session Metadata Exposure: Ensured all templating providers automatically expose ADK session metadata (adk_session_id, adk_user_id, adk_app_name) to templates for context-aware instructions.
  • New Optional Dependency Group: Added a new templating optional dependency group to pyproject.toml to manage the required libraries for each templating engine.
  • Extensive Unit Testing: Developed 51 new comprehensive unit tests across four test suites, covering basic rendering, nested access, control structures, filters, custom extensions, and error handling for all providers.
  • Practical Sample Agents: Created four working sample agents (Jinja2 DevOps bot, Mako data analyst, Mustache customer support, Django content moderator) to demonstrate real-world use cases for each templating engine.
  • Documentation and Code Quality: Updated README.md with feature highlights, installation guide, and a comparison table, and added comprehensive docstrings to all new classes and methods.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This is a fantastic contribution that adds comprehensive templating support to ADK Community. The implementation across four different templating engines (Jinja2, Mako, Mustache, Django) is well-structured, with a clean base class and individual provider implementations. The inclusion of extensive unit tests and sample agents for each provider is excellent and greatly improves the usability and reliability of this new feature.

I've found a few minor areas for improvement, mostly related to a redundant filter in a sample template, a suggestion to improve documentation for the Django provider's side-effects, and a correction to a test case for the Mustache provider. Overall, this is a high-quality pull request.

TODAY'S MODERATION STATS
========================
Total Reviewed: {{ moderation_stats.total_reviewed_today }}
Approved: {{ moderation_stats.approved }} ({{ moderation_stats.approved|add:0|floatformat:0 }})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Django template filter add:0|floatformat:0 applied to moderation_stats.approved is redundant. Since moderation_stats.approved is already an integer, add:0 has no effect, and floatformat:0 will simply convert it to a string, which would happen anyway when rendering {{ moderation_stats.approved }}. This makes the template slightly more complex than necessary and can be simplified.

Suggested change
Approved: {{ moderation_stats.approved }} ({{ moderation_stats.approved|add:0|floatformat:0 }})
Approved: {{ moderation_stats.approved }} ({{ moderation_stats.approved }})

Comment on lines +75 to +86
"""Initialize the Django instruction provider.

Args:
template: The Django template string.
autoescape: If True, enables HTML autoescaping. Default is False
since instructions are typically plain text.
custom_filters: Optional dictionary of custom template filters.
custom_tags: Optional dictionary of custom template tags.

Raises:
ImportError: If Django is not installed.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The provider configures global Django settings via conf.settings.configure() if they haven't been configured already. This is a side-effect that could potentially affect other parts of an application if it also uses Django. It would be beneficial to document this behavior in the __init__ docstring to make users aware of it.

    """Initialize the Django instruction provider.

    Args:
        template: The Django template string.
        autoescape: If True, enables HTML autoescaping. Default is False
            since instructions are typically plain text.
        custom_filters: Optional dictionary of custom template filters.
        custom_tags: Optional dictionary of custom template tags.

    Raises:
        ImportError: If Django is not installed.

    Note:
        If Django settings are not already configured, this provider will
        configure a minimal set of settings for standalone use.
    """

Comment on lines +173 to +183
template = """Environment: {{environment}}
{{#servers}}
Active Servers:
{{#servers}}
- [{{id}}] {{role}}: CPU {{cpu}}%{{#critical}} (CRITICAL!){{/critical}}
{{/servers}}
{{/servers}}
{{^servers}}
No servers active.
{{/servers}}
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Mustache template in this test has flawed logic. The outer {{#servers}} section iterates over the servers list, setting the context to each server item for each iteration. The inner {{#servers}} section then incorrectly tries to find a servers key within the individual server item, which doesn't exist. Consequently, the inner block is never rendered, and the test assertions for srv-01, srv-02, and CRITICAL! should fail. The test is likely passing by mistake or due to an issue with the test runner setup.

The template should be corrected to first display a header if the list is not empty, and then iterate over the list to render the items.

Suggested change
template = """Environment: {{environment}}
{{#servers}}
Active Servers:
{{#servers}}
- [{{id}}] {{role}}: CPU {{cpu}}%{{#critical}} (CRITICAL!){{/critical}}
{{/servers}}
{{/servers}}
{{^servers}}
No servers active.
{{/servers}}
"""
template = """Environment: {{environment}}
{{#servers}}
Active Servers:
{{/servers}}
{{#servers}}
- [{{id}}] {{role}}: CPU {{cpu}}%{{#critical}} (CRITICAL!){{/critical}}
{{/servers}}
{{^servers}}
No servers active.
{{/servers}}
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide Out-of-the-Box Integration for a Popular Templating Framework

1 participant