LangChain Google Integrations

repository·main·Indexed 18 days ago

https://github.com/langchain-ai/langchain-google

A centralized repository for LangChain integrations with Google services, including specialized packages for Gemini API, Vertex AI, and langchain-google-community. It features tools like the BigQueryCallbackHandler for shipping LangChain and LangGraph telemetry to BigQuery, along with a corresponding LangGraph Agent Analytics Dashboard for real-time monitoring of agent metrics, latency, and errors.

Tokens
44.3K
Snippets
127
Records
188
Agent score
64%

What's inside langchain-google

  1. Identify the correct LangChain Google package

    main

    The langchain-google repository is split into three distinct packages depending on which Google service you intend to use:

    • langchain-google-genai: Use this for integrations with Google Generative AI (Gemini API) models via ai.google.dev.
    • langchain-google-vertexai: Use this for enterprise-grade integrations with Generative AI on Vertex AI via Google Cloud.
    • langchain-google-community: Use this for other Google product integrations that do not fall under the Gemini or Vertex AI categories.
  2. How sub-agent attribution works

    main

    In multi-agent LangGraph deployments, the handler automatically assigns the agent column in BigQuery using the following priority order:

    1. metadata["agent"]: Explicit user-supplied value.
    2. metadata["langgraph_node"]: The active LangGraph node name (automatically promotes the node to the agent column).
    3. metadata["checkpoint_ns"]: The LangGraph checkpoint namespace.
    4. handler.graph_name: Fallback for top-level INVOCATION_* events.

    This ensures that telemetry from different sub-agents (e.g., a supervisor calling TheCritic) is correctly attributed without manual configuration.

  3. Setup BigQuery Callback Handler

    main

    To use the BigQueryCallbackHandler for shipping LangChain/LangGraph telemetry to BigQuery, follow these setup steps:

    1. Authenticate with Google Cloud:

      gcloud auth application-default login
      gcloud config set project YOUR_PROJECT_ID
    2. Create a BigQuery dataset (the handler will automatically manage tables and views within this dataset):

      bq mk --dataset YOUR_PROJECT_ID:agent_analytics
    3. Install required dependencies:

      pip install langchain-google-community langgraph langchain-google-genai
    gcloud auth application-default login
    gcloud config set project YOUR_PROJECT_ID
    bq mk --dataset YOUR_PROJECT_ID:agent_analytics
    pip install langchain-google-community langgraph langchain-google-genai
  4. Customize the Dashboard with new charts and events

    main

    You can extend the dashboard by adding new charts, changing the refresh rate, or defining new event types.

    Adding New Charts

    1. Add a canvas element in dashboard.html:
      <canvas id="my-chart"></canvas>
    2. Initialize the chart in JavaScript:
      const ctx = document.getElementById('my-chart').getContext('2d');
      charts.myChart = new Chart(ctx, { /* config */ });
    3. Create an API endpoint in main.py to fetch data:
      @app.get("/api/my-data")
      async def get_my_data():
          sql = f"SELECT ... FROM `{FULL_TABLE_ID}` ..."
          return run_query(sql)
    4. Add an update function in your JS to refresh the chart:
      async function updateMyChart() {
          const response = await fetch('/api/my-data');
          const data = await response.json();
          // Update chart...
      }

    Changing Refresh Rate

    Modify REFRESH_INTERVAL in the dashboard HTML (default is 5000ms):

    const REFRESH_INTERVAL = 5000; // 5 seconds

    Defining New Event Types

    Add colors for new event types in the dashboard configuration:

    const eventTypeColors = {
        'MY_CUSTOM_EVENT': '#FF5733',
        // ...
    };
    const REFRESH_INTERVAL = 5000;
    
    const eventTypeColors = {
        'MY_CUSTOM_EVENT': '#FF5733',
    };
  5. Set up the LangGraph Agent Analytics Dashboard

    main

    The LangGraph Agent Analytics Dashboard is a real-time monitoring platform for LangGraph agents that uses BigQuery as a backend. To use it, you must authenticate with Google Cloud, set your project, and ensure BigQuery data is available.

    1. Google Cloud Authentication

    Authenticate your local environment using Application Default Credentials:

    gcloud auth application-default login

    Set your active GCP project:

    gcloud config set project YOUR_PROJECT_ID

    2. Populate Sample Data

    If you are using the example setup, run the population script from the bigquery_callback directory to generate initial data:

    python populate_sample_data.py

    3. Installation

    Navigate to the webapp directory and install dependencies using pip or uv:

    cd webapp
    pip install -r requirements.txt
    # OR
    uv pip install -r requirements.txt
    gcloud auth application-default login
    gcloud config set project YOUR_PROJECT_ID
    python populate_sample_data.py
    
    cd webapp
    pip install -r requirements.txt
  6. Querying analytics via auto-created views

    main

    When create_views=True (default), the handler creates CREATE OR REPLACE VIEW for each event type. These views unnest JSON columns into typed top-level columns, making SQL queries much simpler.

    Example: Querying token usage from the v_llm_response view:

    SELECT
      agent,
      SUM(usage_total_tokens) AS total_tokens,
      SUM(usage_prompt_tokens) AS prompt_tokens,
      SUM(usage_completion_tokens) AS completion_tokens,
      SAFE_DIVIDE(
        SUM(usage_cached_tokens),
        SUM(usage_prompt_tokens)
      ) AS context_cache_hit_rate
    FROM `PROJECT.DATASET.v_llm_response`
    WHERE DATE(timestamp) = CURRENT_DATE()
    GROUP BY agent
    ORDER BY total_tokens DESC;

    Default view names follow the pattern v_<event_type> (e.g., v_llm_response, v_tool_completed). You can change this using the view_prefix configuration option.

    SELECT
      agent,
      SUM(usage_total_tokens) AS total_tokens,
      SUM(usage_prompt_tokens) AS prompt_tokens,
      SUM(usage_completion_tokens) AS completion_tokens,
      SAFE_DIVIDE(
        SUM(usage_cached_tokens),
        SUM(usage_prompt_tokens)
      ) AS context_cache_hit_rate
    FROM `PROJECT.DATASET.v_llm_response`
    WHERE DATE(timestamp) = CURRENT_DATE()
    GROUP BY agent
    ORDER BY total_tokens DESC;
  7. Install Document AI Warehouse dependencies

    main

    To use the DocumentAIWarehouseRetriever, you must install the docai dependency group for langchain-google-community.

    pip install langchain-google-community[docai]
  8. Deploy the Dashboard to Cloud Run

    main

    To deploy the dashboard to Google Cloud Run, use the following command. This assumes you have a Dockerfile in your directory.

    gcloud run deploy langgraph-analytics \
      --source . \
      --region us-central1 \
      --allow-unauthenticated

    Production Environment Variables

    When deploying to production, ensure you set the following environment variables:

    • GCP_PROJECT_ID
    • BQ_DATASET_ID
    • BQ_TABLE_ID
    gcloud run deploy langgraph-analytics \
      --source . \
      --region us-central1 \
      --allow-unauthenticated
  9. Use langchain-google-community integrations

    main

    The langchain_google_community package provides a wide range of integrations for Google services, including data loaders, vector stores, toolkits, and specialized parsers. You can import these directly from the top-level package or their specific submodules.

    Key integration categories include:

    • Data Loaders: BigQueryLoader, GoogleDriveLoader, GCSFileLoader, GCSDirectoryLoader, GMailLoader, SpeechToTextLoader, CloudVisionLoader.
    • Vector Stores: BigQueryVectorStore, VertexFSVectorStore.
    • Toolkits: CalendarToolkit, GmailToolkit, SheetsToolkit, TasksToolkit.
    • Search & Retrieval: GoogleSearchAPIWrapper, VertexAISearchRetriever, DocumentAIWarehouseRetriever.
    • AI Services: DocAIParser, GoogleTranslateTransformer, TextToSpeechTool, VertexAIRank.