Intro to Request and Response Logging with Gemini
Source notebook
Repo path:
gemini/logging/intro_request_response_logging.ipynb· Open on GitHub · intermediate
Shows how to enable Gemini request-response logging to BigQuery, query logs, and disable logging.
Summary
This notebook teaches how to configure Vertex AI request-response logging for a Gemini model using the preview Vertex AI SDK. The workflow initializes Vertex AI, enables logging with automatic BigQuery resource creation, sends a generate_content request, waits for logs, discovers the output_uri, queries recent log rows, and disables logging.
Key code patterns
Initialize Vertex AI
import vertexai
vertexai.init(project=PROJECT_ID, location=LOCATION)
model = GenerativeModel("gemini-2.5-flash")Sets the Google Cloud project and region before creating the Gemini model client.
Enable logging
logging_config_result = model.set_request_response_logging_config(
enabled=True,
sampling_rate=1.0,
bigquery_destination=f"bq://{PROJECT_ID}",
)
output_uri = logging_config_result.logging_config.bigquery_destination.output_uriTurns on request-response logging and lets Vertex AI auto-create the BigQuery dataset and table.
Generate a logged request
response = model.generate_content("Why is the sky blue?")
print(response.text)
time.sleep(120)A model call is needed to trigger BigQuery resource creation and produce a log entry.
Recover output URI
config = model.set_request_response_logging_config(
enabled=True,
sampling_rate=0.99,
bigquery_destination=f"bq://{PROJECT_ID}",
)
output_uri = config.logging_config.bigquery_destination.output_uriUses a temporary config update to retrieve the auto-created BigQuery destination.
Query logs
uri_parts = output_uri.replace("bq://", "").split(".")
project_id, dataset_id, table_id = uri_parts
full_table_id = f"`{project_id}.{dataset_id}.{table_id}`"
df = bigquery.Client(project=project_id).query(query).to_dataframe()Parses the BigQuery URI and reads recent logging records into a DataFrame.
Disable logging
model.set_request_response_logging_config(
enabled=False,
sampling_rate=1.0,
bigquery_destination=f"bq://{PROJECT_ID}",
)Uses the same configuration method to turn request-response logging off.
Models & APIs used
- Models: gemini-2.5-flash
- APIs / services: Vertex AI, BigQuery
- SDKs / libraries:
google-cloud-aiplatform,google-cloud-bigquery,vertexai
When to use this
Use this pattern when you need to audit, debug, or analyze sampled Gemini request and response payloads in BigQuery.
Gotchas & caveats
- The logging feature is preview-only and uses vertexai.preview.generative_models.GenerativeModel.
- The notebook requires a Google Cloud project ID and Vertex AI initialization in us-central1.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- If logging is already enabled, set_request_response_logging_config can raise AlreadyExists.
- BigQuery dataset, table, and log propagation can take a few minutes.
- Automatically created datasets follow an ENDPOINT_DISPLAY_NAME_ENDPOINT_ID naming pattern.
- Logged request-response pairs larger than the 10MB BigQuery write API row limit are not recorded.
Best practices
- Use sampling_rate to control the fraction of requests logged.
- Use an automatically created BigQuery destination by passing bq://PROJECT_ID.
- Handle AlreadyExists when enabling logging.
- Restore the original sampling_rate after temporarily updating the config to retrieve output_uri.
- Quote the full BigQuery table ID before querying.
- Disable request-response logging when it is no longer needed.
Related
- Concepts: Getting Started
- Entities: Vertex AI · Vertex AI SDK · BigQuery · Gemini
- Area: Gemini Notebooks
- Best practices: Getting Started - Best Practices