DEV Community

Evan Lin for Google Developer Experts

Posted on • Originally published at evanlin.com on

GCP: Upgrading a LINE Bot with Vertex AI ADK Tools for Smart Business Cards and Backup Search

image-20260526210750701

Preface

In the previous article, we successfully upgraded the LINE business card assistant robot (linebot-namecard-python) from the AI Studio API Key verification mode to the enterprise-grade Google Cloud Vertex AI mechanism, completely freeing us from the 429 quota anxiety.

However, the original method of searching for business cards had significant limitations: We had to first fetch all the user's business cards from Firebase, package them into a huge JSON array, and then stuff them into the prompt, asking Gemini to select the most relevant business card object to return.

This approach has three major drawbacks:

  1. Token Waste: With many business cards, each search is a ruthless blow to the token balance.
  2. Lack of Flexibility: The model can only search passively; it cannot proactively ask for details or perform data updates.
  3. Unable to Link Operations: If the user says, "Help me change David Wang's phone number," we have to write a bunch of complex NLP judgments and branches in the Webhook.

To solve these pain points, we decided to refactor the robot and embrace Google Cloud's latest, powerful, and code-friendly Agent Development Kit (ADK)!

This article will share with you how we completely refactored Firebase access into ADK Tools, implemented dynamic closures, and the various top-tier blood and tears pitfalls we encountered during deployment on Cloud Run and with the Antigravity CLI tool!


Architecture Upgrade: Why Choose ADK and Tools?

Agent Development Kit (ADK) is a code-first agent development framework launched by Google Cloud. Previously, in order for us to allow large models to call external APIs, we had to manually write long OpenAPI schemas or complex function-calling descriptions; ADK simplifies all of this into simple Python functions!

We planned five core data operation functions for the business card Agent and registered them as Tools of the Agent in the form of Python functions:

  1. get_all_namecards(): Reads the list of all business cards (including IDs) for the current user.
  2. get_namecard_by_id(card_id): Retrieves the detailed content of a specific business card.
  3. display_namecard(card_id): The core tool! Called when the model matches a business card, used to tell the Python main program "it's time to display this business card on the screen".
  4. update_namecard_memo(card_id, memo): Updates the business card memo.
  5. update_namecard_field(card_id, field, value): Directly updates the specified fields of the business card (name, phone, email, etc.) in natural language.

Core Code Rewrite: Dynamic Closure Tools Implementation

In Webhook development, the most important thing is security. We absolutely cannot allow user A to search or modify user B's business cards.

Therefore, we cannot implement static, global Database Tools. Instead, in handle_smart_query, we dynamically create exclusive Tools for each conversation request through the closure mechanism.

This approach not only perfectly binds the user's user_id but also utilizes the found_card_ids list in the closure to perfectly collect "all business card IDs that the model wants to present to the user" during the decision-making process:

def make_adk_tools(user_id: str, found_card_ids: list):
    """Dynamically create exclusive Firebase data access and operation tools for a specific user"""
    def get_all_namecards() -> list[dict]:
        """Get the list of all business card data in the Firebase database for the current user.
        Each business card data contains a unique card_id field."""
        cards_dict = firebase_utils.get_all_cards(user_id)
        all_cards_list = []
        for card_id, card_data in cards_dict.items():
            card_data_with_id = card_data.copy()
            card_data_with_id['card_id'] = card_id
            all_cards_list.append(card_data_with_id)
        return all_cards_list

    def get_namecard_by_id(card_id: str) -> dict:
        """Get the detailed fields and data of a single business card through a specific card_id."""
        return firebase_utils.get_card_by_id(user_id, card_id)

    def display_namecard(card_id: str) -> str:
        """Display a specific business card to the user.
        When a business card matching the search is found, be sure to call this tool."""
        if card_id not in found_card_ids:
            found_card_ids.append(card_id)
        return f"已將名片 ID 標記為顯示:{card_id}"

    def update_namecard_memo(card_id: str, memo: str) -> bool:
        """Update the memo/note information of a specific business card."""
        return firebase_utils.update_namecard_memo(card_id, user_id, memo)

    def update_namecard_field(card_id: str, field: str, value: str) -> bool:
        """Update the specified field of a specific business card (optional fields: name, title, company, address, phone, email)."""
        return firebase_utils.update_namecard_field(
            user_id, card_id, field, value
        )

    return [
        get_all_namecards,
        get_namecard_by_id,
        display_namecard,
        update_namecard_memo,
        update_namecard_field
    ]

Enter fullscreen mode Exit fullscreen mode

Refactored Main Webhook Logic (handle_smart_query)

Now, when LINE receives a text query, we only need to pass the message to the ADK Runner to run once. Once the Agent decides to call display_namecard, we combine the Agent's friendly Chinese explanation (text reply) with the business card Flex Message (the entire business card) in the LINE reply:

async def handle_smart_query(event: MessageEvent, user_id: str, msg: str):
    found_card_ids = []
    tools = make_adk_tools(user_id, found_card_ids)

    # 1. Create an ADK Agent equipped with exclusive Tools
    agent = Agent(
        name="namecard_agent",
        model="gemini-3-flash-preview",
        instruction=(
            "You are a smart and friendly LINE business card assistant. Your job is to help users manage their business card data.\n"
            "You can use the appropriate tools to read or modify business card records in the Firebase database.\n\n"
            "【Core Operation Guidelines】\n"
            "1. 【Query】When a user queries for someone's or a company's business card, please first call get_all_namecards to get all the data and perform analysis and comparison in the background.\n"
            "2. 【Display】As long as a business card that meets the conditions is found, 『must』 call the display_namecard tool to mark the card_id of that business card for display, so that the system can draw and present it on the LINE screen.\n"
            "3. 【Modify】If the user wants to modify a business card (e.g., phone number, Email, memo), please first compare and find the card_id, and then call the corresponding update tool (such as update_namecard_field or update_namecard_memo) to make the modification. After the modification is successful, 『must』 call display_namecard again to display the updated business card, allowing the user to confirm.\n"
            "4. 【Reply】Finally, please reply to the user with a friendly and concise traditional Chinese tone about the operation results or search progress."
        ),
        tools=tools,
    )

    # 2. Execute the Runner with an in-memory Session
    runner = Runner(
        app_name="namecard_bot_app",
        agent=agent,
        session_service=InMemorySessionService()
    )

    try:
        events = await runner.run_debug(
            msg, user_id=user_id, session_id=user_id
        )

        # Combine the Agent's text reply
        final_text = ""
        for ev in events:
            if ev.content and ev.content.parts:
                for part in ev.content.parts:
                    if part.text:
                        final_text += part.text

        final_text = final_text.strip() or "為您完成處理。"

        reply_msgs = [TextSendMessage(
            text=final_text,
            quick_reply=get_quick_reply_items()
        )]

        # 3. Get the business cards marked for display by the Agent and convert them to Flex Messages
        if found_card_ids:
            for card_id in found_card_ids[:5]:
                card_data = firebase_utils.get_card_by_id(user_id, card_id)
                if card_data:
                    reply_msgs.append(
                        flex_messages.get_namecard_flex_msg(card_data, card_id)
                    )

        await line_bot_api.reply_message(event.reply_token, reply_msgs)

Enter fullscreen mode Exit fullscreen mode

Blood and Tears Pitfalls During the Migration Process

The refactoring process cannot be smooth sailing. In this upgrade, we encountered three top-tier deep pits, each of which almost prevented the online container from providing services. Here is valuable pit-filling experience:

Pitfall 1: Uvicorn Crashes the Event Loop at Startup

When we excitedly pushed the container containing google-adk onto Cloud Run, the deployment failed due to a health check timeout at the last moment! Checking the GCP Log, we were greeted with this heartbreaking RuntimeError:

  File "/app/app/bot_instance.py", line 7, in <module>
    session = aiohttp.ClientSession()
  File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 321, in __init__
    loop = loop or asyncio.get_running_loop()
RuntimeError: no running event loop

Enter fullscreen mode Exit fullscreen mode

Reason: Under the new dependency environment, app/bot_instance.py directly instantiated aiohttp.ClientSession() globally when it was imported (Import Time). However, at this time, Uvicorn's asyncio Event Loop had not even started! This caused aiohttp to throw an exception and crash directly because it couldn't find a running event loop.

Solution: We designed a lazy-load LazyLineBotApi wrapper, delaying the creation of ClientSession and AsyncLineBotApi until the first LINE Webhook request comes in (at this time, the Event Loop must be running), perfectly avoiding the Import Time initialization crash:

