@langfuse/core 4.5.0 → 4.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -253,7 +253,7 @@ var resetGlobalLogger = () => {
253
253
  // package.json
254
254
  var package_default = {
255
255
  name: "@langfuse/core",
256
- version: "4.4.10",
256
+ version: "4.5.0",
257
257
  description: "Core functions and utilities for Langfuse packages",
258
258
  type: "module",
259
259
  sideEffects: false,
@@ -710,12 +710,18 @@ var MediaContentType = {
710
710
  ApplicationX7ZCompressed: "application/x-7z-compressed"
711
711
  };
712
712
 
713
+ // src/api/api/resources/metricsV2/index.ts
714
+ var metricsV2_exports = {};
715
+
713
716
  // src/api/api/resources/metrics/index.ts
714
717
  var metrics_exports = {};
715
718
 
716
719
  // src/api/api/resources/models/index.ts
717
720
  var models_exports = {};
718
721
 
722
+ // src/api/api/resources/observationsV2/index.ts
723
+ var observationsV2_exports = {};
724
+
719
725
  // src/api/api/resources/observations/index.ts
720
726
  var observations_exports = {};
721
727
 
@@ -5588,6 +5594,232 @@ var Media = class {
5588
5594
  }
5589
5595
  };
5590
5596
 
5597
+ // src/api/api/resources/metricsV2/client/Client.ts
5598
+ var MetricsV2 = class {
5599
+ constructor(_options) {
5600
+ this._options = _options;
5601
+ }
5602
+ /**
5603
+ * Get metrics from the Langfuse project using a query object. V2 endpoint with optimized performance.
5604
+ *
5605
+ * ## V2 Differences
5606
+ * - Supports `observations`, `scores-numeric`, and `scores-categorical` views only (traces view not supported)
5607
+ * - Direct access to tags and release fields on observations
5608
+ * - Backwards-compatible: traceName, traceRelease, traceVersion dimensions are still available on observations view
5609
+ * - High cardinality dimensions are not supported and will return a 400 error (see below)
5610
+ *
5611
+ * For more details, see the [Metrics API documentation](https://langfuse.com/docs/metrics/features/metrics-api).
5612
+ *
5613
+ * ## Available Views
5614
+ *
5615
+ * ### observations
5616
+ * Query observation-level data (spans, generations, events).
5617
+ *
5618
+ * **Dimensions:**
5619
+ * - `environment` - Deployment environment (e.g., production, staging)
5620
+ * - `type` - Type of observation (SPAN, GENERATION, EVENT)
5621
+ * - `name` - Name of the observation
5622
+ * - `level` - Logging level of the observation
5623
+ * - `version` - Version of the observation
5624
+ * - `tags` - User-defined tags
5625
+ * - `release` - Release version
5626
+ * - `traceName` - Name of the parent trace (backwards-compatible)
5627
+ * - `traceRelease` - Release version of the parent trace (backwards-compatible, maps to release)
5628
+ * - `traceVersion` - Version of the parent trace (backwards-compatible, maps to version)
5629
+ * - `providedModelName` - Name of the model used
5630
+ * - `promptName` - Name of the prompt used
5631
+ * - `promptVersion` - Version of the prompt used
5632
+ * - `startTimeMonth` - Month of start_time in YYYY-MM format
5633
+ *
5634
+ * **Measures:**
5635
+ * - `count` - Total number of observations
5636
+ * - `latency` - Observation latency (milliseconds)
5637
+ * - `streamingLatency` - Generation latency from completion start to end (milliseconds)
5638
+ * - `inputTokens` - Sum of input tokens consumed
5639
+ * - `outputTokens` - Sum of output tokens produced
5640
+ * - `totalTokens` - Sum of all tokens consumed
5641
+ * - `outputTokensPerSecond` - Output tokens per second
5642
+ * - `tokensPerSecond` - Total tokens per second
5643
+ * - `inputCost` - Input cost (USD)
5644
+ * - `outputCost` - Output cost (USD)
5645
+ * - `totalCost` - Total cost (USD)
5646
+ * - `timeToFirstToken` - Time to first token (milliseconds)
5647
+ * - `countScores` - Number of scores attached to the observation
5648
+ *
5649
+ * ### scores-numeric
5650
+ * Query numeric and boolean score data.
5651
+ *
5652
+ * **Dimensions:**
5653
+ * - `environment` - Deployment environment
5654
+ * - `name` - Name of the score (e.g., accuracy, toxicity)
5655
+ * - `source` - Origin of the score (API, ANNOTATION, EVAL)
5656
+ * - `dataType` - Data type (NUMERIC, BOOLEAN)
5657
+ * - `configId` - Identifier of the score config
5658
+ * - `timestampMonth` - Month in YYYY-MM format
5659
+ * - `timestampDay` - Day in YYYY-MM-DD format
5660
+ * - `value` - Numeric value of the score
5661
+ * - `traceName` - Name of the parent trace
5662
+ * - `tags` - Tags
5663
+ * - `traceRelease` - Release version
5664
+ * - `traceVersion` - Version
5665
+ * - `observationName` - Name of the associated observation
5666
+ * - `observationModelName` - Model name of the associated observation
5667
+ * - `observationPromptName` - Prompt name of the associated observation
5668
+ * - `observationPromptVersion` - Prompt version of the associated observation
5669
+ *
5670
+ * **Measures:**
5671
+ * - `count` - Total number of scores
5672
+ * - `value` - Score value (for aggregations)
5673
+ *
5674
+ * ### scores-categorical
5675
+ * Query categorical score data. Same dimensions as scores-numeric except uses `stringValue` instead of `value`.
5676
+ *
5677
+ * **Measures:**
5678
+ * - `count` - Total number of scores
5679
+ *
5680
+ * ## High Cardinality Dimensions
5681
+ * The following dimensions cannot be used as grouping dimensions in v2 metrics API as they can cause performance issues.
5682
+ * Use them in filters instead.
5683
+ *
5684
+ * **observations view:**
5685
+ * - `id` - Use traceId filter to narrow down results
5686
+ * - `traceId` - Use traceId filter instead
5687
+ * - `userId` - Use userId filter instead
5688
+ * - `sessionId` - Use sessionId filter instead
5689
+ * - `parentObservationId` - Use parentObservationId filter instead
5690
+ *
5691
+ * **scores-numeric / scores-categorical views:**
5692
+ * - `id` - Use specific filters to narrow down results
5693
+ * - `traceId` - Use traceId filter instead
5694
+ * - `userId` - Use userId filter instead
5695
+ * - `sessionId` - Use sessionId filter instead
5696
+ * - `observationId` - Use observationId filter instead
5697
+ *
5698
+ * ## Aggregations
5699
+ * Available aggregation functions: `sum`, `avg`, `count`, `max`, `min`, `p50`, `p75`, `p90`, `p95`, `p99`, `histogram`
5700
+ *
5701
+ * ## Time Granularities
5702
+ * Available granularities for timeDimension: `auto`, `minute`, `hour`, `day`, `week`, `month`
5703
+ * - `auto` bins the data into approximately 50 buckets based on the time range
5704
+ *
5705
+ * @param {LangfuseAPI.GetMetricsV2Request} request
5706
+ * @param {MetricsV2.RequestOptions} requestOptions - Request-specific configuration.
5707
+ *
5708
+ * @throws {@link LangfuseAPI.Error}
5709
+ * @throws {@link LangfuseAPI.UnauthorizedError}
5710
+ * @throws {@link LangfuseAPI.AccessDeniedError}
5711
+ * @throws {@link LangfuseAPI.MethodNotAllowedError}
5712
+ * @throws {@link LangfuseAPI.NotFoundError}
5713
+ *
5714
+ * @example
5715
+ * await client.metricsV2.metrics({
5716
+ * query: "query"
5717
+ * })
5718
+ */
5719
+ metrics(request, requestOptions) {
5720
+ return HttpResponsePromise.fromPromise(
5721
+ this.__metrics(request, requestOptions)
5722
+ );
5723
+ }
5724
+ async __metrics(request, requestOptions) {
5725
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
5726
+ const { query } = request;
5727
+ const _queryParams = {};
5728
+ _queryParams["query"] = query;
5729
+ let _headers = mergeHeaders(
5730
+ (_a2 = this._options) == null ? void 0 : _a2.headers,
5731
+ mergeOnlyDefinedHeaders({
5732
+ Authorization: await this._getAuthorizationHeader(),
5733
+ "X-Langfuse-Sdk-Name": (_c = requestOptions == null ? void 0 : requestOptions.xLangfuseSdkName) != null ? _c : (_b = this._options) == null ? void 0 : _b.xLangfuseSdkName,
5734
+ "X-Langfuse-Sdk-Version": (_e = requestOptions == null ? void 0 : requestOptions.xLangfuseSdkVersion) != null ? _e : (_d = this._options) == null ? void 0 : _d.xLangfuseSdkVersion,
5735
+ "X-Langfuse-Public-Key": (_g = requestOptions == null ? void 0 : requestOptions.xLangfusePublicKey) != null ? _g : (_f = this._options) == null ? void 0 : _f.xLangfusePublicKey
5736
+ }),
5737
+ requestOptions == null ? void 0 : requestOptions.headers
5738
+ );
5739
+ const _response = await fetcher({
5740
+ url: url_exports.join(
5741
+ (_h = await Supplier.get(this._options.baseUrl)) != null ? _h : await Supplier.get(this._options.environment),
5742
+ "/api/public/v2/metrics"
5743
+ ),
5744
+ method: "GET",
5745
+ headers: _headers,
5746
+ queryParameters: { ..._queryParams, ...requestOptions == null ? void 0 : requestOptions.queryParams },
5747
+ timeoutMs: (requestOptions == null ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1e3 : 6e4,
5748
+ maxRetries: requestOptions == null ? void 0 : requestOptions.maxRetries,
5749
+ abortSignal: requestOptions == null ? void 0 : requestOptions.abortSignal
5750
+ });
5751
+ if (_response.ok) {
5752
+ return {
5753
+ data: _response.body,
5754
+ rawResponse: _response.rawResponse
5755
+ };
5756
+ }
5757
+ if (_response.error.reason === "status-code") {
5758
+ switch (_response.error.statusCode) {
5759
+ case 400:
5760
+ throw new Error2(
5761
+ _response.error.body,
5762
+ _response.rawResponse
5763
+ );
5764
+ case 401:
5765
+ throw new UnauthorizedError(
5766
+ _response.error.body,
5767
+ _response.rawResponse
5768
+ );
5769
+ case 403:
5770
+ throw new AccessDeniedError(
5771
+ _response.error.body,
5772
+ _response.rawResponse
5773
+ );
5774
+ case 405:
5775
+ throw new MethodNotAllowedError(
5776
+ _response.error.body,
5777
+ _response.rawResponse
5778
+ );
5779
+ case 404:
5780
+ throw new NotFoundError(
5781
+ _response.error.body,
5782
+ _response.rawResponse
5783
+ );
5784
+ default:
5785
+ throw new LangfuseAPIError({
5786
+ statusCode: _response.error.statusCode,
5787
+ body: _response.error.body,
5788
+ rawResponse: _response.rawResponse
5789
+ });
5790
+ }
5791
+ }
5792
+ switch (_response.error.reason) {
5793
+ case "non-json":
5794
+ throw new LangfuseAPIError({
5795
+ statusCode: _response.error.statusCode,
5796
+ body: _response.error.rawBody,
5797
+ rawResponse: _response.rawResponse
5798
+ });
5799
+ case "timeout":
5800
+ throw new LangfuseAPITimeoutError(
5801
+ "Timeout exceeded when calling GET /api/public/v2/metrics."
5802
+ );
5803
+ case "unknown":
5804
+ throw new LangfuseAPIError({
5805
+ message: _response.error.errorMessage,
5806
+ rawResponse: _response.rawResponse
5807
+ });
5808
+ }
5809
+ }
5810
+ async _getAuthorizationHeader() {
5811
+ const username = await Supplier.get(this._options.username);
5812
+ const password = await Supplier.get(this._options.password);
5813
+ if (username != null && password != null) {
5814
+ return BasicAuth.toAuthorizationHeader({
5815
+ username,
5816
+ password
5817
+ });
5818
+ }
5819
+ return void 0;
5820
+ }
5821
+ };
5822
+
5591
5823
  // src/api/api/resources/metrics/client/Client.ts
5592
5824
  var Metrics = class {
5593
5825
  constructor(_options) {
@@ -6163,6 +6395,217 @@ var Models = class {
6163
6395
  }
6164
6396
  };
6165
6397
 
6398
+ // src/api/api/resources/observationsV2/client/Client.ts
6399
+ var ObservationsV2 = class {
6400
+ constructor(_options) {
6401
+ this._options = _options;
6402
+ }
6403
+ /**
6404
+ * Get a list of observations with cursor-based pagination and flexible field selection.
6405
+ *
6406
+ * ## Cursor-based Pagination
6407
+ * This endpoint uses cursor-based pagination for efficient traversal of large datasets.
6408
+ * The cursor is returned in the response metadata and should be passed in subsequent requests
6409
+ * to retrieve the next page of results.
6410
+ *
6411
+ * ## Field Selection
6412
+ * Use the `fields` parameter to control which observation fields are returned:
6413
+ * - `core` - Always included: id, traceId, startTime, endTime, projectId, parentObservationId, type
6414
+ * - `basic` - name, level, statusMessage, version, environment, bookmarked, public, userId, sessionId
6415
+ * - `time` - completionStartTime, createdAt, updatedAt
6416
+ * - `io` - input, output
6417
+ * - `metadata` - metadata
6418
+ * - `model` - providedModelName, internalModelId, modelParameters
6419
+ * - `usage` - usageDetails, costDetails, totalCost
6420
+ * - `prompt` - promptId, promptName, promptVersion
6421
+ * - `metrics` - latency, timeToFirstToken
6422
+ *
6423
+ * If not specified, `core` and `basic` field groups are returned.
6424
+ *
6425
+ * ## Filters
6426
+ * Multiple filtering options are available via query parameters or the structured `filter` parameter.
6427
+ * When using the `filter` parameter, it takes precedence over individual query parameter filters.
6428
+ *
6429
+ * @param {LangfuseAPI.GetObservationsV2Request} request
6430
+ * @param {ObservationsV2.RequestOptions} requestOptions - Request-specific configuration.
6431
+ *
6432
+ * @throws {@link LangfuseAPI.Error}
6433
+ * @throws {@link LangfuseAPI.UnauthorizedError}
6434
+ * @throws {@link LangfuseAPI.AccessDeniedError}
6435
+ * @throws {@link LangfuseAPI.MethodNotAllowedError}
6436
+ * @throws {@link LangfuseAPI.NotFoundError}
6437
+ *
6438
+ * @example
6439
+ * await client.observationsV2.getMany()
6440
+ */
6441
+ getMany(request = {}, requestOptions) {
6442
+ return HttpResponsePromise.fromPromise(
6443
+ this.__getMany(request, requestOptions)
6444
+ );
6445
+ }
6446
+ async __getMany(request = {}, requestOptions) {
6447
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
6448
+ const {
6449
+ fields,
6450
+ limit,
6451
+ cursor,
6452
+ parseIoAsJson,
6453
+ name,
6454
+ userId,
6455
+ type: type_,
6456
+ traceId,
6457
+ level,
6458
+ parentObservationId,
6459
+ environment,
6460
+ fromStartTime,
6461
+ toStartTime,
6462
+ version,
6463
+ filter
6464
+ } = request;
6465
+ const _queryParams = {};
6466
+ if (fields != null) {
6467
+ _queryParams["fields"] = fields;
6468
+ }
6469
+ if (limit != null) {
6470
+ _queryParams["limit"] = limit.toString();
6471
+ }
6472
+ if (cursor != null) {
6473
+ _queryParams["cursor"] = cursor;
6474
+ }
6475
+ if (parseIoAsJson != null) {
6476
+ _queryParams["parseIoAsJson"] = parseIoAsJson.toString();
6477
+ }
6478
+ if (name != null) {
6479
+ _queryParams["name"] = name;
6480
+ }
6481
+ if (userId != null) {
6482
+ _queryParams["userId"] = userId;
6483
+ }
6484
+ if (type_ != null) {
6485
+ _queryParams["type"] = type_;
6486
+ }
6487
+ if (traceId != null) {
6488
+ _queryParams["traceId"] = traceId;
6489
+ }
6490
+ if (level != null) {
6491
+ _queryParams["level"] = level;
6492
+ }
6493
+ if (parentObservationId != null) {
6494
+ _queryParams["parentObservationId"] = parentObservationId;
6495
+ }
6496
+ if (environment != null) {
6497
+ if (Array.isArray(environment)) {
6498
+ _queryParams["environment"] = environment.map((item) => item);
6499
+ } else {
6500
+ _queryParams["environment"] = environment;
6501
+ }
6502
+ }
6503
+ if (fromStartTime != null) {
6504
+ _queryParams["fromStartTime"] = fromStartTime;
6505
+ }
6506
+ if (toStartTime != null) {
6507
+ _queryParams["toStartTime"] = toStartTime;
6508
+ }
6509
+ if (version != null) {
6510
+ _queryParams["version"] = version;
6511
+ }
6512
+ if (filter != null) {
6513
+ _queryParams["filter"] = filter;
6514
+ }
6515
+ let _headers = mergeHeaders(
6516
+ (_a2 = this._options) == null ? void 0 : _a2.headers,
6517
+ mergeOnlyDefinedHeaders({
6518
+ Authorization: await this._getAuthorizationHeader(),
6519
+ "X-Langfuse-Sdk-Name": (_c = requestOptions == null ? void 0 : requestOptions.xLangfuseSdkName) != null ? _c : (_b = this._options) == null ? void 0 : _b.xLangfuseSdkName,
6520
+ "X-Langfuse-Sdk-Version": (_e = requestOptions == null ? void 0 : requestOptions.xLangfuseSdkVersion) != null ? _e : (_d = this._options) == null ? void 0 : _d.xLangfuseSdkVersion,
6521
+ "X-Langfuse-Public-Key": (_g = requestOptions == null ? void 0 : requestOptions.xLangfusePublicKey) != null ? _g : (_f = this._options) == null ? void 0 : _f.xLangfusePublicKey
6522
+ }),
6523
+ requestOptions == null ? void 0 : requestOptions.headers
6524
+ );
6525
+ const _response = await fetcher({
6526
+ url: url_exports.join(
6527
+ (_h = await Supplier.get(this._options.baseUrl)) != null ? _h : await Supplier.get(this._options.environment),
6528
+ "/api/public/v2/observations"
6529
+ ),
6530
+ method: "GET",
6531
+ headers: _headers,
6532
+ queryParameters: { ..._queryParams, ...requestOptions == null ? void 0 : requestOptions.queryParams },
6533
+ timeoutMs: (requestOptions == null ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1e3 : 6e4,
6534
+ maxRetries: requestOptions == null ? void 0 : requestOptions.maxRetries,
6535
+ abortSignal: requestOptions == null ? void 0 : requestOptions.abortSignal
6536
+ });
6537
+ if (_response.ok) {
6538
+ return {
6539
+ data: _response.body,
6540
+ rawResponse: _response.rawResponse
6541
+ };
6542
+ }
6543
+ if (_response.error.reason === "status-code") {
6544
+ switch (_response.error.statusCode) {
6545
+ case 400:
6546
+ throw new Error2(
6547
+ _response.error.body,
6548
+ _response.rawResponse
6549
+ );
6550
+ case 401:
6551
+ throw new UnauthorizedError(
6552
+ _response.error.body,
6553
+ _response.rawResponse
6554
+ );
6555
+ case 403:
6556
+ throw new AccessDeniedError(
6557
+ _response.error.body,
6558
+ _response.rawResponse
6559
+ );
6560
+ case 405:
6561
+ throw new MethodNotAllowedError(
6562
+ _response.error.body,
6563
+ _response.rawResponse
6564
+ );
6565
+ case 404:
6566
+ throw new NotFoundError(
6567
+ _response.error.body,
6568
+ _response.rawResponse
6569
+ );
6570
+ default:
6571
+ throw new LangfuseAPIError({
6572
+ statusCode: _response.error.statusCode,
6573
+ body: _response.error.body,
6574
+ rawResponse: _response.rawResponse
6575
+ });
6576
+ }
6577
+ }
6578
+ switch (_response.error.reason) {
6579
+ case "non-json":
6580
+ throw new LangfuseAPIError({
6581
+ statusCode: _response.error.statusCode,
6582
+ body: _response.error.rawBody,
6583
+ rawResponse: _response.rawResponse
6584
+ });
6585
+ case "timeout":
6586
+ throw new LangfuseAPITimeoutError(
6587
+ "Timeout exceeded when calling GET /api/public/v2/observations."
6588
+ );
6589
+ case "unknown":
6590
+ throw new LangfuseAPIError({
6591
+ message: _response.error.errorMessage,
6592
+ rawResponse: _response.rawResponse
6593
+ });
6594
+ }
6595
+ }
6596
+ async _getAuthorizationHeader() {
6597
+ const username = await Supplier.get(this._options.username);
6598
+ const password = await Supplier.get(this._options.password);
6599
+ if (username != null && password != null) {
6600
+ return BasicAuth.toAuthorizationHeader({
6601
+ username,
6602
+ password
6603
+ });
6604
+ }
6605
+ return void 0;
6606
+ }
6607
+ };
6608
+
6166
6609
  // src/api/api/resources/observations/client/Client.ts
6167
6610
  var Observations = class {
6168
6611
  constructor(_options) {
@@ -11428,6 +11871,10 @@ var LangfuseAPIClient = class {
11428
11871
  var _a2;
11429
11872
  return (_a2 = this._media) != null ? _a2 : this._media = new Media(this._options);
11430
11873
  }
11874
+ get metricsV2() {
11875
+ var _a2;
11876
+ return (_a2 = this._metricsV2) != null ? _a2 : this._metricsV2 = new MetricsV2(this._options);
11877
+ }
11431
11878
  get metrics() {
11432
11879
  var _a2;
11433
11880
  return (_a2 = this._metrics) != null ? _a2 : this._metrics = new Metrics(this._options);
@@ -11436,6 +11883,10 @@ var LangfuseAPIClient = class {
11436
11883
  var _a2;
11437
11884
  return (_a2 = this._models) != null ? _a2 : this._models = new Models(this._options);
11438
11885
  }
11886
+ get observationsV2() {
11887
+ var _a2;
11888
+ return (_a2 = this._observationsV2) != null ? _a2 : this._observationsV2 = new ObservationsV2(this._options);
11889
+ }
11439
11890
  get observations() {
11440
11891
  var _a2;
11441
11892
  return (_a2 = this._observations) != null ? _a2 : this._observations = new Observations(this._options);
@@ -12111,8 +12562,10 @@ export {
12111
12562
  LoggerSingleton as logger,
12112
12563
  media_exports as media,
12113
12564
  metrics_exports as metrics,
12565
+ metricsV2_exports as metricsV2,
12114
12566
  models_exports as models,
12115
12567
  observations_exports as observations,
12568
+ observationsV2_exports as observationsV2,
12116
12569
  opentelemetry_exports as opentelemetry,
12117
12570
  organizations_exports as organizations,
12118
12571
  projects_exports as projects,