@langchain/openai 0.0.21 → 0.0.22

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.
@@ -649,9 +649,15 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
649
649
  count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
650
650
  }
651
651
  if (openAIMessage.additional_kwargs.function_call?.arguments) {
652
- count += await this.getNumTokens(
653
- // Remove newlines and spaces
654
- JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
652
+ try {
653
+ count += await this.getNumTokens(
654
+ // Remove newlines and spaces
655
+ JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
656
+ }
657
+ catch (error) {
658
+ console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
659
+ count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
660
+ }
655
661
  }
656
662
  totalCount += count;
657
663
  return count;
@@ -645,9 +645,15 @@ export class ChatOpenAI extends BaseChatModel {
645
645
  count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
646
646
  }
647
647
  if (openAIMessage.additional_kwargs.function_call?.arguments) {
648
- count += await this.getNumTokens(
649
- // Remove newlines and spaces
650
- JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
648
+ try {
649
+ count += await this.getNumTokens(
650
+ // Remove newlines and spaces
651
+ JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
652
+ }
653
+ catch (error) {
654
+ console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
655
+ count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
656
+ }
651
657
  }
652
658
  totalCount += count;
653
659
  return count;
@@ -521,6 +521,7 @@ test("Test ChatOpenAI token usage reporting for streaming function calls", async
521
521
  },
522
522
  ],
523
523
  }).bind({
524
+ seed: 42,
524
525
  functions: [extractionFunctionSchema],
525
526
  function_call: { name: "extractor" },
526
527
  });
@@ -543,6 +544,7 @@ test("Test ChatOpenAI token usage reporting for streaming function calls", async
543
544
  },
544
545
  ],
545
546
  }).bind({
547
+ seed: 42,
546
548
  functions: [extractionFunctionSchema],
547
549
  function_call: { name: "extractor" },
548
550
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.21",
3
+ "version": "0.0.22",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {