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

core[minor], integrations...[patch]: Support ToolCall as Tool input and ToolMessage as Tool output #24038

Merged
merged 33 commits into from
Jul 11, 2024

Conversation

baskaryan
Copy link
Collaborator

@baskaryan baskaryan commented Jul 9, 2024

Changes:

  • ToolCall, InvalidToolCall and ToolCallChunk can all accept a "type" parameter now
  • LLM integration packages add "type" to all the above
  • Tool supports ToolCall inputs that have "type" specified
  • Tool outputs ToolMessage when a ToolCall is passed as input
  • Tools can separately specify ToolMessage.content and ToolMessage.raw_output
  • Tools emit events for validation errors (using on_tool_error and on_tool_end)

Example:

@tool("structured_api", response_format="content_and_raw_output")
def _mock_structured_tool_with_raw_output(
    arg1: int, arg2: bool, arg3: Optional[dict] = None
) -> Tuple[str, dict]:
    """A Structured Tool"""
    return f"{arg1} {arg2}", {"arg1": arg1, "arg2": arg2, "arg3": arg3}


def test_tool_call_input_tool_message_with_raw_output() -> None:
    tool_call: Dict = {
        "name": "structured_api",
        "args": {"arg1": 1, "arg2": True, "arg3": {"img": "base64string..."}},
        "id": "123",
        "type": "tool_call",
    }
    expected = ToolMessage("1 True", raw_output=tool_call["args"], tool_call_id="123")
    tool = _mock_structured_tool_with_raw_output
    actual = tool.invoke(tool_call)
    assert actual == expected

    tool_call.pop("type")
    with pytest.raises(ValidationError):
        tool.invoke(tool_call)

    actual_content = tool.invoke(tool_call["args"])
    assert actual_content == expected.content

@efriis efriis added the partner label Jul 9, 2024
@efriis efriis self-assigned this Jul 9, 2024
Copy link

vercel bot commented Jul 9, 2024

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

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jul 11, 2024 8:56pm

@baskaryan baskaryan marked this pull request as ready for review July 10, 2024 01:03
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. Ɑ: core Related to langchain-core 🤖:improvement Medium size change to existing code to handle new use-cases labels Jul 10, 2024
@baskaryan
Copy link
Collaborator Author

corresponding ToolNode update langchain-ai/langgraph#977

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jul 10, 2024
@eyurtsev eyurtsev self-assigned this Jul 10, 2024
Copy link
Collaborator

@eyurtsev eyurtsev left a comment

Choose a reason for hiding this comment

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

partial review

libs/core/langchain_core/tools.py Outdated Show resolved Hide resolved
libs/core/langchain_core/messages/base.py Outdated Show resolved Hide resolved
libs/core/langchain_core/tools.py Outdated Show resolved Hide resolved
libs/core/langchain_core/tools.py Outdated Show resolved Hide resolved
type: NotRequired[Literal["tool_call_chunk"]]


def tool_call_chunk(
Copy link
Collaborator

Choose a reason for hiding this comment

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

why is this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

convenience / to make sure ppl don't mess up the type

Copy link
Collaborator

Choose a reason for hiding this comment

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

How is that easier than using ToolCallChunk? Is it validating that there are no extra arguments or typos in the arguments?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

type is hard coded

@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Jul 10, 2024
@eyurtsev eyurtsev self-requested a review July 10, 2024 19:55
self,
tool_input: Union[str, Dict],
) -> Union[str, Dict[str, Any]]:
def _parse_input(self, tool_input: Union[str, Dict]) -> Union[str, Dict[str, Any]]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

What can tool input be here? (if you know of the top of you head and have time could be good to update the doc-string)

libs/core/langchain_core/tools.py Show resolved Hide resolved
@eyurtsev eyurtsev added the 08 RFC Request for Comment. Solicit design input from other contributors label Jul 10, 2024
@baskaryan baskaryan added the needs test PR needs to be updated with tests label Jul 10, 2024
@baskaryan baskaryan changed the title rfc: sep methd for tool message res core[minor]: Support ToolCall as Tool input and ToolMessage as Tool output Jul 10, 2024
@baskaryan baskaryan changed the title core[minor]: Support ToolCall as Tool input and ToolMessage as Tool output core[minor], integrations...[patch]: Support ToolCall as Tool input and ToolMessage as Tool output Jul 10, 2024
@baskaryan baskaryan added the needs documentation PR needs to be updated with documentation label Jul 10, 2024
@ccurme
Copy link
Collaborator

ccurme commented Jul 11, 2024

Integration tests look good: https://github.com/langchain-ai/langchain/actions/runs/9893304257

run_id=run_id,
# Inputs by definition should always be dicts.
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we say they're by definition dicts (and can't be str)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

copy/pasted from elsewhere, cc @eyurtsev

Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment is originally from observing this:

@tool
def foo(x: str):
   return x

foo.invoke({'x': 'hello'})

It's possible that str is still supported on its own. I don't know what we want to do in that case -- since we have pretty odd behavior when it comes to langsmith API requiring auto upgrades to dicts

"id": rtc.get("id"),
"index": rtc.get("index"),
}
create_tool_call_chunk(
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are places we've duplicated logic (e.g., in astream) that may also need to be updated.

What will the behavior be if a tool call is missing a type? Possible there will be issues operating on old serialized tool calls (ok if so-- helps to know in case bugs pop up)? And do we need to update other integrations (e.g., community and other repos)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

only thing that depends on type atm is Tools when they're taking a ToolCall (which is a new feature, you couldn't pass in ToolCalls at all before)

@baskaryan baskaryan merged commit 5fd1e67 into master Jul 11, 2024
241 checks passed
@baskaryan baskaryan deleted the bagatur/rfc_sep_method_for_tool_res branch July 11, 2024 21:54
baskaryan added a commit that referenced this pull request Jul 11, 2024
Relies on #24038

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
08 RFC Request for Comment. Solicit design input from other contributors Ɑ: core Related to langchain-core 🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. needs documentation PR needs to be updated with documentation needs test PR needs to be updated with tests partner size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants