@langchain/google-common 0.2.10 → 0.2.12

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.
@@ -8,6 +8,7 @@ const messages_1 = require("@langchain/core/messages");
8
8
  const runnables_1 = require("@langchain/core/runnables");
9
9
  const openai_tools_1 = require("@langchain/core/output_parsers/openai_tools");
10
10
  const stream_1 = require("@langchain/core/utils/stream");
11
+ const types_1 = require("@langchain/core/utils/types");
11
12
  const common_js_1 = require("./utils/common.cjs");
12
13
  const connection_js_1 = require("./connection.cjs");
13
14
  const gemini_js_1 = require("./utils/gemini.cjs");
@@ -380,7 +381,7 @@ class ChatGoogleBase extends chat_models_1.BaseChatModel {
380
381
  let functionName = name ?? "extract";
381
382
  let outputParser;
382
383
  let tools;
383
- if (isZodSchema(schema)) {
384
+ if ((0, types_1.isInteropZodSchema)(schema)) {
384
385
  const jsonSchema = (0, zod_to_gemini_parameters_js_1.schemaToGeminiParameters)(schema);
385
386
  tools = [
386
387
  {
@@ -408,10 +409,12 @@ class ChatGoogleBase extends chat_models_1.BaseChatModel {
408
409
  functionName = schema.name;
409
410
  }
410
411
  else {
412
+ // We are providing the schema for *just* the parameters, probably
413
+ const parameters = (0, zod_to_gemini_parameters_js_1.removeAdditionalProperties)(schema);
411
414
  geminiFunctionDefinition = {
412
415
  name: functionName,
413
416
  description: schema.description ?? "",
414
- parameters: schema,
417
+ parameters,
415
418
  };
416
419
  }
417
420
  tools = [
@@ -451,9 +454,3 @@ class ChatGoogleBase extends chat_models_1.BaseChatModel {
451
454
  }
452
455
  }
453
456
  exports.ChatGoogleBase = ChatGoogleBase;
454
- function isZodSchema(
455
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
456
- input) {
457
- // Check for a characteristic method of Zod schemas
458
- return typeof input?.parse === "function";
459
- }
@@ -4,9 +4,9 @@ import { BaseChatModel, LangSmithParams, type BaseChatModelParams } from "@langc
4
4
  import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
5
5
  import { AIMessageChunk } from "@langchain/core/messages";
6
6
  import { BaseLanguageModelInput, StructuredOutputMethodOptions } from "@langchain/core/language_models/base";
7
- import type { z } from "zod";
8
7
  import { Runnable } from "@langchain/core/runnables";
9
8
  import { AsyncCaller } from "@langchain/core/utils/async_caller";
9
+ import { InteropZodType } from "@langchain/core/utils/types";
10
10
  import { GoogleAIBaseLLMInput, GoogleAIModelParams, GoogleAISafetySetting, GoogleConnectionParams, GooglePlatformType, GoogleAIBaseLanguageModelCallOptions, GoogleAIAPI, GoogleAIAPIParams, GoogleSearchToolSetting } from "./types.js";
11
11
  import { AbstractGoogleLLMConnection } from "./connection.js";
12
12
  import { GoogleAbstractedClient } from "./auth.js";
@@ -74,8 +74,8 @@ export declare abstract class ChatGoogleBase<AuthOptions> extends BaseChatModel<
74
74
  _streamResponseChunks(_messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
75
75
  /** @ignore */
76
76
  _combineLLMOutput(): never[];
77
- withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
78
- withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
77
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
78
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
79
79
  raw: BaseMessage;
80
80
  parsed: RunOutput;
81
81
  }>;
@@ -5,12 +5,13 @@ import { AIMessageChunk } from "@langchain/core/messages";
5
5
  import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
6
6
  import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";
7
7
  import { concat } from "@langchain/core/utils/stream";
8
+ import { isInteropZodSchema, } from "@langchain/core/utils/types";
8
9
  import { convertToGeminiTools, copyAIModelParams, copyAndValidateModelParamsInto, } from "./utils/common.js";
9
10
  import { AbstractGoogleLLMConnection } from "./connection.js";
10
11
  import { DefaultGeminiSafetyHandler, getGeminiAPI } from "./utils/gemini.js";
11
12
  import { ApiKeyGoogleAuth } from "./auth.js";
12
13
  import { ensureParams } from "./utils/failed_handler.js";
13
- import { schemaToGeminiParameters } from "./utils/zod_to_gemini_parameters.js";
14
+ import { removeAdditionalProperties, schemaToGeminiParameters, } from "./utils/zod_to_gemini_parameters.js";
14
15
  export class ChatConnection extends AbstractGoogleLLMConnection {
15
16
  constructor(fields, caller, client, streaming) {
16
17
  super(fields, caller, client, streaming);
@@ -376,7 +377,7 @@ export class ChatGoogleBase extends BaseChatModel {
376
377
  let functionName = name ?? "extract";
377
378
  let outputParser;
378
379
  let tools;
379
- if (isZodSchema(schema)) {
380
+ if (isInteropZodSchema(schema)) {
380
381
  const jsonSchema = schemaToGeminiParameters(schema);
381
382
  tools = [
382
383
  {
@@ -404,10 +405,12 @@ export class ChatGoogleBase extends BaseChatModel {
404
405
  functionName = schema.name;
405
406
  }
406
407
  else {
408
+ // We are providing the schema for *just* the parameters, probably
409
+ const parameters = removeAdditionalProperties(schema);
407
410
  geminiFunctionDefinition = {
408
411
  name: functionName,
409
412
  description: schema.description ?? "",
410
- parameters: schema,
413
+ parameters,
411
414
  };
412
415
  }
413
416
  tools = [
@@ -446,9 +449,3 @@ export class ChatGoogleBase extends BaseChatModel {
446
449
  });
447
450
  }
448
451
  }
449
- function isZodSchema(
450
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
451
- input) {
452
- // Check for a characteristic method of Zod schemas
453
- return typeof input?.parse === "function";
454
- }
@@ -154,6 +154,7 @@ export interface AnthropicUsage {
154
154
  output_tokens: number;
155
155
  cache_creation_input_tokens: number | null;
156
156
  cache_creation_output_tokens: number | null;
157
+ cache_read_input_tokens: number | null;
157
158
  }
158
159
  export type AnthropicResponseData = AnthropicResponseMessage | AnthropicStreamBaseEvent;
159
160
  export interface AnthropicResponseMessage {
@@ -116,13 +116,23 @@ function getAnthropicAPI(config) {
116
116
  };
117
117
  return newAIMessageChunk(ret);
118
118
  }
119
- function messageToGenerationInfo(message) {
119
+ function messageToUsageMetadata(message) {
120
120
  const usage = message?.usage;
121
+ const inputTokens = usage?.input_tokens ?? 0;
122
+ const outputTokens = usage?.output_tokens ?? 0;
121
123
  const usageMetadata = {
122
- input_tokens: usage?.input_tokens ?? 0,
123
- output_tokens: usage?.output_tokens ?? 0,
124
- total_tokens: (usage?.input_tokens ?? 0) + (usage?.output_tokens ?? 0),
124
+ input_tokens: inputTokens,
125
+ output_tokens: outputTokens,
126
+ total_tokens: inputTokens + outputTokens,
127
+ input_token_details: {
128
+ cache_read: usage?.cache_read_input_tokens ?? 0,
129
+ cache_creation: usage?.cache_creation_input_tokens ?? 0,
130
+ },
125
131
  };
132
+ return usageMetadata;
133
+ }
134
+ function messageToGenerationInfo(message) {
135
+ const usageMetadata = messageToUsageMetadata(message);
126
136
  return {
127
137
  usage_metadata: usageMetadata,
128
138
  finish_reason: message.stop_reason,
@@ -113,13 +113,23 @@ export function getAnthropicAPI(config) {
113
113
  };
114
114
  return newAIMessageChunk(ret);
115
115
  }
116
- function messageToGenerationInfo(message) {
116
+ function messageToUsageMetadata(message) {
117
117
  const usage = message?.usage;
118
+ const inputTokens = usage?.input_tokens ?? 0;
119
+ const outputTokens = usage?.output_tokens ?? 0;
118
120
  const usageMetadata = {
119
- input_tokens: usage?.input_tokens ?? 0,
120
- output_tokens: usage?.output_tokens ?? 0,
121
- total_tokens: (usage?.input_tokens ?? 0) + (usage?.output_tokens ?? 0),
121
+ input_tokens: inputTokens,
122
+ output_tokens: outputTokens,
123
+ total_tokens: inputTokens + outputTokens,
124
+ input_token_details: {
125
+ cache_read: usage?.cache_read_input_tokens ?? 0,
126
+ cache_creation: usage?.cache_creation_input_tokens ?? 0,
127
+ },
122
128
  };
129
+ return usageMetadata;
130
+ }
131
+ function messageToGenerationInfo(message) {
132
+ const usageMetadata = messageToUsageMetadata(message);
123
133
  return {
124
134
  usage_metadata: usageMetadata,
125
135
  finish_reason: message.stop_reason,
@@ -1,9 +1,45 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-unused-vars */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.jsonSchemaToGeminiParameters = exports.schemaToGeminiParameters = exports.removeAdditionalProperties = void 0;
4
+ exports.jsonSchemaToGeminiParameters = exports.schemaToGeminiParameters = exports.removeAdditionalProperties = exports.adjustObjectType = void 0;
5
5
  const types_1 = require("@langchain/core/utils/types");
6
- const zod_to_json_schema_1 = require("zod-to-json-schema");
6
+ const json_schema_1 = require("@langchain/core/utils/json_schema");
7
+ /* eslint-disable no-param-reassign */
8
+ function adjustObjectType(
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ obj
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ ) {
13
+ if (!Array.isArray(obj.type)) {
14
+ return obj;
15
+ }
16
+ const len = obj.type.length;
17
+ const nullIndex = obj.type.indexOf("null");
18
+ if (len === 2 && nullIndex >= 0) {
19
+ // There are only two values set for the type, and one of them is "null".
20
+ // Set the type to the other one and set nullable to true.
21
+ const typeIndex = nullIndex === 0 ? 1 : 0;
22
+ obj.type = obj.type[typeIndex];
23
+ obj.nullable = true;
24
+ }
25
+ else if (len === 1 && nullIndex === 0) {
26
+ // This is nullable only without a type, which doesn't
27
+ // make sense for Gemini
28
+ throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
29
+ }
30
+ else if (len === 1) {
31
+ // Although an array, it has only one value.
32
+ // So set it to the string to match what Gemini expects.
33
+ obj.type = obj?.type[0];
34
+ }
35
+ else {
36
+ // Anything else could be a union type, so reject it.
37
+ throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
38
+ }
39
+ return obj;
40
+ }
41
+ exports.adjustObjectType = adjustObjectType;
42
+ /* eslint-enable no-param-reassign */
7
43
  function removeAdditionalProperties(
8
44
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
45
  obj) {
@@ -12,31 +48,7 @@ obj) {
12
48
  if ("additionalProperties" in newObj) {
13
49
  delete newObj.additionalProperties;
14
50
  }
15
- if (Array.isArray(obj.type)) {
16
- const len = obj.type.length;
17
- const nullIndex = obj.type.indexOf("null");
18
- if (len === 2 && nullIndex >= 0) {
19
- // There are only two values set for the type, and one of them is "null".
20
- // Set the type to the other one and set nullable to true.
21
- const typeIndex = nullIndex === 0 ? 1 : 0;
22
- newObj.type = obj.type[typeIndex];
23
- newObj.nullable = true;
24
- }
25
- else if (len === 1 && nullIndex === 0) {
26
- // This is nullable only without a type, which doesn't
27
- // make sense for Gemini
28
- throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
29
- }
30
- else if (len === 1) {
31
- // Although an array, it has only one value.
32
- // So set it to the string to match what Gemini expects.
33
- newObj.type = obj?.type[0];
34
- }
35
- else {
36
- // Anything else could be a union type, so reject it.
37
- throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
38
- }
39
- }
51
+ adjustObjectType(newObj);
40
52
  for (const key in newObj) {
41
53
  if (key in newObj) {
42
54
  if (Array.isArray(newObj[key])) {
@@ -57,7 +69,7 @@ function schemaToGeminiParameters(schema) {
57
69
  // attributes, so we need to explicitly remove them.
58
70
  // Zod sometimes also makes an array of type (because of .nullish()),
59
71
  // which needs cleaning up.
60
- const jsonSchema = removeAdditionalProperties((0, types_1.isZodSchema)(schema) ? (0, zod_to_json_schema_1.zodToJsonSchema)(schema) : schema);
72
+ const jsonSchema = removeAdditionalProperties((0, types_1.isInteropZodSchema)(schema) ? (0, json_schema_1.toJsonSchema)(schema) : schema);
61
73
  const { $schema, ...rest } = jsonSchema;
62
74
  return rest;
63
75
  }
@@ -1,6 +1,7 @@
1
- import type { z } from "zod";
2
- import { type JsonSchema7Type } from "zod-to-json-schema";
1
+ import { InteropZodType } from "@langchain/core/utils/types";
2
+ import { type JsonSchema7Type } from "@langchain/core/utils/json_schema";
3
3
  import { GeminiFunctionSchema, GeminiJsonSchema } from "../types.js";
4
+ export declare function adjustObjectType(obj: Record<string, any>): Record<string, any>;
4
5
  export declare function removeAdditionalProperties(obj: Record<string, any>): GeminiJsonSchema;
5
- export declare function schemaToGeminiParameters<RunOutput extends Record<string, any> = Record<string, any>>(schema: z.ZodType<RunOutput> | z.ZodEffects<z.ZodType<RunOutput>> | JsonSchema7Type): GeminiFunctionSchema;
6
+ export declare function schemaToGeminiParameters<RunOutput extends Record<string, any> = Record<string, any>>(schema: InteropZodType<RunOutput> | JsonSchema7Type): GeminiFunctionSchema;
6
7
  export declare function jsonSchemaToGeminiParameters(schema: Record<string, any>): GeminiFunctionSchema;
@@ -1,6 +1,41 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
- import { isZodSchema } from "@langchain/core/utils/types";
3
- import { zodToJsonSchema } from "zod-to-json-schema";
2
+ import { isInteropZodSchema, } from "@langchain/core/utils/types";
3
+ import { toJsonSchema, } from "@langchain/core/utils/json_schema";
4
+ /* eslint-disable no-param-reassign */
5
+ export function adjustObjectType(
6
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
+ obj
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ ) {
10
+ if (!Array.isArray(obj.type)) {
11
+ return obj;
12
+ }
13
+ const len = obj.type.length;
14
+ const nullIndex = obj.type.indexOf("null");
15
+ if (len === 2 && nullIndex >= 0) {
16
+ // There are only two values set for the type, and one of them is "null".
17
+ // Set the type to the other one and set nullable to true.
18
+ const typeIndex = nullIndex === 0 ? 1 : 0;
19
+ obj.type = obj.type[typeIndex];
20
+ obj.nullable = true;
21
+ }
22
+ else if (len === 1 && nullIndex === 0) {
23
+ // This is nullable only without a type, which doesn't
24
+ // make sense for Gemini
25
+ throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
26
+ }
27
+ else if (len === 1) {
28
+ // Although an array, it has only one value.
29
+ // So set it to the string to match what Gemini expects.
30
+ obj.type = obj?.type[0];
31
+ }
32
+ else {
33
+ // Anything else could be a union type, so reject it.
34
+ throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
35
+ }
36
+ return obj;
37
+ }
38
+ /* eslint-enable no-param-reassign */
4
39
  export function removeAdditionalProperties(
5
40
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
41
  obj) {
@@ -9,31 +44,7 @@ obj) {
9
44
  if ("additionalProperties" in newObj) {
10
45
  delete newObj.additionalProperties;
11
46
  }
12
- if (Array.isArray(obj.type)) {
13
- const len = obj.type.length;
14
- const nullIndex = obj.type.indexOf("null");
15
- if (len === 2 && nullIndex >= 0) {
16
- // There are only two values set for the type, and one of them is "null".
17
- // Set the type to the other one and set nullable to true.
18
- const typeIndex = nullIndex === 0 ? 1 : 0;
19
- newObj.type = obj.type[typeIndex];
20
- newObj.nullable = true;
21
- }
22
- else if (len === 1 && nullIndex === 0) {
23
- // This is nullable only without a type, which doesn't
24
- // make sense for Gemini
25
- throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
26
- }
27
- else if (len === 1) {
28
- // Although an array, it has only one value.
29
- // So set it to the string to match what Gemini expects.
30
- newObj.type = obj?.type[0];
31
- }
32
- else {
33
- // Anything else could be a union type, so reject it.
34
- throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
35
- }
36
- }
47
+ adjustObjectType(newObj);
37
48
  for (const key in newObj) {
38
49
  if (key in newObj) {
39
50
  if (Array.isArray(newObj[key])) {
@@ -53,7 +64,7 @@ export function schemaToGeminiParameters(schema) {
53
64
  // attributes, so we need to explicitly remove them.
54
65
  // Zod sometimes also makes an array of type (because of .nullish()),
55
66
  // which needs cleaning up.
56
- const jsonSchema = removeAdditionalProperties(isZodSchema(schema) ? zodToJsonSchema(schema) : schema);
67
+ const jsonSchema = removeAdditionalProperties(isInteropZodSchema(schema) ? toJsonSchema(schema) : schema);
57
68
  const { $schema, ...rest } = jsonSchema;
58
69
  return rest;
59
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-common",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {
@@ -32,15 +32,14 @@
32
32
  "author": "LangChain",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "uuid": "^10.0.0",
36
- "zod-to-json-schema": "^3.22.4"
35
+ "uuid": "^10.0.0"
37
36
  },
38
37
  "peerDependencies": {
39
- "@langchain/core": ">=0.3.55 <0.4.0"
38
+ "@langchain/core": ">=0.3.58 <0.4.0"
40
39
  },
41
40
  "devDependencies": {
42
41
  "@jest/globals": "^29.5.0",
43
- "@langchain/core": "0.3.57",
42
+ "@langchain/core": "workspace:*",
44
43
  "@langchain/scripts": ">=0.1.0 <0.2.0",
45
44
  "@swc/core": "^1.3.90",
46
45
  "@swc/jest": "^0.2.29",
@@ -138,4 +137,4 @@
138
137
  "experimental/utils/media_core.d.ts",
139
138
  "experimental/utils/media_core.d.cts"
140
139
  ]
141
- }
140
+ }