feat: add AG2 multi-agent framework template#917
feat: add AG2 multi-agent framework template#917faridun-ag2 wants to merge 3 commits intoGoogleCloudPlatform:mainfrom
Conversation
Add AG2 (formerly AutoGen) as a new agent template with cloud_run and none deployment targets. AG2 is an open-source multi-agent framework with 500K+ monthly PyPI downloads. New template includes: - app/agent.py with tool use via AG2's decorator pattern - Vertex AI (Gemini) by default, Google API key fallback - FastAPI serving layer with /query and /health endpoints - Getting started notebook with GroupChat example - Integration tests for tool and agent execution Jinja integration touches: - fast_api_app.py — AG2 branch with simple query endpoint - typing.py, telemetry.py — AG2 branches without LangChain deps - test_server_e2e.py, load_test.py — AG2 test/load patterns - Makefile — playground for none deployment
|
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. |
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a new multi-agent template powered by the AG2 framework, providing a core agent implementation with tool support, a tutorial notebook, and deployment scaffolding for Cloud Run and GKE. The reviewer identified several critical bugs in app/agent.py and the tutorial notebook, specifically the use of non-existent process() and messages attributes on the ChatResult object, which will cause runtime errors. Additionally, typos in the Gemini model names were noted, and a suggestion was made to include the gemini extra in the notebook's dependency installation.
| response = user_proxy.run(assistant, message=message) | ||
| response.process() |
| if response.summary: | ||
| return response.summary.replace("TERMINATE", "").strip() | ||
|
|
||
| for msg in reversed(response.messages): |
|
|
||
| llm_config = LLMConfig( | ||
| { | ||
| "model": "gemini-2.5-flash", |
|
|
||
| llm_config = LLMConfig( | ||
| { | ||
| "model": "gemini-2.5-flash", |
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%pip install \"ag2[openai]>=0.11.4,<1.0\" python-dotenv -q" |
There was a problem hiding this comment.
Since the template defaults to using Gemini (Vertex AI), the gemini extra should be included in the installation command to ensure all necessary dependencies are present.
| "%pip install \"ag2[openai]>=0.11.4,<1.0\" python-dotenv -q" | |
| "%pip install \"ag2[openai,gemini]>=0.11.4,<1.0\" python-dotenv -q" |
| "def square(n: Annotated[int, \"The number to square\"]) -> int:\n", | ||
| " return n * n\n", | ||
| "\n", | ||
| "result = user_proxy.run(assistant, message=\"What is 42 squared?\").process()" |
There was a problem hiding this comment.
| "result = coordinator.run(\n", | ||
| " manager,\n", | ||
| " message=\"Write a brief overview of how AI is used in weather forecasting.\",\n", | ||
| ").process()" |
- Remove unnecessary process() call from run_agent() — result is already available on the RunResponse object - Add gemini extra to notebook pip install command - Remove .process() calls from notebook code cells
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a new multi-agent template based on the AG2 framework, providing a complete implementation with tool use, FastAPI endpoints for deployment on Cloud Run and GKE, and comprehensive integration tests. The review feedback identifies critical issues in agent.py regarding the incorrect instantiation of LLMConfig and the use of an invalid model name. Furthermore, it suggests improving code quality by replacing file-wide linting suppressions with targeted ones and ensuring consistent use of the module-level logger in the FastAPI application files.
| llm_config = LLMConfig( | ||
| { | ||
| "model": "gemini-2.5-flash", | ||
| "api_type": "google", | ||
| "project_id": os.environ["GOOGLE_CLOUD_PROJECT"], | ||
| "location": os.environ["GOOGLE_CLOUD_LOCATION"], | ||
| } | ||
| ) |
There was a problem hiding this comment.
The LLMConfig instantiation is incorrect and uses an invalid model name. In ag2, LLMConfig expects a config_list (a list of dictionaries) rather than a single dictionary. Additionally, gemini-2.5-flash is not a valid model name; it should be gemini-2.0-flash or gemini-1.5-flash.
llm_config = LLMConfig(
config_list=[
{
"model": "gemini-2.0-flash",
"api_type": "google",
"project_id": os.environ["GOOGLE_CLOUD_PROJECT"],
"location": os.environ["GOOGLE_CLOUD_LOCATION"],
}
]
)| llm_config = LLMConfig( | ||
| { | ||
| "model": "gemini-2.5-flash", | ||
| "api_key": os.environ.get("GOOGLE_API_KEY", ""), | ||
| "api_type": "google", | ||
| } | ||
| ) |
There was a problem hiding this comment.
| @@ -0,0 +1,119 @@ | |||
| # ruff: noqa | |||
There was a problem hiding this comment.
Using # ruff: noqa to disable all linting for the entire file is discouraged. It is better to address specific linting issues or use targeted suppressions if Jinja syntax causes false positives during template linting. This ensures the template follows the project's code quality standards.
References
- The project uses ruff for Python linting to maintain code quality. (link)
| @app.post("/query", response_model=QueryResponse) | ||
| def query(request: QueryRequest) -> QueryResponse: | ||
| """Run the AG2 agent with the given message.""" | ||
| logging.info(f"Received query: {request.message}") |
There was a problem hiding this comment.
Use the module-level logger instead of the global logging module for consistency and to ensure logs are correctly handled by the configured logging system.
logger.info(f"Received query: {request.message}")References
- Follow established patterns and replicate existing conventions for naming and logic. (link)
| @app.post("/query", response_model=QueryResponse) | ||
| def query(request: QueryRequest) -> QueryResponse: | ||
| """Run the AG2 agent with the given message.""" | ||
| logging.info(f"Received query: {request.message}") |
There was a problem hiding this comment.
Use the module-level logger instead of the global logging module for consistency.
logger.info(f"Received query: {request.message}")References
- Follow established patterns and replicate existing conventions for naming and logic. (link)
- Use module-level logger instead of logging.info in fast_api_app.py (cloud_run and gke) - Fix ty type-check error: wrap response.messages in list() for reversed() compatibility Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a new multi-agent template based on the AG2 framework, including core implementation, integration tests, and deployment configurations for Cloud Run and GKE. The review feedback correctly identifies typos in the Gemini model versions and a potential AttributeError in the response processing logic. Furthermore, the reviewer suggests improving telemetry and unifying logging practices to maintain consistency with existing templates and established repository patterns.
|
|
||
| llm_config = LLMConfig( | ||
| { | ||
| "model": "gemini-2.5-flash", |
|
|
||
| llm_config = LLMConfig( | ||
| { | ||
| "model": "gemini-2.5-flash", |
| if response.summary: | ||
| return response.summary.replace("TERMINATE", "").strip() | ||
|
|
||
| for msg in reversed(list(response.messages)): |
There was a problem hiding this comment.
| def setup_telemetry() -> str | None: | ||
| """Configure telemetry for AG2 agents.""" | ||
| bucket = os.environ.get("LOGS_BUCKET_NAME") | ||
| if bucket: | ||
| logger.info("Telemetry bucket configured: %s", bucket) | ||
| else: | ||
| logger.info("No telemetry bucket configured") | ||
| return bucket |
There was a problem hiding this comment.
The setup_telemetry implementation for AG2 is very minimal compared to other agent templates (like LangGraph or ADK), which include OpenTelemetry tracing and Cloud Logging integration. To maintain feature parity and provide a better production-ready experience, consider implementing a more robust telemetry setup for AG2 agents.
| {%- if cookiecutter.get("agent_name") == "ag2" %} | ||
| logger.info("Feedback received: %s", feedback.model_dump()) | ||
| {%- else %} | ||
| logger.log_struct(feedback.model_dump(), severity="INFO") | ||
| {%- endif %} |
There was a problem hiding this comment.
The logging approach for ag2 is inconsistent with other agents in this file. While other agents use logger.log_struct for structured logging in Google Cloud, ag2 uses standard logger.info. It is recommended to unify the logger initialization (using google.cloud.logging) and usage across all agent types to ensure consistent log formatting and better observability in Cloud Logging.
| {%- if cookiecutter.get("agent_name") == "ag2" %} | ||
| logger.info("Feedback received: %s", feedback.model_dump()) | ||
| {%- else %} | ||
| logger.log_struct(feedback.model_dump(), severity="INFO") | ||
| {%- endif %} |
There was a problem hiding this comment.
The logging approach for ag2 is inconsistent with other agents in this file. While other agents use logger.log_struct for structured logging in Google Cloud, ag2 uses standard logger.info. It is recommended to unify the logger initialization (using google.cloud.logging) and usage across all agent types to ensure consistent log formatting and better observability in Cloud Logging.
|
Hello @eliasecchig @elia-secchi! Note on Gemini Code Assist review commentsThe automated Gemini reviewer flagged several issues that are incorrect due to outdated knowledge of the AG2 and Gemini APIs. Wanted to call these out to save you time: 1. 2. 3. Feedback that was addressed
Intentional design decisions
|
Summary
cloud_runandnonedeployment targetsProblem
The starter pack supports ADK and LangGraph but has no multi-agent framework template. AG2 is one of the most popular multi-agent frameworks (500K+ monthly PyPI downloads, 4,300+ GitHub stars) and fills this gap.
Solution
New
ag2agent template following the existing template structure:app/agent.py— Core agent with tool use via AG2's decorator patternnotebooks/getting_started.ipynb— Basic usage, custom tools, and GroupChattests/integration/test_agent.py— Tool tests and agent integration tests.template/templateconfig.yaml— Template config for cloud_run and none targetsJinja integration in existing templates:
fast_api_app.py— AG2 branch with/queryand/healthendpointstyping.py,telemetry.py— AG2 branches without LangChain dependenciestest_server_e2e.py,load_test.py— AG2-specific test and load patternsMakefile— AG2 playground fornonedeployment targetTesting
make lintpassesmake testpasses (495 passed, 3 skipped)/queryreturns correct tool-use response