Skip to content

@posthog/ai crashes without @opentelemetry/exporter-trace-otlp-http despite it being marked optional #3250

@muhammadolim

Description

@muhammadolim

Bug description

@posthog/ai declares @opentelemetry/exporter-trace-otlp-http as an optional peer dependency in peerDependenciesMeta, but the main entry point unconditionally requires it:

CJS (dist/index.cjs, line 6):

var exporterTraceOtlpHttp = require('@opentelemetry/exporter-trace-otlp-http');

ESM (dist/index.mjs, line 5):

import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';

This causes a MODULE_NOT_FOUND crash at import time for any consumer that doesn't install the OpenTelemetry package — even if they only use withTracing or LangChainCallbackHandler, which have nothing to do with OpenTelemetry.

Steps to reproduce

  1. Install @posthog/ai without @opentelemetry/exporter-trace-otlp-http
  2. Import anything from the package:
    import { withTracing } from '@posthog/ai';
  3. Application crashes:
    Error: Cannot find module '@opentelemetry/exporter-trace-otlp-http'
    

Root cause

PostHogTraceExporter (which extends OTLPTraceExporter) is bundled into the main dist/index.cjs and dist/index.mjs entry points. Because it extends the OTel base class, the import must resolve at module load time — making the "optional" declaration in peerDependenciesMeta ineffective at runtime.

The sub-entry point @posthog/ai/otel already exists and correctly isolates this dependency. The issue is that the main entry point re-exports it as well.

Suggested fix

Any of these would work:

  1. Remove PostHogTraceExporter from the main entry point — keep it only in @posthog/ai/otel (least invasive)
  2. Lazy-load the dependency inside PostHogTraceExporter's constructor via dynamic require()
  3. Wrap the top-level require in try/catch and throw a descriptive error only when PostHogTraceExporter is actually instantiated

Environment

  • @posthog/ai: 7.12.0
  • Node.js: v22.22.0
  • Package manager: Yarn 1.22.22

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions