langsmith 0.5.6 → 0.5.8

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.
Files changed (38) hide show
  1. package/dist/client.cjs +378 -3
  2. package/dist/client.d.ts +202 -2
  3. package/dist/client.js +378 -3
  4. package/dist/evaluation/evaluator.cjs +1 -1
  5. package/dist/evaluation/evaluator.js +2 -2
  6. package/dist/experimental/anthropic/index.cjs +15 -1
  7. package/dist/experimental/anthropic/index.js +15 -1
  8. package/dist/experimental/vercel/index.cjs +23 -5
  9. package/dist/experimental/vercel/index.js +23 -5
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/utils/error.cjs +16 -0
  14. package/dist/utils/error.d.ts +8 -0
  15. package/dist/utils/error.js +15 -0
  16. package/dist/utils/fs.browser.cjs +51 -0
  17. package/dist/utils/fs.browser.d.ts +25 -0
  18. package/dist/utils/fs.browser.js +37 -0
  19. package/dist/utils/fs.cjs +101 -0
  20. package/dist/utils/fs.d.ts +21 -0
  21. package/dist/utils/fs.js +54 -0
  22. package/dist/utils/jestlike/vendor/evaluatedBy.cjs +2 -2
  23. package/dist/utils/jestlike/vendor/evaluatedBy.js +3 -3
  24. package/dist/utils/prompt_cache/index.cjs +31 -5
  25. package/dist/utils/prompt_cache/index.d.ts +3 -2
  26. package/dist/utils/prompt_cache/index.js +31 -5
  27. package/dist/utils/prompts.cjs +4 -3
  28. package/dist/utils/prompts.js +4 -3
  29. package/dist/wrappers/anthropic.cjs +8 -3
  30. package/dist/wrappers/anthropic.d.ts +5 -0
  31. package/dist/wrappers/anthropic.js +8 -4
  32. package/package.json +4 -4
  33. package/dist/utils/prompt_cache/fs.browser.cjs +0 -24
  34. package/dist/utils/prompt_cache/fs.browser.d.ts +0 -16
  35. package/dist/utils/prompt_cache/fs.browser.js +0 -20
  36. package/dist/utils/prompt_cache/fs.cjs +0 -86
  37. package/dist/utils/prompt_cache/fs.d.ts +0 -16
  38. package/dist/utils/prompt_cache/fs.js +0 -49
@@ -1,4 +1,5 @@
1
1
  import { parse as parseVersion } from "semver";
