feat: derive Tool description and parameters from function#12023
Draft
gaurav0107 wants to merge 1 commit into
Draft
feat: derive Tool description and parameters from function#12023gaurav0107 wants to merge 1 commit into
gaurav0107 wants to merge 1 commit into
Conversation
Allow `Tool` to auto-derive its `description` and `parameters` from the tool's `function` (or `async_function`) when they are not provided explicitly, reusing the shared `_create_tool_parameter_schema` helper (the same logic used by `create_tool_from_function`). This enables constructing a tool directly from a typed function, e.g. `Tool(name="get_weather", function=get_weather)`: the description comes from the function docstring and the parameters JSON schema is generated from its type hints. Passing `description` and `parameters` explicitly keeps the previous behavior unchanged.
|
@gaurav0107 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
parameteranddescriptioncreation fromcreate_tool_from_functionintoTool#9006Proposed Changes:
Toolcurrently requires an explicitname,description, and JSON-schemaparameters. The schema-generation logic that turns a typed function intoparameterslived only insidecreate_tool_from_function, so there was no way tobuild a
Tooldirectly from a function without going through the separatecreate_tool_from_function/@toolhelpers — awkward when constructing tools fromconfig/YAML that already carry a callable.
This PR makes
descriptionandparametersoptional and derives them from the tool'sfunction(orasync_function) when they are not provided, matching the API themaintainers outlined in the issue:
Implementation:
create_tool_from_functioninto ashared, reusable
_create_tool_parameter_schema(function, inputs_from_state)helper(no behavior change to
create_tool_from_function).Tool.descriptionandTool.parametersoptional (Nonedefault). In__post_init__, any missing field is derived from the available callable via a new_derive_description_and_parameters()method (descriptionfrom the functiondocstring,
parametersfrom_create_tool_parameter_schema). The helper is importedlazily to avoid a circular import (
from_functionimportsTool).regenerated.
namestays required.The change is fully backward compatible: callers that pass
descriptionandparametersexplicitly get identical behavior.How did you test it?
test/tools/test_tool.py(TestToolAutoDeriveFromFunction)covering: derivation from sync and async functions, equivalence with
create_tool_from_function, backward compatibility (explicit values untouched),partial derivation (only the missing field), empty description when the function has
no docstring, exclusion of
inputs_from_stateparameters from the derived schema,serialization round-trip, and the missing-type-hint error path.
test/tools/suite (276 passed, 11 skipped), plusruff format --check,ruff check,mypy, and the CI import smoke (.github/utils/check_imports.py,242 modules) locally — all green.
releasenotes/notes/.Notes for the reviewer
nameis intentionally kept required to keep the change minimal; name inference fromthe function could be a follow-up if desired.
the relevant tests.
Checklist
feat:.