class LazyLineBotApi:
    def __init__ (self):
        self._api = None
        self.session = None

    def _get_api(self):
        if self._api is None:
            self.session = aiohttp.ClientSession()
            async_http_client = AiohttpAsyncHttpClient(self.session)
            self._api = AsyncLineBotApi(
                config.CHANNEL_ACCESS_TOKEN, async_http_client
            )
        return self._api

    def __getattr__ (self, name):
        return getattr(self._get_api(), name)

line_bot_api = LazyLineBotApi()

Enter fullscreen mode Exit fullscreen mode

Pitfall 2: GCP's Default GOOGLE_CLOUD_LOCATION and Region 404

After successfully starting the container, we tried entering text in LINE, but saw a big red error again in the background:

Error executing ADK smart query: 404 NOT_FOUND. 
Publisher Model `projects/line-vertex/locations/asia-east1/publishers/google/models/gemini-3-flash-preview` was not found.

Enter fullscreen mode Exit fullscreen mode

Reason: Because our Cloud Run service is deployed in Taiwan (asia-east1), GCP will automatically inject GOOGLE_CLOUD_LOCATION=asia-east1 into the environment variables. However, in the Vertex AI ecosystem, many of the latest and most powerful models (such as gemini-3-flash-preview) only provide services in the global region! When the underlying SDK of ADK automatically reads asia-east1 to search for models, it will naturally throw a 404.

Solution: We directly override the environment variable at the first moment in the system's configuration entry app/config.py, directing all Vertex AI model searches to the global region:

# Force GOOGLE_CLOUD_LOCATION to global so that Vertex AI and ADK look
# for models in the global region
os.environ["GOOGLE_CLOUD_LOCATION"] = "global"

Enter fullscreen mode Exit fullscreen mode

Pitfall 3: Insurance Mechanism in Extreme Situations - Local Keyword Backup Search

After the user's LINE bot goes live, any API quota explosion or network timeout should not cause the user to see a cold "server failure". To guarantee production-level SLA, we added a seamless keyword search backup mechanism (Local Keyword Fallback) in the except block of handle_smart_query.

If Vertex AI or ADK encounters any exceptions during execution, the system will automatically enable Firebase local keyword matching in the background, still perfectly returning matching business card Flex messages, providing the user with the most elegant protection net:

    except Exception as e:
        print(f"Error executing ADK smart query: {e}")
        # Backup search mechanism: When Vertex AI or ADK API is abnormal, automatically enable local keyword filtering search to ensure service continuity
        try:
            all_cards_dict = firebase_utils.get_all_cards(user_id)
            fallback_matches = []
            if all_cards_dict:
                for card_id, card_data in all_cards_dict.items():
                    name = card_data.get("name", "").lower()
                    company = card_data.get("company", "").lower()
                    query_lower = msg.lower()
                    if query_lower in name or query_lower in company:
                        fallback_matches.append((card_id, card_data))

            if fallback_matches:
                reply_msgs = [TextSendMessage(
                    text="「智慧搜尋」服務暫時無法取得,"
                         "已自動啟用「關鍵字備援搜尋」為您找到以下相關名片:",
                    quick_reply=get_quick_reply_items()
                )]
                for card_id, card_data in fallback_matches[:5]:
                    reply_msgs.append(
                        flex_messages.get_namecard_flex_msg(card_data, card_id)
                    )
                await line_bot_api.reply_message(event.reply_token, reply_msgs)
                return
        except Exception as fallback_err:
            print(f"Fallback search also failed: {fallback_err}")

Enter fullscreen mode Exit fullscreen mode

Summary and Benefits

After refactoring into an ADK Agent + Tools architecture, it brought amazing substantial changes:

  1. Extreme Token Saving: The model only calls get_all_namecards when it needs to read business cards, and general conversations no longer need to repeatedly transmit huge JSON data.
  2. Multi-step Natural Dialogue Linking: The user only needs to type "Help me change David Wang's memo to 'Meeting next Monday'", and the model will automatically and continuously call get_all_namecards() -> find the ID -> call update_namecard_memo(id, ...) -> and then call display_namecard(id) to show the latest results.
  3. Code Quality Leap: In this refactoring, we also strictly controlled through flake8, completing 100% clean code formatting and zero-warning compilation.

The complete and linter-optimized code has been pushed to GitHub simultaneously. I hope this dynamic closure design and Cloud Run, Event Loop pit-filling practice can help everyone avoid more detours when building production-level AI Agent Web applications! See you next time!

Top comments (0)