Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Allow checking tool_calls on any BaseMessage without type casts #7479

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jacoblee93
Copy link
Collaborator

@jacoblee93 jacoblee93 commented Jan 7, 2025

We should no longer need to do casts like this:

const prettyPrint = (message: BaseMessage) => {
  let txt = `[${message.getType()}]: ${message.content}`;
  if (
    isAIMessage(message) && (message as AIMessage)?.tool_calls?.length || 0 > 0
  ) {
    ...
  }
  console.log(txt);
};

Instead:

const prettyPrint = (message: BaseMessage) => {
  let txt = `[${message.getType()}]: ${message.content}`;
  if (message.tool_calls?.length) {
    ...
  }
  console.log(txt);
};

Copy link

vercel bot commented Jan 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 8, 2025 1:36am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Jan 8, 2025 1:36am

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. auto:improvement Medium size change to existing code to handle new use-cases labels Jan 7, 2025
@jacoblee93
Copy link
Collaborator Author

So this will mess up a few automatic type inference situations like this:

// messages is inferred as SystemMessage[]
const messages = [new SystemMessage("foo")];
messages.push(new AIMessage("bar"));
error TS2345: Argument of type 'AIMessage' is not assignable to parameter of type 'HumanMessage'.
  Types of property 'tool_calls' are incompatible.
    Type 'ToolCall[] | undefined' is not assignable to type 'never[] | undefined'.
      Type 'ToolCall[]' is not assignable to type 'never[]'.
        Type 'ToolCall' is not assignable to type 'never'.

I still think it's worth it since you can do const messages: BaseMessage[] = [...] or just use dict (as most people should be doing) syntax and it's just this one type narrowing situation that causes issues. Would love thoughts.

@jacoblee93
Copy link
Collaborator Author

Yeah think I need to find a way to make this non-breaking...

@dosubot dosubot bot added the lgtm PRs that are ready to be merged as-is label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:improvement Medium size change to existing code to handle new use-cases lgtm PRs that are ready to be merged as-is size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants