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.

5.9 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.cohere._config import CohereSourceConfig
from products.warehouse_sources.sources.cohere.cohere import (
    COHERE_API_VERSION_V1,
    cohere_source,
    validate_credentials as validate_cohere_credentials,
)
from products.warehouse_sources.sources.cohere.settings import ENDPOINTS, RETIRED_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    SimpleSource,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)


@SourceRegistry.register
class CohereSource(SimpleSource[CohereSourceConfig]):
    codebase_detection = detect(
        env=("^COHERE_", "^CO_API_KEY$"),
        npm=("cohere-ai",),
        pypi=("cohere",),
        go=("github.com/cohere-ai/cohere-go",),
        maven=("com.cohere:cohere-java",),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    # Cohere's v2 reference documents datasets, models, and embed jobs at /v1/ paths, so a "v2" pin
    # would name a wire Cohere does not serve for these tables. Batches exist only at /v2/, with no
    # v1 route to choose between, so that endpoint's path version is fixed in settings and is not a
    # version pin. v1 is not sunset either - the 2025-09-15 announcement retires individual
    # endpoints, not the version - so v1 stays supported and undeprecated.
    supported_versions = (COHERE_API_VERSION_V1,)
    default_version = COHERE_API_VERSION_V1
    api_docs_url = "https://docs.cohere.com/reference/about"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.COHERE

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.COHERE,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Cohere",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your Cohere API key to pull your Cohere assets and job history into the PostHog Data warehouse.

Create an API key in your [Cohere dashboard](https://dashboard.cohere.com/api-keys). Prefer a production key: trial keys are capped at 1,000 API calls per month, which a recurring sync can exhaust.
""",
            iconPath="/static/services/cohere.png",
            docsUrl="https://posthog.com/docs/cdp/sources/cohere",
            keywords=["llm", "ai", "embeddings", "fine-tuning"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="...",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.cohere.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # An invalid or revoked key surfaces as a requests HTTPError when `_fetch_page` calls
            # `raise_for_status()`. Retrying can't fix a credential problem, so stop the sync. Match
            # the stable status text and base host, not the per-request path/query.
            "401 Client Error: Unauthorized for url: https://api.cohere.com": "Your Cohere API key is invalid or has been revoked. Create a new key in your Cohere dashboard, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.cohere.com": "Your Cohere API key is missing the permissions needed to sync this data. Check the key permissions in your Cohere dashboard, then reconnect.",
            # A schema created before the endpoint was retired keeps syncing until someone turns it
            # off, and no retry brings a removed route back.
            **{reason: reason for reason in RETIRED_ENDPOINTS.values()},
        }

    def get_schemas(
        self,
        config: CohereSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Cohere exposes no reliable server-side timestamp range filter across these list endpoints
        # (only /datasets documents before/after created-at filters, and the entities are mutable
        # with tiny row counts), so every endpoint is full refresh only. Statuses and timestamps
        # mutate in place, so append-only would drop those updates.
        schemas = [
            SourceSchema(
                name=endpoint,
                supports_incremental=False,
                supports_append=False,
                incremental_fields=[],
            )
            for endpoint in ENDPOINTS
        ]
        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]
        return schemas

    def validate_credentials(
        self,
        config: CohereSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_cohere_credentials(config.api_key):
            return True, None

        return False, "Invalid Cohere API key"

    def source_for_pipeline(self, config: CohereSourceConfig, inputs: SourceInputs) -> SourceResponse:
        return cohere_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
        )