Introduction to Google Agent Development Kit In Python

Google’s Agent Development Kit (ADK) is a powerful framework that makes it easy to create, evaluate, and deploy AI agents. This tutorial will walk you through using the ADK to build text analysis agents with different architectures, based on the provided code example.

Getting Started with ADK

Prerequisites

Before diving into the code, you’ll need to set up your environment:

  1. Install ADK using pip in a virtual environment:
   python -m venv venv
   source venv/bin/activate  # On Windows: venv\Scripts\activate
   pip install google-adk
  1. Get a Google API key:
  • Visit AI Studio at aistudio.google.com
  • Click “Get API Key” in the top right corner
  • Create a new key and store it securely in your environment variables[3][5]
  1. Install required dependencies:
   pip install textblob groq

Understanding Agent Types in ADK

The ADK provides several agent types that can be used for different purposes:

1. Basic Agent

The simplest form of agent that handles a single task. In your code, you’ve implemented a basic text analysis agent (currently commented out):

root_agent = Agent(
    name="text_analysis_agent",
    model=MODEL,
    description="Agent to analyse a given text",
    instruction="You are a text analyst agent with the ability to analyse a given text and also to provide the sentiment of the text",
    tools=[get_sentiment],
)

This agent uses the get_sentiment function as a tool to analyze text sentiment:

def get_sentiment(text:str):
    sentiment = TextBlob(text).sentiment
    return sentiment.polarity

2. LLM Agent

The LlmAgent is specialized for working with language models. Your code defines several LLM agents:

writer_agent = LlmAgent(
    name="WriterAgent",
    model=MODEL,
    instruction="You are a Writer Agent. Write a comprehensive article about the given topic and also use information from the search results from the internet",
    description="Write Comprehensive Article",
    output_key="written_results"
)

The output_key parameter is important as it defines how this agent’s output will be referred to by other agents in a pipeline.

3. Sequential Agent

A SequentialAgent runs multiple sub-agents in sequence, with each agent’s output potentially feeding into the next:

root_agent = SequentialAgent(
    name="ResearchWriterPipelineAgent",
    sub_agents=[writer_agent, summarizer_agent, translator_agent]
)

In this example, the writer agent would run first, followed by the summarizer, and finally the translator. The output from one agent flows into the next.

4. Parallel Agent

The ParallelAgent runs multiple agents concurrently, which is ideal for independent tasks:

root_agent = ParallelAgent(
    name="ParallelTextAnalysisAgent",
    sub_agents=[sentiment_agent, keyword_agent, summarizer_agent],
    description="Runs multiple specialist agents in parallel to analyse a text" 
)

This approach is faster when tasks don’t depend on each other’s outputs.

Building a Text Analysis System

Let’s implement a complete text analysis system using the ADK. We’ll use the active Parallel Agent implementation from your code:

Step 1: Define Specialized Agents

Each agent handles a specific aspect of text analysis:

sentiment_agent = LlmAgent(
    name="SentimentAnalyzer",
    model=MODEL,
    description="Provide the sentiment of the text",
    instruction="Analyze the sentiment of the provided text.",
    output_key="sentiment"
)

keyword_agent = LlmAgent(
    name="KeywordExtractor",
    model=MODEL,
    description="Extract the relevant keywords from the text",
    instruction="Extract main keywords from the provided text.",
    output_key="keywords"
)

summarizer_agent = LlmAgent(
    name="Summarizer",
    model=MODEL,
    description="Summarize the given text",
    instruction="Create a concise summary of the text.",
    output_key="summary"
)

Step 2: Combine Agents in Parallel

The root agent orchestrates the parallel execution:

root_agent = ParallelAgent(
    name="ParallelTextAnalysisAgent",
    sub_agents=[sentiment_agent, keyword_agent, summarizer_agent],
    description="Runs multiple specialist agents in parallel to analyse a text" 
)

Step 3: Run the Agent

To run the agent with input:

result = root_agent.run("Your text to analyze goes here.")
print(result)

# Access individual outputs
print("Sentiment:", result.sentiment)
print("Keywords:", result.keywords)
print("Summary:", result.summary)

Practical Example: Running a Complex Analysis Pipeline

Let’s expand our previous example by implementing a sequential pipeline that first writes an article, then summarizes it, and finally translates it—before merging it all into a final result.

1. Define the Pipeline Agents

Here’s how you define the agents for this pipeline:

