Skip to content

Instantly share code, notes, and snippets.

@ducnh1022
Created March 25, 2026 19:37
Show Gist options
  • Select an option

  • Save ducnh1022/9972ae70b31f81d7fa31b6c23ab634f3 to your computer and use it in GitHub Desktop.

Select an option

Save ducnh1022/9972ae70b31f81d7fa31b6c23ab634f3 to your computer and use it in GitHub Desktop.
def get_table_info(project_id, dataset_id, table_id, sample_size=5):
table_ref = f"{project_id}.{dataset_id}.{table_id}"
# Get schema
table = client.get_table(table_ref)
schema = table.schema
# Get sample data
query = f"""
SELECT *
FROM `{table_ref}`
LIMIT {sample_size}
"""
rows = client.query(query).result()
sample_rows = [dict(row.items()) for row in rows]
# Build result
result = {}
for field in schema:
col_name = field.name
col_type = field.field_type
# Extract sample values for this column
samples = [row.get(col_name) for row in sample_rows]
result[col_name] = {
"type": col_type,
"samples": samples
}
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment