Skip to content

feat: add type validation to params when tool is invoked #129

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

Merged
merged 88 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from 81 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
5f1b2b0
feat: add authenticated parameters support
kurtisvg Mar 30, 2025
34bec37
chore: add asyncio dep
kurtisvg Mar 30, 2025
f0024bf
chore: run itest
kurtisvg Mar 30, 2025
a646285
chore: add type hint
kurtisvg Mar 30, 2025
0dc7034
fix: call tool instead of client
kurtisvg Mar 30, 2025
61d32aa
chore: correct arg name
kurtisvg Mar 31, 2025
744ade9
feat: add support for bound parameters
kurtisvg Mar 31, 2025
b875771
chore: add tests for bound parameters
kurtisvg Mar 31, 2025
138d8d9
docs: update syntax error on readme (#121)
Yuan325 Mar 31, 2025
e2f5b4d
ci: added release please config (#112)
twishabansal Apr 1, 2025
280a79f
added basic e2e tests
twishabansal Apr 1, 2025
63808ce
change license year
twishabansal Apr 1, 2025
55f5ca0
add test deps
twishabansal Apr 1, 2025
ed16488
fix tests
twishabansal Apr 1, 2025
f0991ab
fix tests
twishabansal Apr 1, 2025
cc6d568
fix tests
twishabansal Apr 1, 2025
1f63b0d
add new test case
twishabansal Apr 1, 2025
66e88ab
fix docstring
twishabansal Apr 1, 2025
1fa1b7a
added todo
twishabansal Apr 1, 2025
12af5fa
cleanup
twishabansal Apr 1, 2025
9c3ba38
add bind param test case
twishabansal Apr 1, 2025
d75828e
make bind params dynamic
twishabansal Apr 1, 2025
fef1f7f
try fix test errors
twishabansal Apr 1, 2025
cd701b7
lint
twishabansal Apr 1, 2025
7a5a1bd
remove redundant test
twishabansal Apr 1, 2025
e343e78
test fix
twishabansal Apr 1, 2025
11d62b4
fix docstring
twishabansal Apr 1, 2025
87e00cc
feat: add authenticated parameters support
kurtisvg Mar 30, 2025
6b263ad
chore: add asyncio dep
kurtisvg Mar 30, 2025
5fe541f
chore: run itest
kurtisvg Mar 30, 2025
e9d7a31
chore: add type hint
kurtisvg Mar 30, 2025
58c55cf
fix: call tool instead of client
kurtisvg Mar 30, 2025
c1a482a
chore: correct arg name
kurtisvg Mar 31, 2025
c1ac2cd
chore: address feedback
kurtisvg Apr 2, 2025
bcba462
chore: address more feedback
kurtisvg Apr 2, 2025
c8491a9
feat: add support for bound parameters
kurtisvg Mar 31, 2025
c26c453
chore: add tests for bound parameters
kurtisvg Mar 31, 2025
3abe9e4
chore: address feedback
kurtisvg Apr 2, 2025
3200f00
Merge branch 'kvg-bound-params' into twisha-core-e2e
twishabansal Apr 2, 2025
9482f37
revert package file changes
twishabansal Apr 2, 2025
6dca7f9
fix error message
twishabansal Apr 2, 2025
4d562bd
revert package files
twishabansal Apr 2, 2025
cc7ccad
lint
twishabansal Apr 2, 2025
84e66a6
fix error message
twishabansal Apr 2, 2025
fe2a332
Update packages/toolbox-core/tests/test_e2e.py
twishabansal Apr 2, 2025
3c67596
try changing docstring to include args and their descriptions
twishabansal Apr 2, 2025
183ea48
fix docstring
twishabansal Apr 2, 2025
df96f53
lint
twishabansal Apr 2, 2025
8023460
fix test
twishabansal Apr 2, 2025
3520277
lint
twishabansal Apr 2, 2025
beab901
added return type annotation
twishabansal Apr 2, 2025
10601aa
Add docstrings
twishabansal Apr 2, 2025
65b729d
use and update schema
twishabansal Apr 2, 2025
c613ae8
lint
twishabansal Apr 2, 2025
9e63c37
lint
twishabansal Apr 2, 2025
f242722
try out pydantic validation
twishabansal Apr 2, 2025
e60ab95
revert changes to e2e test file
twishabansal Apr 2, 2025
c7b76da
add basic pydantic type checking
twishabansal Apr 2, 2025
9e01213
lint
twishabansal Apr 2, 2025
5ac71e2
fix pydantic validation error
twishabansal Apr 2, 2025
484d347
fix error string
twishabansal Apr 2, 2025
a3ecb49
fix error string
twishabansal Apr 2, 2025
8796452
lint
twishabansal Apr 2, 2025
f4a9c7d
added to_pydantic_model as a method under the toolschema class
twishabansal Apr 2, 2025
8793fa7
lint
twishabansal Apr 2, 2025
ce72925
Merge branch 'main' into twisha-core-docstring
twishabansal Apr 3, 2025
53655ea
revert changes to e2e test file
twishabansal Apr 3, 2025
d6c0b34
change string to str and in arg type
twishabansal Apr 3, 2025
1260987
small change
twishabansal Apr 3, 2025
57843e9
fix imports
twishabansal Apr 3, 2025
4adda1b
remove toolschema usage
twishabansal Apr 3, 2025
5cc8978
lint
twishabansal Apr 3, 2025
018cd88
Merge branch 'twisha-core-docstring' into twisha-core-pydantic
twishabansal Apr 3, 2025
2085aef
create pydantic model at init
twishabansal Apr 3, 2025
889401c
lint
twishabansal Apr 3, 2025
1dffce2
move create_docstring method outside the class.
twishabansal Apr 3, 2025
2187cea
lint
twishabansal Apr 3, 2025
7c6ec53
Merge branch 'twisha-core-docstring' into twisha-core-pydantic
twishabansal Apr 3, 2025
728c172
Merge branch 'main' into twisha-core-pydantic
twishabansal Apr 3, 2025
8ae4782
lint
twishabansal Apr 3, 2025
da2b838
Merge branch 'main' into twisha-core-pydantic
twishabansal Apr 3, 2025
98169e6
Update packages/toolbox-core/src/toolbox_core/tool.py
twishabansal Apr 3, 2025
45bcdca
move to_pydantic_model outside the class
twishabansal Apr 3, 2025
5d97a2c
lint
twishabansal Apr 3, 2025
0ca6adb
rename function
twishabansal Apr 3, 2025
e91da98
Merge branch 'main' into twisha-core-pydantic
twishabansal Apr 3, 2025
515bdc5
added name to pydantic model
twishabansal Apr 3, 2025
5cee81d
lint
twishabansal Apr 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/toolbox-core/src/toolbox_core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
Mapping,
Optional,
Sequence,
Type,
Union,
cast,
)

from aiohttp import ClientSession
from pydantic import BaseModel, Field, create_model

from toolbox_core.protocol import ParameterSchema

Expand Down Expand Up @@ -78,6 +81,8 @@ def __init__(
self.__url = f"{base_url}/api/tool/{name}/invoke"
self.__description = description
self.__params = params
self.__pydantic_model = self._to_pydantic_model()

inspect_type_params = [param.to_param() for param in self.__params]

# the following properties are set to help anyone that might inspect it determine usage
Expand All @@ -86,6 +91,7 @@ def __init__(
self.__signature__ = Signature(
parameters=inspect_type_params, return_annotation=str
)

self.__annotations__ = {p.name: p.annotation for p in inspect_type_params}
# TODO: self.__qualname__ ??

Expand All @@ -96,6 +102,19 @@ def __init__(
# map of parameter name to value (or callable that produces that value)
self.__bound_parameters = bound_params

def _to_pydantic_model(self) -> Type[BaseModel]:
"""Converts the given manifest schema to a Pydantic BaseModel class."""
field_definitions = {}
for field in self.__params:
field_definitions[field.name] = cast(
Any,
(
field.to_param().annotation,
Field(description=field.description),
),
)
return create_model("tool_model", **field_definitions)

def __copy(
self,
session: Optional[ClientSession] = None,
Expand Down Expand Up @@ -170,6 +189,9 @@ async def __call__(self, *args: Any, **kwargs: Any) -> str:
all_args.apply_defaults() # Include default values if not provided
payload = all_args.arguments

# Perform argument type checks using pydantic
self.__pydantic_model.model_validate(payload)

# apply bounded parameters
for param, value in self.__bound_parameters.items():
if asyncio.iscoroutinefunction(value):
Expand Down
5 changes: 3 additions & 2 deletions packages/toolbox-core/tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import pytest
import pytest_asyncio
from pydantic import ValidationError

from toolbox_core.client import ToolboxClient
from toolbox_core.tool import ToolboxTool
Expand Down Expand Up @@ -77,8 +78,8 @@ async def test_run_tool_missing_params(self, get_n_rows_tool: ToolboxTool):
async def test_run_tool_wrong_param_type(self, get_n_rows_tool: ToolboxTool):
"""Invoke a tool with wrong param type."""
with pytest.raises(
Exception,
match='provided parameters were invalid: unable to parse value for "num_rows": .* not type "string"',
ValidationError,
match=r"num_rows\s+Input should be a valid string\s+\[type=string_type,\s+input_value=2,\s+input_type=int\]",
):
await get_n_rows_tool(num_rows=2)

Expand Down