from google.adk.agents import SequentialAgent, LlmAgent
from google.adk.models.lite_llm import LiteLlm

MODEL = LiteLlm(model="groq/llama3-8b-8192")

writer_agent = LlmAgent(
    name="WriterAgent",
    model=MODEL,
    instruction="You are a Writer Agent. Write a comprehensive article about the given topic.",
    description="Write Comprehensive Article",
    output_key="written_results"
)

summarizer_agent = LlmAgent(
    model=MODEL,
    name="SummarizeContent",
    instruction="Summarize the text found in 'written_results'",
    output_key="summary"
)

translator_agent = LlmAgent(
    name="TranslatorAgent",
    model=MODEL,
    instruction="You are an expert Translator Agent. Convert the summary to both German and French.",
    description="Translate Summary",
    output_key="translation"
)

# Build the sequential pipeline
pipeline_agent = SequentialAgent(
    name="ResearchWriterPipelineAgent",
    sub_agents=[writer_agent, summarizer_agent, translator_agent]
)

2. Running the Pipeline

To execute the pipeline:

result = pipeline_agent.run("Artificial intelligence in healthcare")
print("Article:", result.written_results)
print("Summary:", result.summary)
print("Translation:", result.translation)

This will execute the agents in sequence: writer → summarizer → translator.


3. Running Parallel and Sequential Agents Together

You can combine parallel and sequential agents for more complex workflows.

Suppose you want to:

  • Write an article
  • Summarize and translate it in parallel
  • Analyze the original article for keywords and sentiment in parallel

You would need to structure the agents accordingly and use SequentialAgent and ParallelAgent together.

Design Note:
Currently, the ADK does not natively allow nested structured pipelines (e.g., parallel inside sequential), but you can simulate this by running multiple steps and passing results manually or using a custom orchestrator.

However, you can use the tools in your code to build specific pipelines as shown above.


4. Best Practices for Agent Development

  • Modular Design:
    Each agent should handle a single, well-defined task.
  • Descriptive Names and Instructions:
    Use clear names, descriptions, and instructions to ensure clarity.
  • Output Keys:
    Utilize output_key to make results easy to reference and use in pipelines.
  • Error Handling:
    Implement error handling around agent runs to catch and log unexpected issues.
  • Logging:
    Log intermediate results and outputs for debugging and traceability.
  • Testing:
    Test each agent individually before combining them into pipelines.

5. Integrating Tools and Functions

You can integrate custom tools (like get_sentiment from textblob) as shown in your initial code:

def get_sentiment(text: str):
    return TextBlob(text).sentiment.polarity

agent = Agent(
    name="text_analysis_agent",
    model=MODEL,
    tools=[get_sentiment],
    instruction="You are a text analyst agent. Analyze the text and provide its sentiment.",
    description="Agent to analyze a given text"
)

When you run this agent, the get_sentiment tool will be available for use by the agent.


6. Handling Outputs and Chaining Results

You might want to chain outputs in more complex ways, for example:

from google.adk.utils import merge_outputs

# Suppose you have run multiple agents and collected results:
parallel_result = root_agent.run("Some text")
pipeline_result = pipeline_agent.run("Some topic")

# Combine results as needed:
combined = {
    "analysis": parallel_result,
    "article_and_translation": pipeline_result
}

7. Debugging and Troubleshooting

  • Check Agent Outputs:
    Print intermediate outputs to see what each step is producing.
  • Model Errors:
    If your LLM model does not respond or gives unexpected results, check your API key and model connection.
  • Tool Integration:
    Ensure your custom tools are properly defined and imported.
  • Sequencing:
    Make sure agents in SequentialAgent are in the correct order.

8. Summary Table: Agent Types and Use Cases

Agent TypeDescriptionUse Case Example
AgentBasic agent with custom toolsSimple text analysis
LlmAgentAgent specialized for LLMsWriting, summarizing, translating
SequentialAgentRuns agents in seriesWrite → Summarize → Translate
ParallelAgentRuns agents in parallelSentiment + Keywords + Summary

9. Conclusion

Google’s Agent Development Kit (ADK) makes it easy to create, manage, and orchestrate sophisticated AI agent workflows. By combining different agent types and integrating custom tools, you can build modular, robust systems for tasks like text analysis, summarization, translation, and more.

If you want to explore further, consider experimenting with more complex agent combinations, integrating external data sources, or connecting agents to APIs for live data retrieval.


Leave a Comment

Your email address will not be published. Required fields are marked *