Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Oct 25, 2025

Important

Update Langchain to v1, refactor CallbackHandler for new message types, and adjust tests accordingly.

  • Dependencies:
    • Update @langchain/core to ^1.0.1, @langchain/langgraph to ^1.0.1, and @langchain/openai to ^1.0.0 in package.json.
  • CallbackHandler:
    • Replace instanceof checks with getType() for message type in CallbackHandler.
    • Modify extractChatMessageContent() to handle new message types.
  • Tests:
    • Update langchain.e3e.test.ts to reflect changes in message handling and tool call structure.
    • Adjust expectations for tool call structure in langchain.e3e.test.ts.

This description was created by Ellipsis for 5c0925c. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Overview

Updated On: 2025-10-25 17:45:16 UTC

Greptile Summary

This PR upgrades LangChain dependencies from v0.x to v1.x (@langchain/core, @langchain/langgraph, and @langchain/openai).

Key changes:

  • Replaces instanceof checks with .getType() method calls for message type detection (e.g., message instanceof HumanMessagemessage.getType() === "human")
  • Removes unused imports (ChatMessage, FunctionMessage, HumanMessage, SystemMessage, ToolMessage)
  • Adds additional handling for tool_calls from message.additional_kwargs
  • Updates test assertions to match v1 tool call structure where tool calls are nested under a function property
  • Removes instanceof BaseMessage check before type casting in handleLLMEnd

Issues found:

  • Potential tool_calls overwrite issue in extractChatMessageContent (lines 870-876)
  • Debug console.log left in test file

Confidence Score: 3/5

  • This PR has a logic issue that could cause tool_calls to be incorrectly overwritten in certain scenarios
  • The migration from v0.x to v1.x appears mostly sound, but there's a critical logic bug where additional_kwargs.tool_calls unconditionally overwrites message.tool_calls if both exist. The removed instanceof BaseMessage check could also cause type safety issues. The debug console.log is a minor style issue.
  • Pay close attention to packages/langchain/src/CallbackHandler.ts lines 870-876 where tool_calls handling may overwrite valid data

Important Files Changed

File Analysis

Filename Score Overview
packages/langchain/src/CallbackHandler.ts 3/5 Replaces instanceof checks with .getType() calls for v1 compatibility; adds tool_calls handling from additional_kwargs but has potential overwrite issue on line 870-876
tests/e2e/langchain.e3e.test.ts 4/5 Updates test assertions for v1 tool_calls structure (nested under function property); includes debug console.log that should be removed

Sequence Diagram

sequenceDiagram
    participant LC as LangChain v1
    participant CB as CallbackHandler
    participant LF as Langfuse
    
    LC->>CB: handleChatModelStart(messages)
    CB->>CB: extractChatMessageContent(message)
    Note over CB: Uses message.getType() instead of instanceof
    CB->>CB: Check message type (human/ai/system/tool/function)
    
    alt AI Message with tool_calls
        CB->>CB: Check message.tool_calls exists
        CB->>CB: Set response.tool_calls from message.tool_calls
        CB->>CB: Check additional_kwargs.tool_calls
        Note over CB: ⚠️ May overwrite existing tool_calls
        CB->>CB: Set response.tool_calls from additional_kwargs
    end
    
    CB->>LF: startObservation(extracted messages)
    
    LC->>CB: handleLLMEnd(output)
    CB->>CB: Extract lastResponse.message
    Note over CB: Cast to BaseMessage without instanceof check
    CB->>CB: extractChatMessageContent(message)
    CB->>LF: Update observation with output
    
    Note over LC,LF: Tool calls structure changed: <br/>v0.x: tool_calls[0].name<br/>v1.x: tool_calls[0].function.name
Loading

@vercel
Copy link

vercel bot commented Oct 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
langfuse-js Ready Ready Preview Oct 25, 2025 5:39pm

@hassiebp hassiebp marked this pull request as ready for review October 25, 2025 17:41
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@hassiebp hassiebp merged commit 2d80d09 into main Oct 26, 2025
10 of 12 checks passed
@hassiebp hassiebp deleted the add-langchain-v1-support branch October 26, 2025 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants