-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(@langchain/core): support tools with custom state or context provided by ToolRuntime #9295
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
base: main
Are you sure you want to change the base?
Conversation
…vided by ToolRuntime
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few thoughts:
- for what it's worth, idk if it's totally necessary that we have an addressable ToolRuntime type like python does considering the problem space is so different. Important part is that we can get context about the agent loop from within a tool
- First gut reaction to introducing a conditional type on tools is it seems like a band aid
Some ideas that I had:
- Always pass the context to the tool parameters, but only make it type safe if you explicitly cast the second arg to be a type containing the context info
- introduce a utility similar to getCurrentTaskInput that looks at hidden parameters on call options (e.g. getAgentContext or something like that)
That may work for
IMHO this doesn't feel like clean DX, bc it would require to import this function from somewhere and it also wouldn't be clear what the function does, except of "magically" giving you the value from somewhere.
I would argue the other way: introducing things like |
|
I think the thing I'm more concerned about is how this would be leaking concepts from Some other Q's while I'm thinking about it:
|
Great question: I suggest to parse the schema with provided context and state and throw a meaningful error saying that the tool was used within an agent with different stateSchema/contextSchema definition.
No, we are staying backwards compatible: if the user doesn't provide |
|
A few notes:
|
Historically LangChain tools don't have a concept of
stateandcontext. This is only required when used within LangGraph or withcreateAgent. This PR attempts to make toolsstateandcontextaware. It adds support for automatically injecting runtime context into tool functions whenstateSchemaorcontextSchemaare provided. This enables tools to access graph state, runtime context, store, and other runtime information without requiring manual parameter passing.Trade offs / Alternatives
I think this is the best approach given that the
toolsfunction has already several overloads. What I don't like here is that the 2nd argument completely changes now but I think this is fine given type support.Alternatively I was considering implementing an
agenttoolfunction that provides desired / simpler typing of this but it seems like to far off what we do in Python.Motivation
Currently, tools can only access their input parameters and configuration. However, in many agent workflows, tools need access to:
This change aligns the TypeScript implementation with the Python
ToolRuntimefeature, providing a consistent developer experience across both languages.Usage Example
Todo
I wanted to get some feedback on this before continuing. Missing pieces are:
Backwards Compatibility
✅ Fully backwards compatible - Existing tools without
stateSchemaorcontextSchemacontinue to work exactly as before. The runtime parameter is only added when schemas are explicitly provided.