:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import dataclasses
from collections.abc import Callable
from datetime import UTC, date, datetime
from typing import Any, Optional
from urllib.parse import urlsplit, urlunsplit
from requests import PreparedRequest, Response
from products.warehouse_sources.sources.deepgram.settings import DEEPGRAM_ENDPOINTS, DeepgramEndpointConfig
from products.warehouse_sources.sources.sdk import (
AuthConfigBase,
Endpoint,
EndpointResource,
IncrementalConfig,
IncrementalFieldType,
PageNumberPaginator,
Resource,
RESTAPIConfig,
ResumableSourceManager,
SinglePagePaginator,
SourceResponse,
make_parent_key_name,
make_tracked_session,
rest_api_resources,
validate_via_probe,
)
DEEPGRAM_BASE_URL = "https://api.deepgram.com/v1"
# The requests log caps `limit` at 1000; use the max to minimise round trips.
REQUESTS_PAGE_SIZE = 1000
# Parent (projects) list is the fan-out seed for every project-scoped endpoint.
_PARENT_RESOURCE = "projects"
_PARENT_ID_FIELD = "project_id"
# The framework injects the parent id under this `_<parent>_<field>` name; the child map lifts it back
# to the flat `project_id` column the tables have always exposed.
_PARENT_ID_KEY = make_parent_key_name(_PARENT_RESOURCE, _PARENT_ID_FIELD)
class DeepgramTokenAuth(AuthConfigBase):
"""Deepgram's Management API expects `Authorization: Token <key>` (not Bearer).
Supplying it through a framework auth object (rather than a hand-built header) registers the key
for value-based log redaction, so a failed or sampled request never persists the customer's secret.
"""
def __init__(self, api_key: str) -> None:
self.api_key = api_key
def __call__(self, request: PreparedRequest) -> PreparedRequest:
request.headers["Authorization"] = f"Token {self.api_key}"
return request
def secret_values(self) -> tuple[str, ...]:
return (self.api_key,) if self.api_key else ()
class DeepgramRequestsPaginator(PageNumberPaginator):
"""Page/`limit` pagination over Deepgram's requests log (0-indexed `page`).
The requests log reports no total count, so — like the hand-rolled loop this replaces — a page
shorter than the requested `limit` (or an empty page) is the last page. Stopping on a short page
avoids the extra empty-page request the plain ``PageNumberPaginator`` would pay.
"""
def __init__(self) -> None:
super().__init__(base_page=0, page_param="page")
def update_state(self, response: Response, data: Optional[list[Any]] = None) -> None:
super().update_state(response, data)
if self._has_next_page and data is not None and len(data) < REQUESTS_PAGE_SIZE:
self._has_next_page = False
@dataclasses.dataclass(frozen=False)
class DeepgramResumeConfig:
# Opaque paginator/fan-out state handed back by the rest_source framework (per-parent completed-path
# progress plus the in-progress project's requests-log page). Retained as a single blob so the shape
# can evolve without a state-format migration.
fanout_state: dict[str, Any] | None = None
# Legacy fields from the hand-rolled resume format. Kept (with defaults) so an old saved state still
# parses via ``dataclass(**saved)``; a run resumed from one starts fan-out fresh (a re-read the merge
# dedupes) rather than mis-mapping the old positional scope onto the new state.
project_id: str | None = None
page: int | None = None
def _format_start_value(value: Any) -> str:
"""Format an incremental cursor value for Deepgram's `start` filter.
The requests log accepts only YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS, or YYYY-MM-DDTHH:MM:SS+HH:MM.
It rejects fractional seconds and a `Z` suffix with HTTP 400, so a datetime cursor is emitted as
whole-second ISO 8601 with a numeric UTC offset. The seconds are floored, so the boundary second
is re-fetched (the merge dedupes on the composite primary key) rather than skipped. Future-dated
cursors are capped at now so we never build a start-in-the-future filter.
"""
now = datetime.now(UTC)
if isinstance(value, datetime):
aware = value if value.tzinfo is not None else value.replace(tzinfo=UTC)
aware = now if aware > now else aware
return aware.astimezone(UTC).replace(microsecond=0).isoformat()
if isinstance(value, date):
capped = now.date() if value > now.date() else value
return capped.isoformat()
return str(value)
def _format_start_date(value: Any) -> str:
"""Format an incremental cursor value for the usage and billing endpoints' `start` filter.
Those endpoints accept whole days only (YYYY-MM-DD) and reject a timestamp, so a datetime cursor
is truncated to its UTC date. The boundary day is therefore re-read in full each sync. The merge
collapses that re-read on the composite primary key, which is also what keeps an in-progress
day's totals current. Future-dated cursors are capped at today so we never ask for a window that
has not started.
"""
today = datetime.now(UTC).date()
if isinstance(value, datetime):
aware = value if value.tzinfo is not None else value.replace(tzinfo=UTC)
resolved = aware.astimezone(UTC).date()
elif isinstance(value, date):
resolved = value
else:
return str(value)[:10]
return min(resolved, today).isoformat()
def _redact_url_userinfo(url: str) -> str:
"""Strip embedded userinfo (`user:pass@`) from a URL.
Deepgram callback URLs can carry Basic Auth credentials in the userinfo component; those must not
land in the warehouse where anyone with query access could read them. The host/path is preserved
so the row still records which callback was used.
"""
try:
parts = urlsplit(url)
except ValueError:
return url
if "@" not in parts.netloc:
return url
host = parts.netloc.rsplit("@", 1)[1]
return urlunsplit(parts._replace(netloc=host))
def _grouping_key(row: dict[str, Any], dimensions: list[str]) -> str:
"""Build the stable slice identifier an aggregate row is keyed on.
Each dimension is null unless the request grouped by it, so no single dimension can key the row.
Joining all of them gives one non-null value per slice, and an empty string when the response is
grouped by period alone. The merge predicate then stays exact whichever grouping comes back.
"""
parts = []
for dimension in dimensions:
value = row.get(dimension)
if isinstance(value, list):
parts.append(",".join(str(item) for item in value))
else:
parts.append("" if value is None else str(value))
return "|".join(parts)
def _expand_models(row: dict[str, Any]) -> list[dict[str, Any]]:
# The catalogue arrives as two parallel arrays; each model becomes a row tagged with the array it
# came from, which is the only place the speech-to-text / text-to-speech split is recorded.
parent_id = row.get(_PARENT_ID_KEY)
return [
{**model, "model_type": model_type, _PARENT_ID_KEY: parent_id}
for model_type in ("stt", "tts")
for model in row.get(model_type) or []
]
def _expand_usage_fields(row: dict[str, Any]) -> list[dict[str, Any]]:
parent_id = row.get(_PARENT_ID_KEY)
rows: list[dict[str, Any]] = [
{
_PARENT_ID_KEY: parent_id,
"field": "models",
"value": model.get("model_id"),
"name": model.get("name"),
"language": model.get("language"),
"version": model.get("version"),
}
for model in row.get("models") or []
]
rows.extend(
{_PARENT_ID_KEY: parent_id, "field": field, "value": value}
for field in ("tags", "processing_methods", "features")
for value in row.get(field) or []
)
return rows
def _expand_billing_fields(row: dict[str, Any]) -> list[dict[str, Any]]:
parent_id = row.get(_PARENT_ID_KEY)
rows: list[dict[str, Any]] = [
{_PARENT_ID_KEY: parent_id, "field": field, "value": value}
for field in ("accessors", "deployments", "tags")
for value in row.get(field) or []
]
# Line items arrive as a name -> human-readable description map rather than a list.
rows.extend(
{_PARENT_ID_KEY: parent_id, "field": "line_items", "value": name, "description": description}
for name, description in (row.get("line_items") or {}).items()
)
return rows
# Endpoints whose response body is several parallel collections instead of one list of rows. Each
# expander turns one body into the rows the table holds; `data_key="$"` hands it the whole body.
_ROW_EXPANDERS: dict[str, Callable[[dict[str, Any]], list[dict[str, Any]]]] = {
"models": _expand_models,
"usage_fields": _expand_usage_fields,
"billing_fields": _expand_billing_fields,
}
def _normalize_row(config: DeepgramEndpointConfig, row: dict[str, Any]) -> dict[str, Any]:
if config.flatten_key and isinstance(row.get(config.flatten_key), dict):
nested = row.pop(config.flatten_key)
row = {**row, **nested}
# Fan-out rows carry the parent project's id so the composite primary key stays unique table-wide.
if _PARENT_ID_KEY in row:
row[_PARENT_ID_FIELD] = row.pop(_PARENT_ID_KEY)
# Request-log rows can echo the callback URL, which may embed Basic Auth credentials.
if isinstance(row.get("callback"), str):
row["callback"] = _redact_url_userinfo(row["callback"])
if config.grouping_dimensions:
row["grouping_key"] = _grouping_key(row, config.grouping_dimensions)
# A row missing a required primary-key field would let the delta merge build a partial predicate
# and overwrite unrelated rows in the same project, so fail loudly instead of emitting it.
for primary_key in config.primary_keys:
if row.get(primary_key) is None:
raise ValueError(f"Deepgram {config.name} row missing required primary key '{primary_key}'")
return row
def _make_child_map(
config: DeepgramEndpointConfig,
) -> Callable[[dict[str, Any]], dict[str, Any] | list[dict[str, Any]]]:
"""Per-row transform for a fanned-out (project-scoped) endpoint.
Reproduces the old ``_transform_row``: flatten a nested sub-object into the row root, lift the
framework's parent-id key back to the flat ``project_id`` column, redact any callback credentials,
and fail loud on a row missing a required primary key. Endpoints with a row expander return the
several rows their single response body carries.
"""
expand = _ROW_EXPANDERS.get(config.name)
def _map(row: dict[str, Any]) -> dict[str, Any] | list[dict[str, Any]]:
if expand is None:
return _normalize_row(config, row)
return [_normalize_row(config, expanded) for expanded in expand(row)]
return _map
def _incremental_config(
config: DeepgramEndpointConfig, should_use_incremental_field: bool, db_incremental_field_last_value: Any
) -> IncrementalConfig | None:
"""Server-side `start` filter for an incremental endpoint, only when we have a cursor to filter on.
No filter on a first (full) sync. The persisted watermark is formatted (and future-clamped) the
way the endpoint's `start` expects: whole seconds for the requests log, whole days for the usage
and billing aggregates.
"""
if not should_use_incremental_field or db_incremental_field_last_value is None:
return None
cursor = config.incremental_fields[0]
return {
"start_param": "start",
"cursor_path": cursor["field"],
"convert": _format_start_date if cursor["field_type"] == IncrementalFieldType.Date else _format_start_value,
}
def _projects_resource() -> EndpointResource:
# The top-level /projects list synced as its own table — yielded raw (no fan-out, no per-row
# transform), exactly as the old is_project_list path did.
return {
"name": "projects",
"endpoint": {
"path": "/projects",
"data_selector": "projects",
"paginator": SinglePagePaginator(),
},
}
def _projects_parent_resource() -> EndpointResource:
# The same /projects list used as the fan-out seed. The old code skipped projects with no
# project_id; drop them here so the child resolve (which would otherwise raise) never sees an
# id-less parent row.
return {
"name": "projects",
"endpoint": {
"path": "/projects",
"data_selector": "projects",
"paginator": SinglePagePaginator(),
},
"data_map": lambda row: [row] if row.get(_PARENT_ID_FIELD) else [],
}
def _child_resource(
endpoint: str, config: DeepgramEndpointConfig, incremental: IncrementalConfig | None
) -> EndpointResource:
params: dict[str, Any] = {
_PARENT_ID_FIELD: {"type": "resolve", "resource": _PARENT_RESOURCE, "field": _PARENT_ID_FIELD},
}
endpoint_config: Endpoint = {
"path": f"/projects/{{{_PARENT_ID_FIELD}}}{config.path}",
"params": params,
"data_selector": config.data_key,
}
if config.paginated:
params["limit"] = REQUESTS_PAGE_SIZE
endpoint_config["paginator"] = DeepgramRequestsPaginator()
else:
endpoint_config["paginator"] = SinglePagePaginator()
if incremental is not None:
endpoint_config["incremental"] = incremental
return {
"name": endpoint,
"include_from_parent": [_PARENT_ID_FIELD],
"endpoint": endpoint_config,
"data_map": _make_child_map(config),
}
def _resources_for(endpoint: str, incremental: IncrementalConfig | None) -> list[str | EndpointResource]:
config = DEEPGRAM_ENDPOINTS[endpoint]
if config.is_project_list:
return [_projects_resource()]
return [_projects_parent_resource(), _child_resource(endpoint, config, incremental)]
def deepgram_source(
api_key: str,
endpoint: str,
team_id: int,
job_id: str,
resumable_source_manager: ResumableSourceManager[DeepgramResumeConfig],
should_use_incremental_field: bool = False,
db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
config = DEEPGRAM_ENDPOINTS[endpoint]
incremental = (
_incremental_config(config, should_use_incremental_field, db_incremental_field_last_value)
if config.supports_incremental
else None
)
rest_config: RESTAPIConfig = {
"client": {
"base_url": DEEPGRAM_BASE_URL,
# Auth (the Token header) is supplied via the framework auth object so its value is redacted
# from logs; only the non-secret Accept header is set here.
"headers": {"Accept": "application/json"},
"auth": DeepgramTokenAuth(api_key),
},
"resource_defaults": {},
"resources": _resources_for(endpoint, incremental),
}
initial_paginator_state: dict[str, Any] | None = None
if resumable_source_manager.can_resume():
resume = resumable_source_manager.load_state()
if resume is not None and resume.fanout_state is not None:
initial_paginator_state = resume.fanout_state
def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
# Save AFTER a page is yielded so a crash re-yields the last page (merge dedupes) rather than
# skipping it. A ``None`` state means no page remains — nothing to persist.
if state is not None:
resumable_source_manager.save_state(DeepgramResumeConfig(fanout_state=state))
resources = rest_api_resources(
rest_config,
team_id,
job_id,
db_incremental_field_last_value,
resume_hook=save_checkpoint,
initial_paginator_state=initial_paginator_state,
)
resource: Resource = next(r for r in resources if r.name == endpoint)
return SourceResponse(
name=endpoint,
items=lambda: resource,
primary_keys=config.primary_keys,
# Deepgram documents no default order for any of the incremental endpoints, and it can't be
# curl-verified without a live token, so we use "desc": the pipeline finalises the incremental
# watermark only at job end rather than checkpointing per batch, which stays correct regardless
# of the actual arrival order. The `start` filter still bounds each incremental sync
# server-side, so this is not a re-fetch-all-history situation. Full-refresh endpoints keep
# the default "asc".
sort_mode="desc" if config.supports_incremental else "asc",
partition_count=1,
partition_size=1,
partition_mode="datetime" if config.partition_key else None,
partition_format="month" if config.partition_key else None,
partition_keys=[config.partition_key] if config.partition_key else None,
)
def validate_credentials(api_key: str) -> bool:
# Listing projects is the cheapest probe that proves the token is genuine; it is also the seed for
# every other (project-scoped) endpoint, so a token that can't list projects can't sync anything.
ok, _status = validate_via_probe(
lambda: make_tracked_session(redact_values=(api_key,)),
f"{DEEPGRAM_BASE_URL}/projects",
headers={"Authorization": f"Token {api_key}", "Accept": "application/json"},
)
return ok