2
+ import { getInvalidPromptIdentifierMsg } from "./error.js";
2
3
  export function isVersionGreaterOrEqual(current_version, target_version) {
3
4
  const current = parseVersion(current_version);
4
5
  const target = parseVersion(target_version);
@@ -13,20 +14,20 @@ export function parsePromptIdentifier(identifier) {
13
14
  identifier.startsWith("/") ||
14
15
  identifier.endsWith("/") ||
15
16
  identifier.split(":").length > 2) {
16
- throw new Error(`Invalid identifier format: ${identifier}`);
17
+ throw new Error(getInvalidPromptIdentifierMsg(identifier));
17
18
  }
18
19
  const [ownerNamePart, commitPart] = identifier.split(":");
19
20
  const commit = commitPart || "latest";
20
21
  if (ownerNamePart.includes("/")) {
21
22
  const [owner, name] = ownerNamePart.split("/", 2);
22
23
  if (!owner || !name) {
23
- throw new Error(`Invalid identifier format: ${identifier}`);
24
+ throw new Error(getInvalidPromptIdentifierMsg(identifier));
24
25
  }
25
26
  return [owner, name, commit];
26
27
  }
27
28
  else {
28
29
  if (!ownerNamePart) {
29
- throw new Error(`Invalid identifier format: ${identifier}`);
30
+ throw new Error(getInvalidPromptIdentifierMsg(identifier));
30
31
  }
31
32
  return ["-", ownerNamePart, commit];
32
33
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.wrapAnthropic = void 0;
4
+ exports.createUsageMetadata = createUsageMetadata;
4
5
  const traceable_js_1 = require("../traceable.cjs");
5
6
  const usage_js_1 = require("../utils/usage.cjs");
6
7
  const TRACED_INVOCATION_KEYS = ["top_k", "top_p", "stream", "thinking"];
@@ -17,14 +18,18 @@ function createUsageMetadata(anthropicUsage) {
17
18
  const outputTokens = typeof anthropicUsage.output_tokens === "number"
18
19
  ? anthropicUsage.output_tokens
19
20
  : 0;
20
- const totalTokens = inputTokens + outputTokens;
21
21
  const inputTokenDetails = (0, usage_js_1.convertAnthropicUsageToInputTokenDetails)(
22
22
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
23
  anthropicUsage);
24
+ // Anthropic cache tokens are ADDITIVE (not subsets of input_tokens like OpenAI).
25
+ // Sum them into input_tokens so the backend cost calculation is correct.
26
+ const cacheTokenSum = Object.values(inputTokenDetails).reduce((sum, v) => sum + (v ?? 0), 0);
27
+ const adjustedInputTokens = inputTokens + cacheTokenSum;
28
+ const adjustedTotalTokens = adjustedInputTokens + outputTokens;
24
29
  return {
25
- input_tokens: inputTokens,
30
+ input_tokens: adjustedInputTokens,
26
31
  output_tokens: outputTokens,
27
- total_tokens: totalTokens,
32
+ total_tokens: adjustedTotalTokens,
28
33
  ...(Object.keys(inputTokenDetails).length > 0 && {
29
34
  input_token_details: inputTokenDetails,
30
35
  }),
@@ -2,6 +2,7 @@ import type Anthropic from "@anthropic-ai/sdk";
2
2
  import type { Stream } from "@anthropic-ai/sdk/streaming";
3
3
  import type { MessageStream } from "@anthropic-ai/sdk/lib/MessageStream";
4
4
  import type { RunTreeConfig } from "../index.js";
5
+ import { KVMap } from "../schemas.js";
5
6
  type ExtraRunTreeConfig = Pick<Partial<RunTreeConfig>, "name" | "metadata" | "tags">;
6
7
  type MessagesNamespace = {
7
8
  create: (...args: any[]) => any;
@@ -31,6 +32,10 @@ type PatchedAnthropicClient<T extends AnthropicType> = T & {
31
32
  };
32
33
  };
33
34
  };
35
+ /**
36
+ * Create usage metadata from Anthropic's token usage format.
37
+ */
38
+ export declare function createUsageMetadata(anthropicUsage: Partial<Anthropic.Messages.Usage>): KVMap | undefined;
34
39
  /**
35
40
  * Wraps an Anthropic client's completion methods, enabling automatic LangSmith
36
41
  * tracing. Method signatures are unchanged, with the exception that you can pass
@@ -4,7 +4,7 @@ const TRACED_INVOCATION_KEYS = ["top_k", "top_p", "stream", "thinking"];
4
4
  /**
5
5
  * Create usage metadata from Anthropic's token usage format.
6
6
  */
7
- function createUsageMetadata(anthropicUsage) {
7
+ export function createUsageMetadata(anthropicUsage) {
8
8
  if (!anthropicUsage) {
9
9
  return undefined;
10
10
  }
@@ -14,14 +14,18 @@ function createUsageMetadata(anthropicUsage) {
14
14
  const outputTokens = typeof anthropicUsage.output_tokens === "number"
15
15
  ? anthropicUsage.output_tokens
16
16
  : 0;
17
- const totalTokens = inputTokens + outputTokens;
18
17
  const inputTokenDetails = convertAnthropicUsageToInputTokenDetails(
19
18
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
19
  anthropicUsage);
20
+ // Anthropic cache tokens are ADDITIVE (not subsets of input_tokens like OpenAI).
21
+ // Sum them into input_tokens so the backend cost calculation is correct.
22
+ const cacheTokenSum = Object.values(inputTokenDetails).reduce((sum, v) => sum + (v ?? 0), 0);
23
+ const adjustedInputTokens = inputTokens + cacheTokenSum;
24
+ const adjustedTotalTokens = adjustedInputTokens + outputTokens;
21
25
  return {
22
- input_tokens: inputTokens,
26
+ input_tokens: adjustedInputTokens,
23
27
  output_tokens: outputTokens,
24
- total_tokens: totalTokens,
28
+ total_tokens: adjustedTotalTokens,
25
29
  ...(Object.keys(inputTokenDetails).length > 0 && {
26
30
  input_token_details: inputTokenDetails,
27
31
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langsmith",
3
- "version": "0.5.6",
3
+ "version": "0.5.8",
4
4
  "description": "Client library to connect to the LangSmith Observability and Evaluation Platform.",
5
5
  "packageManager": "yarn@1.22.19",
6
6
  "files": [
@@ -158,7 +158,7 @@
158
158
  "@ai-sdk/provider": "^3.0.0",
159
159
  "@anthropic-ai/claude-agent-sdk": "^0.2.34",
160
160
  "@google/genai": "^1.29.0",
161
- "@anthropic-ai/sdk": "^0.74.0",
161
+ "@anthropic-ai/sdk": "^0.78.0",
162
162
  "@babel/preset-env": "^7.22.4",
163
163
  "@faker-js/faker": "^8.4.1",
164
164
  "@jest/globals": "^29.5.0",
@@ -167,7 +167,7 @@
167
167
  "@langchain/langgraph": "^0.3.6",
168
168
  "@langchain/openai": "^0.5.16",
169
169
  "@opentelemetry/api": "^1.9.0",
170
- "@opentelemetry/auto-instrumentations-node": "^0.69.0",
170
+ "@opentelemetry/auto-instrumentations-node": "^0.70.1",
171
171
  "@opentelemetry/sdk-node": "^0.212.0",
172
172
  "@opentelemetry/sdk-trace-base": "^2.0.0",
173
173
  "@opentelemetry/sdk-trace-node": "^2.0.0",
@@ -449,6 +449,6 @@
449
449
  "rimraf/**/glob": "10.5.0"
450
450
  },
451
451
  "browser": {
452
- "./dist/utils/prompt_cache/fs.js": "./dist/utils/prompt_cache/fs.browser.js"
452
+ "./dist/utils/fs.js": "./dist/utils/fs.browser.js"
453
453
  }
454
454
  }
@@ -1,24 +0,0 @@
1
- "use strict";
2
- /**
3
- * File system operations for prompt cache (Browser version).
4
- *
5
- * This stub is used in browser builds via the package.json browser field.
6
- * File operations are not supported in browser environments.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.dumpCache = dumpCache;
10
- exports.loadCache = loadCache;
11
- /**
12
- * Dump cache entries to a JSON file.
13
- * @throws Error - Always throws in browser environments.
14
- */
15
- function dumpCache(_filePath, _entries) {
16
- throw new Error("dump() is not supported in browser environments.");
17
- }
18
- /**
19
- * Load cache entries from a JSON file.
20
- * @throws Error - Always throws in browser environments.
21
- */
22
- function loadCache(_filePath) {
23
- throw new Error("load() is not supported in browser environments.");
24
- }
@@ -1,16 +0,0 @@
1
- /**
2
- * File system operations for prompt cache (Browser version).
3
- *
4
- * This stub is used in browser builds via the package.json browser field.
5
- * File operations are not supported in browser environments.
6
- */
7
- /**
8
- * Dump cache entries to a JSON file.
9
- * @throws Error - Always throws in browser environments.
10
- */
11
- export declare function dumpCache(_filePath: string, _entries: Record<string, unknown>): void;
12
- /**
13
- * Load cache entries from a JSON file.
14
- * @throws Error - Always throws in browser environments.
15
- */
16
- export declare function loadCache(_filePath: string): Record<string, unknown> | null;
@@ -1,20 +0,0 @@
1
- /**
2
- * File system operations for prompt cache (Browser version).
3
- *
4
- * This stub is used in browser builds via the package.json browser field.
5
- * File operations are not supported in browser environments.
6
- */
7
- /**
8
- * Dump cache entries to a JSON file.
9
- * @throws Error - Always throws in browser environments.
10
- */
11
- export function dumpCache(_filePath, _entries) {
12
- throw new Error("dump() is not supported in browser environments.");
13
- }
14
- /**
15
- * Load cache entries from a JSON file.
16
- * @throws Error - Always throws in browser environments.
17
- */
18
- export function loadCache(_filePath) {
19
- throw new Error("load() is not supported in browser environments.");
20
- }
@@ -1,86 +0,0 @@
1
- "use strict";
2
- /**
3
- * File system operations for prompt cache (Node.js version).
4
- *
5
- * This file is swapped with prompts_cache_fs.browser.ts for browser builds
6
- * via the package.json browser field.
7
- */
8
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
- if (k2 === undefined) k2 = k;
10
- var desc = Object.getOwnPropertyDescriptor(m, k);
11
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
- desc = { enumerable: true, get: function() { return m[k]; } };
13
- }
14
- Object.defineProperty(o, k2, desc);
15
- }) : (function(o, m, k, k2) {
16
- if (k2 === undefined) k2 = k;
17
- o[k2] = m[k];
18
- }));
19
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
- Object.defineProperty(o, "default", { enumerable: true, value: v });
21
- }) : function(o, v) {
22
- o["default"] = v;
23
- });
24
- var __importStar = (this && this.__importStar) || (function () {
25
- var ownKeys = function(o) {
26
- ownKeys = Object.getOwnPropertyNames || function (o) {
27
- var ar = [];
28
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
- return ar;
30
- };
31
- return ownKeys(o);
32
- };
33
- return function (mod) {
34
- if (mod && mod.__esModule) return mod;
35
- var result = {};
36
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
- __setModuleDefault(result, mod);
38
- return result;
39
- };
40
- })();
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.dumpCache = dumpCache;
43
- exports.loadCache = loadCache;
44
- const fs = __importStar(require("node:fs"));
45
- const path = __importStar(require("node:path"));
46
- /**
47
- * Dump cache entries to a JSON file.
48
- */
49
- function dumpCache(filePath, entries) {
50
- const dir = path.dirname(filePath);
51
- if (!fs.existsSync(dir)) {
52
- fs.mkdirSync(dir, { recursive: true });
53
- }
54
- const data = { entries };
55
- // Atomic write: write to temp file then rename
56
- const tempPath = `${filePath}.tmp`;
57
- try {
58
- fs.writeFileSync(tempPath, JSON.stringify(data, null, 2));
59
- fs.renameSync(tempPath, filePath);
60
- }
61
- catch (e) {
62
- // Clean up temp file on failure
63
- if (fs.existsSync(tempPath)) {
64
- fs.unlinkSync(tempPath);
65
- }
66
- throw e;
67
- }
68
- }
69
- /**
70
- * Load cache entries from a JSON file.
71
- *
72
- * @returns The entries object, or null if file doesn't exist or is invalid.
73
- */
74
- function loadCache(filePath) {
75
- if (!fs.existsSync(filePath)) {
76
- return null;
77
- }
78
- try {
79
- const content = fs.readFileSync(filePath, "utf-8");
80
- const data = JSON.parse(content);
81
- return data.entries ?? null;
82
- }
83
- catch {
84
- return null;
85
- }
86
- }
@@ -1,16 +0,0 @@
1
- /**
2
- * File system operations for prompt cache (Node.js version).
3
- *
4
- * This file is swapped with prompts_cache_fs.browser.ts for browser builds
5
- * via the package.json browser field.
6
- */
7
- /**
8
- * Dump cache entries to a JSON file.
9
- */
10
- export declare function dumpCache(filePath: string, entries: Record<string, unknown>): void;
11
- /**
12
- * Load cache entries from a JSON file.
13
- *
14
- * @returns The entries object, or null if file doesn't exist or is invalid.
15
- */
16
- export declare function loadCache(filePath: string): Record<string, unknown> | null;
@@ -1,49 +0,0 @@
1
- /**
2
- * File system operations for prompt cache (Node.js version).
3
- *
4
- * This file is swapped with prompts_cache_fs.browser.ts for browser builds
5
- * via the package.json browser field.
6
- */
7
- import * as fs from "node:fs";
8
- import * as path from "node:path";
9
- /**
10
- * Dump cache entries to a JSON file.
11
- */
12
- export function dumpCache(filePath, entries) {
13
- const dir = path.dirname(filePath);
14
- if (!fs.existsSync(dir)) {
15
- fs.mkdirSync(dir, { recursive: true });
16
- }
17
- const data = { entries };
18
- // Atomic write: write to temp file then rename
19
- const tempPath = `${filePath}.tmp`;
20
- try {
21
- fs.writeFileSync(tempPath, JSON.stringify(data, null, 2));
22
- fs.renameSync(tempPath, filePath);
23
- }
24
- catch (e) {
25
- // Clean up temp file on failure
26
- if (fs.existsSync(tempPath)) {
27
- fs.unlinkSync(tempPath);
28
- }
29
- throw e;
30
- }
31
- }
32
- /**
33
- * Load cache entries from a JSON file.
34
- *
35
- * @returns The entries object, or null if file doesn't exist or is invalid.
36
- */
37
- export function loadCache(filePath) {
38
- if (!fs.existsSync(filePath)) {
39
- return null;
40
- }
41
- try {
42
- const content = fs.readFileSync(filePath, "utf-8");
43
- const data = JSON.parse(content);
44
- return data.entries ?? null;
45
- }
46
- catch {
47
- return null;
48
- }
49
- }