-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: Validate Agent's Tool <-> State mapping #10257
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
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Pull Request Test Coverage Report for Build 20305608747Details
💛 - Coveralls |
| # Validate tools' state mappings against state schema | ||
| if tools: | ||
| self._validate_tools_against_state_schema(tools) | ||
|
|
||
| self.tool_invoker_kwargs = tool_invoker_kwargs | ||
| self._tool_invoker = None | ||
| if self.tools: |
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.
We already have a self.tools if check on line 234. Could we move this validation step in there?
Also does this validation work when using MCPToolset?
| tool_invoker_inputs=tool_invoker_inputs, | ||
| ) | ||
|
|
||
| def _validate_tools_against_state_schema(self, tools: ToolsType) -> None: |
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.
I think it would make more sense to put this as util method in ./state/state_utils.py since this purely a tool to state check and doesn't really involve other parts of the Agent. Could we move it in there as a standalone method?
| # Validate outputs_to_state keys exist in state schema (only when no handler) | ||
| if tool.outputs_to_state: | ||
| for state_key, config in tool.outputs_to_state.items(): | ||
| if config.get("handler") is None and state_key not in self.state_schema: |
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.
Why does config.get("handler") need to be None to perform the state_key check?
| f"2. Or provide a handler to create the key dynamically:\n" | ||
| f" Tool(..., outputs_to_state={{'{state_key}':" | ||
| f" {{'source': 'output', 'handler': your_handler}}}})" |
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.
This isn't correct. The handler does not create the state key dynamically. It handles how the merge operation should occur for an existing state key. For example, if we are writing to the state key documents: list[Document], the handler determines how the new value being written is merged with the existing value in state.
So the handler doesn't affect the key mapping at all.
|
Closing to restart from scratch on this one @sjrl |
Why:
Prevents runtime failures by validating tool state mappings against the Agent's state schema at initialization and runtime. Catches misconfigured
inputs_from_stateandoutputs_to_stateearly with clear error messages thus improving devXWhat:
_validate_tools_against_state_schemamethod to validate state key referencesinputs_from_statekeys exist in the Agent's state schemaoutputs_to_statekeys exist in state schema (unless a handler is provided, which can create keys dynamically)_select_toolsHow can it be used:
How did you test it:
ValueErroris raised with appropriate error messagesNotes for the reviewer:
outputs_to_statewith a handler bypasses validation since handlers can create keys dynamically; ensure this exception is correct