Skip to content

: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.

9.2 KB
Raw Blame
from dataclasses import dataclass, field
from datetime import timedelta
from typing import Any, Literal, Optional

from products.warehouse_sources.sources.sdk import IncrementalField, IncrementalFieldType

# US cloud host. EU-region accounts use https://eu.api.smith.langchain.com and self-hosted
# deployments use their own host — the user overrides via the source's `host` field.
DEFAULT_BASE_URL = "https://api.smith.langchain.com"

# Fields requested from POST /runs/query via `select`. The default select pulls heavyweight
# internals (manifest, serialized, events, S3 URL variants) that bloat every page and slow the
# tight run-query rate limits, so we ask only for the analytics-relevant columns.
RUNS_SELECT_FIELDS = [
    "id",
    "name",
    "run_type",
    "start_time",
    "end_time",
    "status",
    "error",
    "extra",
    "inputs",
    "outputs",
    "parent_run_id",
    "session_id",
    "reference_example_id",
    "total_tokens",
    "prompt_tokens",
    "completion_tokens",
    "total_cost",
    "prompt_cost",
    "completion_cost",
    "first_token_time",
    "trace_id",
    "dotted_order",
    "tags",
    "thread_id",
    "feedback_stats",
]

# Run internals the annotation-queue runs listing returns but `RUNS_SELECT_FIELDS` leaves out of the
# runs table: serialized manifests, event streams, and pre-signed S3 URLs that expire anyway.
ANNOTATION_QUEUE_RUNS_DROPPED_FIELDS = (
    "serialized",
    "manifest_id",
    "manifest_s3_id",
    "events",
    "inputs_s3_urls",
    "outputs_s3_urls",
    "s3_urls",
)

# The fields holding a run's raw prompt and completion. They carry nearly all of a run's size, so
# `_fetch_runs_page` gives them up as a last resort when one run stays over the response cap.
RUNS_HEAVY_SELECT_FIELDS = ("inputs", "outputs")


@dataclass(frozen=False)  # mutability is unused (always constructed fresh); explicit per house convention
class LangSmithEndpointConfig:
    name: str
    path: str
    incremental_fields: list[IncrementalField]
    # runs/query and threads/query are POSTs whose cursor lives in the JSON body, workspaces is one
    # unpaginated GET, and every other list endpoint is a GET paginated with offset/limit params.
    pagination: Literal["cursor", "offset", "none"] = "offset"
    # Stable creation-style timestamp to partition by (never a mutable `modified_at` field).
    partition_key: Optional[str] = None
    page_size: int = 100
    primary_keys: list[str] = field(default_factory=lambda: ["id"])
    # Name of the server-side lower-bound time filter (`start_time` in the runs/query body,
    # `min_created_at` query param on feedback). None means no server-side window — full
    # refresh only.
    window_param: Optional[str] = None
    # Floor the first incremental backfill to the last N days instead of the entire retention
    # window, bounding the initial pull against the tight runs/query rate limits. On threads it is
    # the fixed window of every full refresh, because the API otherwise defaults to the last day.
    default_lookback_days: Optional[int] = None
    # Trailing overlap re-subtracted from the watermark on every incremental run, re-pulling a
    # window of rows so runs whose fields mutate after creation (end_time, outputs, feedback
    # stats land when the run finishes) are re-read; the delta merge dedupes on the primary key.
    incremental_lookback: Optional[timedelta] = None
    # We request `order=asc` on runs/query, but we can't rely on response ordering across every
    # LangSmith deployment (offset endpoints document no ordering at all), so every incremental
    # endpoint declares `desc`: the pipeline then persists the watermark (max value seen) only at
    # successful job end instead of checkpointing per batch, which is correct under any actual
    # ordering. Mid-run recovery comes from the resumable cursor/offset state instead.
    sort_mode: Literal["asc", "desc"] = "asc"
    # Endpoint whose ids scope this one. Examples and annotation-queue runs can only be listed per
    # dataset or per queue, and threads only per tracing project, so they are paged once per parent.
    parent: Optional[str] = None
    # Query param that carries the parent id. None puts it in the `{parent_id}` placeholder of `path`.
    parent_query_param: Optional[str] = None
    # Row field the parent id is written to, for endpoints whose rows don't carry it themselves.
    parent_id_field: Optional[str] = None
    # Extra query params for listing the parent ids.
    parent_list_params: dict[str, Any] = field(default_factory=dict)
    # Fields removed from every row before it is yielded.
    dropped_fields: tuple[str, ...] = ()


