langsmith 0.3.49 → 0.3.50

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.
@@ -6,10 +6,24 @@ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
6
6
  const constants_js_1 = require("./constants.cjs");
7
7
  const utils_js_1 = require("./utils.cjs");
8
8
  const run_trees_js_1 = require("../../run_trees.cjs");
9
+ const NANOSECOND_DIGITS = 9;
10
+ const MICROSECOND_DIGITS = 6;
9
11
  function isTraceableSpan(span) {
10
12
  return (span.attributes[constants_js_1.LANGSMITH_TRACEABLE] === "true" ||
11
13
  typeof span.attributes["ai.operationId"] === "string");
12
14
  }
15
+ /**
16
+ * Convert hrTime to timestamp, for example "2019-05-14T17:00:00.000123Z"
17
+ * @param time
18
+ */
19
+ function hrTimeToTimeStamp(time) {
20
+ const precision = NANOSECOND_DIGITS;
21
+ const tmp = `${"0".repeat(precision)}${time[1]}Z`;
22
+ const nanoString = tmp.substring(tmp.length - precision - 1);
23
+ const date = new Date(time[0] * 1000).toISOString();
24
+ // We only need 6 digits of precision for the dotted order
25
+ return `${date.replace("000Z", nanoString.slice(0, MICROSECOND_DIGITS))}Z`;
26
+ }
13
27
  function getParentSpanId(span) {
14
28
  // Backcompat shim to support OTEL 1.x and 2.x
15
29
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -39,20 +53,35 @@ class LangSmithOTLPSpanProcessor extends sdk_trace_base_1.BatchSpanProcessor {
39
53
  this.traceMap[span.spanContext().traceId].spanCount++;
40
54
  const isTraceable = isTraceableSpan(span);
41
55
  const parentSpanId = getParentSpanId(span);
42
- this.traceMap[span.spanContext().traceId].spanInfo[span.spanContext().spanId] = {
43
- isTraceable,
44
- parentSpanId,
45
- };
46
56
  let currentCandidateParentSpanId = parentSpanId;
47
57
  let traceableParentId;
58
+ let parentDottedOrder;
59
+ // LangSmith uses the first span's id as the trace id, NOT the actual OTEL trace id
60
+ // Default to the current span if no parent information is present
61
+ let lsTraceId = (0, utils_js_1.getUuidFromOtelSpanId)(span.spanContext().spanId);
48
62
  while (currentCandidateParentSpanId) {
49
63
  const currentSpanInfo = this.traceMap[span.spanContext().traceId].spanInfo[currentCandidateParentSpanId];
50
64
  if (currentSpanInfo?.isTraceable) {
51
65
  traceableParentId = currentCandidateParentSpanId;
66
+ parentDottedOrder = currentSpanInfo.dottedOrder;
67
+ lsTraceId = currentSpanInfo.lsTraceId;
52
68
  break;
53
69
  }
54
70
  currentCandidateParentSpanId = currentSpanInfo?.parentSpanId;
55
71
  }
72
+ const startTimestamp = hrTimeToTimeStamp(span.startTime);
73
+ const spanUuid = (0, utils_js_1.getUuidFromOtelSpanId)(span.spanContext().spanId);
74
+ const dottedOrderComponent = (0, run_trees_js_1.stripNonAlphanumeric)(startTimestamp) + spanUuid;
75
+ const currentDottedOrder = parentDottedOrder
76
+ ? `${parentDottedOrder}.${dottedOrderComponent}`
77
+ : dottedOrderComponent;
78
+ this.traceMap[span.spanContext().traceId].spanInfo[span.spanContext().spanId] = {
79
+ isTraceable,
80
+ lsTraceId,
81
+ spanId: span.spanContext().spanId,
82
+ parentSpanId,
83
+ dottedOrder: currentDottedOrder,
84
+ };
56
85
  if (!traceableParentId) {
57
86
  span.attributes[constants_js_1.LANGSMITH_IS_ROOT] = true;
58
87
  }
@@ -60,6 +89,8 @@ class LangSmithOTLPSpanProcessor extends sdk_trace_base_1.BatchSpanProcessor {
60
89
  span.attributes[constants_js_1.LANGSMITH_PARENT_RUN_ID] =
61
90
  (0, utils_js_1.getUuidFromOtelSpanId)(traceableParentId);
62
91
  }
92
+ span.attributes[constants_js_1.LANGSMITH_DOTTED_ORDER] = currentDottedOrder;
93
+ span.attributes[constants_js_1.LANGSMITH_TRACE_ID] = lsTraceId;
63
94
  if (isTraceable) {
64
95
  super.onStart(span, parentContext);
65
96
  }
@@ -1,11 +1,25 @@
1
1
  import { BatchSpanProcessor, } from "@opentelemetry/sdk-trace-base";
2
- import { LANGSMITH_IS_ROOT, LANGSMITH_PARENT_RUN_ID, LANGSMITH_TRACEABLE, } from "./constants.js";
2
+ import { LANGSMITH_IS_ROOT, LANGSMITH_PARENT_RUN_ID, LANGSMITH_TRACEABLE, LANGSMITH_DOTTED_ORDER, LANGSMITH_TRACE_ID, } from "./constants.js";
3
3
  import { getUuidFromOtelSpanId } from "./utils.js";
4
- import { RunTree } from "../../run_trees.js";
4
+ import { RunTree, stripNonAlphanumeric } from "../../run_trees.js";
5
+ const NANOSECOND_DIGITS = 9;
6
+ const MICROSECOND_DIGITS = 6;
5
7
  export function isTraceableSpan(span) {
6
8
  return (span.attributes[LANGSMITH_TRACEABLE] === "true" ||
7
9
  typeof span.attributes["ai.operationId"] === "string");
8
10
  }
11
+ /**
12
+ * Convert hrTime to timestamp, for example "2019-05-14T17:00:00.000123Z"
13
+ * @param time
14
+ */
15
+ function hrTimeToTimeStamp(time) {
16
+ const precision = NANOSECOND_DIGITS;
17
+ const tmp = `${"0".repeat(precision)}${time[1]}Z`;
18
+ const nanoString = tmp.substring(tmp.length - precision - 1);
19
+ const date = new Date(time[0] * 1000).toISOString();
20
+ // We only need 6 digits of precision for the dotted order
21
+ return `${date.replace("000Z", nanoString.slice(0, MICROSECOND_DIGITS))}Z`;
22
+ }
9
23
  function getParentSpanId(span) {
10
24
  // Backcompat shim to support OTEL 1.x and 2.x
11
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -35,20 +49,35 @@ export class LangSmithOTLPSpanProcessor extends BatchSpanProcessor {
35
49
  this.traceMap[span.spanContext().traceId].spanCount++;
36
50
  const isTraceable = isTraceableSpan(span);
37
51
  const parentSpanId = getParentSpanId(span);
38
- this.traceMap[span.spanContext().traceId].spanInfo[span.spanContext().spanId] = {
39
- isTraceable,
40
- parentSpanId,
41
- };
42
52
  let currentCandidateParentSpanId = parentSpanId;
43
53
  let traceableParentId;
54
+ let parentDottedOrder;
55
+ // LangSmith uses the first span's id as the trace id, NOT the actual OTEL trace id
56
+ // Default to the current span if no parent information is present
57
+ let lsTraceId = getUuidFromOtelSpanId(span.spanContext().spanId);
44
58
  while (currentCandidateParentSpanId) {
45
59
  const currentSpanInfo = this.traceMap[span.spanContext().traceId].spanInfo[currentCandidateParentSpanId];
46
60
  if (currentSpanInfo?.isTraceable) {
47
61
  traceableParentId = currentCandidateParentSpanId;
62
+ parentDottedOrder = currentSpanInfo.dottedOrder;
63
+ lsTraceId = currentSpanInfo.lsTraceId;
48
64
  break;
49
65
  }
50
66
  currentCandidateParentSpanId = currentSpanInfo?.parentSpanId;
51
67
  }
68
+ const startTimestamp = hrTimeToTimeStamp(span.startTime);
69
+ const spanUuid = getUuidFromOtelSpanId(span.spanContext().spanId);
70
+ const dottedOrderComponent = stripNonAlphanumeric(startTimestamp) + spanUuid;
71
+ const currentDottedOrder = parentDottedOrder
72
+ ? `${parentDottedOrder}.${dottedOrderComponent}`
73
+ : dottedOrderComponent;
74
+ this.traceMap[span.spanContext().traceId].spanInfo[span.spanContext().spanId] = {
75
+ isTraceable,
76
+ lsTraceId,
77
+ spanId: span.spanContext().spanId,
78
+ parentSpanId,
79
+ dottedOrder: currentDottedOrder,
80
+ };
52
81
  if (!traceableParentId) {
53
82
  span.attributes[LANGSMITH_IS_ROOT] = true;
54
83
  }
@@ -56,6 +85,8 @@ export class LangSmithOTLPSpanProcessor extends BatchSpanProcessor {
56
85
  span.attributes[LANGSMITH_PARENT_RUN_ID] =
57
86
  getUuidFromOtelSpanId(traceableParentId);
58
87
  }
88
+ span.attributes[LANGSMITH_DOTTED_ORDER] = currentDottedOrder;
89
+ span.attributes[LANGSMITH_TRACE_ID] = lsTraceId;
59
90
  if (isTraceable) {
60
91
  super.onStart(span, parentContext);
61
92
  }
package/dist/index.cjs CHANGED
@@ -10,4 +10,4 @@ Object.defineProperty(exports, "overrideFetchImplementation", { enumerable: true
10
10
  var project_js_1 = require("./utils/project.cjs");
11
11
  Object.defineProperty(exports, "getDefaultProjectName", { enumerable: true, get: function () { return project_js_1.getDefaultProjectName; } });
12
12
  // Update using yarn bump-version
13
- exports.__version__ = "0.3.49";
13
+ exports.__version__ = "0.3.50";
package/dist/index.d.ts CHANGED
@@ -3,4 +3,4 @@ export type { Dataset, Example, TracerSession, Run, Feedback, RetrieverOutput, }
3
3
  export { RunTree, type RunTreeConfig } from "./run_trees.js";
4
4
  export { overrideFetchImplementation } from "./singletons/fetch.js";
5
5
  export { getDefaultProjectName } from "./utils/project.js";
6
- export declare const __version__ = "0.3.49";
6
+ export declare const __version__ = "0.3.50";
package/dist/index.js CHANGED
@@ -3,4 +3,4 @@ export { RunTree } from "./run_trees.js";
3
3
  export { overrideFetchImplementation } from "./singletons/fetch.js";
4
4
  export { getDefaultProjectName } from "./utils/project.js";
5
5
  // Update using yarn bump-version
6
- export const __version__ = "0.3.49";
6
+ export const __version__ = "0.3.50";
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.RunTree = void 0;
37
+ exports.stripNonAlphanumeric = stripNonAlphanumeric;
37
38
  exports.convertToDottedOrderFormat = convertToDottedOrderFormat;
38
39
  exports.isRunTree = isRunTree;
39
40
  exports.isRunnableConfigLike = isRunnableConfigLike;
@@ -1,5 +1,6 @@
1
1
  import { Client } from "./client.js";
2
2
  import { Attachments, BaseRun, KVMap, RunCreate } from "./schemas.js";
3
+ export declare function stripNonAlphanumeric(input: string): string;
3
4
  export declare function convertToDottedOrderFormat(epoch: number, runId: string, executionOrder?: number): {
4
5
  dottedOrder: string;
5
6
  microsecondPrecisionDatestring: string;
package/dist/run_trees.js CHANGED
@@ -7,7 +7,7 @@ import { getEnvironmentVariable, getRuntimeEnvironment, } from "./utils/env.js";
7
7
  import { getDefaultProjectName } from "./utils/project.js";
8
8
  import { getLangSmithEnvironmentVariable } from "./utils/env.js";
9
9
  import { warnOnce } from "./utils/warn.js";
10
- function stripNonAlphanumeric(input) {
10
+ export function stripNonAlphanumeric(input) {
11
11
  return input.replace(/[-:.]/g, "");
12
12
  }
13
13
  export function convertToDottedOrderFormat(epoch, runId, executionOrder = 1) {
@@ -6,14 +6,42 @@ function extractInputTokenDetails(providerMetadata, spanAttributes) {
6
6
  if (providerMetadata.anthropic != null &&
7
7
  typeof providerMetadata.anthropic === "object") {
8
8
  const anthropic = providerMetadata.anthropic;
9
- if (anthropic.cacheReadInputTokens != null &&
10
- typeof anthropic.cacheReadInputTokens === "number") {
11
- inputTokenDetails.cache_read = anthropic.cacheReadInputTokens;
9
+ if (anthropic.usage != null && typeof anthropic.usage === "object") {
10
+ // Raw usage from Anthropic returned in AI SDK 5
11
+ const usage = anthropic.usage;
12
+ if (usage.cache_creation != null &&
13
+ typeof usage.cache_creation === "object") {
14
+ const cacheCreation = usage.cache_creation;
15
+ if (typeof cacheCreation.ephemeral_5m_input_tokens === "number") {
16
+ inputTokenDetails.ephemeral_5m_input_tokens =
17
+ cacheCreation.ephemeral_5m_input_tokens;
18
+ }
19
+ if (typeof cacheCreation.ephemeral_1h_input_tokens === "number") {
20
+ inputTokenDetails.ephemeral_1hr_input_tokens =
21
+ cacheCreation.ephemeral_1h_input_tokens;
22
+ }
23
+ // If cache_creation not returned (no beta header passed),
24
+ // fallback to assuming 5m cache tokens
25
+ }
26
+ else if (typeof usage.cache_creation_input_tokens === "number") {
27
+ inputTokenDetails.ephemeral_5m_input_tokens =
28
+ usage.cache_creation_input_tokens;
29
+ }
30
+ if (typeof usage.cache_read_input_tokens === "number") {
31
+ inputTokenDetails.cache_read = usage.cache_read_input_tokens;
32
+ }
12
33
  }
13
- if (anthropic.cacheCreationInputTokens != null &&
14
- typeof anthropic.cacheCreationInputTokens === "number") {
15
- inputTokenDetails.ephemeral_5m_input_tokens =
16
- anthropic.cacheCreationInputTokens;
34
+ else {
35
+ // AI SDK 4 fields
36
+ if (anthropic.cacheReadInputTokens != null &&
37
+ typeof anthropic.cacheReadInputTokens === "number") {
38
+ inputTokenDetails.cache_read = anthropic.cacheReadInputTokens;
39
+ }
40
+ if (anthropic.cacheCreationInputTokens != null &&
41
+ typeof anthropic.cacheCreationInputTokens === "number") {
42
+ inputTokenDetails.ephemeral_5m_input_tokens =
43
+ anthropic.cacheCreationInputTokens;
44
+ }
17
45
  }
18
46
  return inputTokenDetails;
19
47
  }
@@ -3,14 +3,42 @@ function extractInputTokenDetails(providerMetadata, spanAttributes) {
3
3
  if (providerMetadata.anthropic != null &&
4
4
  typeof providerMetadata.anthropic === "object") {
5
5
  const anthropic = providerMetadata.anthropic;
6
- if (anthropic.cacheReadInputTokens != null &&
7
- typeof anthropic.cacheReadInputTokens === "number") {
8
- inputTokenDetails.cache_read = anthropic.cacheReadInputTokens;
6
+ if (anthropic.usage != null && typeof anthropic.usage === "object") {
7
+ // Raw usage from Anthropic returned in AI SDK 5
8
+ const usage = anthropic.usage;
9
+ if (usage.cache_creation != null &&
10
+ typeof usage.cache_creation === "object") {
11
+ const cacheCreation = usage.cache_creation;
12
+ if (typeof cacheCreation.ephemeral_5m_input_tokens === "number") {
13
+ inputTokenDetails.ephemeral_5m_input_tokens =
14
+ cacheCreation.ephemeral_5m_input_tokens;
15
+ }
16
+ if (typeof cacheCreation.ephemeral_1h_input_tokens === "number") {
17
+ inputTokenDetails.ephemeral_1hr_input_tokens =
18
+ cacheCreation.ephemeral_1h_input_tokens;
19
+ }
20
+ // If cache_creation not returned (no beta header passed),
21
+ // fallback to assuming 5m cache tokens
22
+ }
23
+ else if (typeof usage.cache_creation_input_tokens === "number") {
24
+ inputTokenDetails.ephemeral_5m_input_tokens =
25
+ usage.cache_creation_input_tokens;
26
+ }
27
+ if (typeof usage.cache_read_input_tokens === "number") {
28
+ inputTokenDetails.cache_read = usage.cache_read_input_tokens;
29
+ }
9
30
  }
10
- if (anthropic.cacheCreationInputTokens != null &&
11
- typeof anthropic.cacheCreationInputTokens === "number") {
12
- inputTokenDetails.ephemeral_5m_input_tokens =
13
- anthropic.cacheCreationInputTokens;
31
+ else {
32
+ // AI SDK 4 fields
33
+ if (anthropic.cacheReadInputTokens != null &&
34
+ typeof anthropic.cacheReadInputTokens === "number") {
35
+ inputTokenDetails.cache_read = anthropic.cacheReadInputTokens;
36
+ }
37
+ if (anthropic.cacheCreationInputTokens != null &&
38
+ typeof anthropic.cacheCreationInputTokens === "number") {
39
+ inputTokenDetails.ephemeral_5m_input_tokens =
40
+ anthropic.cacheCreationInputTokens;
41
+ }
14
42
  }
15
43
  return inputTokenDetails;
16
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langsmith",
3
- "version": "0.3.49",
3
+ "version": "0.3.50",
4
4
  "description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
5
5
  "packageManager": "yarn@1.22.19",
6
6
  "files": [
@@ -144,8 +144,8 @@
144
144
  "uuid": "^10.0.0"
145
145
  },
146
146
  "devDependencies": {
147
- "@ai-sdk/anthropic": "2.0.0-beta.8",
148
- "@ai-sdk/openai": "2.0.0-beta.11",
147
+ "@ai-sdk/anthropic": "2.0.0-beta.12",
148
+ "@ai-sdk/openai": "2.0.0-beta.15",
149
149
  "@babel/preset-env": "^7.22.4",
150
150
  "@faker-js/faker": "^8.4.1",
151
151
  "@jest/globals": "^29.5.0",