@posthog/ai 5.2.1 → 5.2.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 5.2.2
2
+
3
+ - Add support for parsing tool calls from reasoning models in LangChain by converting the tool call format to the expected shape
4
+
1
5
  # 5.2.1
2
6
 
3
7
  - Fix crash when importing @posthog/ai with OpenAI SDK v5.x by deferring access to Chat, Completions, and Responses classes until runtime
package/lib/index.cjs CHANGED
@@ -2510,10 +2510,19 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
2510
2510
  }
2511
2511
  return undefined;
2512
2512
  }
2513
+ _convertLcToolCallsToOai(toolCalls) {
2514
+ return toolCalls.map(toolCall => ({
2515
+ type: 'function',
2516
+ id: toolCall.id,
2517
+ function: {
2518
+ name: toolCall.name,
2519
+ arguments: JSON.stringify(toolCall.args)
2520
+ }
2521
+ }));
2522
+ }
2513
2523
  _convertMessageToDict(message) {
2514
2524
  let messageDict = {};
2515
- // Check the _getType() method or type property instead of instanceof
2516
- const messageType = message._getType?.() || message.type;
2525
+ const messageType = message.getType();
2517
2526
  switch (messageType) {
2518
2527
  case 'human':
2519
2528
  messageDict = {
@@ -2526,6 +2535,9 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
2526
2535
  role: 'assistant',
2527
2536
  content: message.content
2528
2537
  };
2538
+ if (message.tool_calls) {
2539
+ messageDict.tool_calls = this._convertLcToolCallsToOai(message.tool_calls);
2540
+ }
2529
2541
  break;
2530
2542
  case 'system':
2531
2543
  messageDict = {
@@ -2547,9 +2559,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
2547
2559
  break;
2548
2560
  default:
2549
2561
  messageDict = {
2550
- role: messageType || 'unknown',
2562
+ role: messageType,
2551
2563
  content: String(message.content)
2552
2564
  };
2565
+ break;
2553
2566
  }
2554
2567
  if (message.additional_kwargs) {
2555
2568
  messageDict = {