_START_TIME_INCREMENTAL_FIELDS: list[IncrementalField] = [
    {
        "label": "start_time",
        "type": IncrementalFieldType.DateTime,
        "field": "start_time",
        "field_type": IncrementalFieldType.DateTime,
    },
]

_CREATED_AT_INCREMENTAL_FIELDS: list[IncrementalField] = [
    {
        "label": "created_at",
        "type": IncrementalFieldType.DateTime,
        "field": "created_at",
        "field_type": IncrementalFieldType.DateTime,
    },
]


LANGSMITH_ENDPOINTS: dict[str, LangSmithEndpointConfig] = {
    # Traces and their nested spans (LLM calls, chains, tools, retrievers) across every tracing
    # project. The flagship table: one row per run, filtered server-side by start_time.
    "runs": LangSmithEndpointConfig(
        name="runs",
        path="/api/v1/runs/query",
        pagination="cursor",
        partition_key="start_time",
        incremental_fields=_START_TIME_INCREMENTAL_FIELDS,
        window_param="start_time",
        default_lookback_days=365,
        # end_time/outputs/feedback_stats land after the run starts; the start_time cursor alone
        # would freeze a run at its first-synced (possibly still-running) state.
        incremental_lookback=timedelta(hours=1),
        sort_mode="desc",
    ),
    # Tracing projects ("sessions" in the LangSmith API), with rolled-up run counts, costs, and
    # latency percentiles. Small list; no server-side time filter, so full refresh only.
    "projects": LangSmithEndpointConfig(
        name="projects",
        path="/api/v1/sessions",
        incremental_fields=[],
    ),
    "datasets": LangSmithEndpointConfig(
        name="datasets",
        path="/api/v1/datasets",
        incremental_fields=[],
    ),
    # Dataset examples across all datasets. GET /examples must be scoped to a dataset, so this is
    # paged per dataset (see `parent`). The API versions examples via `as_of` snapshots
    # rather than a created/modified filter, so full refresh only.
    "examples": LangSmithEndpointConfig(
        name="examples",
        path="/api/v1/examples",
        incremental_fields=[],
        parent="datasets",
        parent_query_param="dataset",
    ),
    # Human and programmatic feedback scores attached to runs. `min_created_at` is a genuine
    # server-side filter; feedback edits move modified_at (not filterable), so the lookback
    # re-pulls a trailing window and merge dedupes on id.
    "feedback": LangSmithEndpointConfig(
        name="feedback",
        path="/api/v1/feedback",
        partition_key="created_at",
        incremental_fields=_CREATED_AT_INCREMENTAL_FIELDS,
        window_param="min_created_at",
        default_lookback_days=365,
        incremental_lookback=timedelta(hours=1),
        sort_mode="desc",
    ),
    "annotation_queues": LangSmithEndpointConfig(
        name="annotation_queues",
        path="/api/v1/annotation-queues",
        incremental_fields=[],
    ),
    # Which runs sit in which annotation queue, with each item's review state. The listing has no
    # time filter, so full refresh only.
    "annotation_queue_runs": LangSmithEndpointConfig(
        name="annotation_queue_runs",
        path="/api/v1/annotation-queues/{parent_id}/runs",
        incremental_fields=[],
        parent="annotation_queues",
        parent_id_field="queue_id",
        primary_keys=["queue_id", "queue_run_id"],
        dropped_fields=ANNOTATION_QUEUE_RUNS_DROPPED_FIELDS,
    ),
    # Conversation threads per tracing project, with turn counts, token, cost, and latency rolled
    # up over the query window. The rollups only cover the window sent, so an incremental window
    # would overwrite a thread's totals with partial ones: full refresh over a fixed lookback.
    "threads": LangSmithEndpointConfig(
        name="threads",
        path="/api/v2/threads/query",
        pagination="cursor",
        incremental_fields=[],
        parent="projects",
        parent_id_field="project_id",
        # Experiments are tracing projects too, but hold evaluation runs, not conversations.
        parent_list_params={"reference_free": "true"},
        primary_keys=["project_id", "thread_id"],
        default_lookback_days=365,
    ),
    # Workspaces the API key can see. Resolves the `tenant_id` carried on queues, datasets, and
    # projects. GET /workspaces returns them all in one unpaginated list.
    "workspaces": LangSmithEndpointConfig(
        name="workspaces",
        path="/api/v1/workspaces",
        pagination="none",
        incremental_fields=[],
    ),
}

ENDPOINTS = tuple(LANGSMITH_ENDPOINTS.keys())

INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    name: config.incremental_fields for name, config in LANGSMITH_ENDPOINTS.items()
}