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

Conversation

twishabansal
Copy link
Contributor

@twishabansal twishabansal commented Apr 2, 2025

Adds type validation to inputs when a tool is invoked using pydantic.

@twishabansal
Copy link
Contributor Author

twishabansal commented Apr 3, 2025

I see base model also has a signiture -- can we reuse theirs instead of creating our own? https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel

There is also a .model_fields option -- maybe we use this to replace parameters to address my comment in the other PR about not using ToolSchema directly?

By default, Pydantic models treat all fields without defaults as keyword-only arguments in their generated init signature.

This means, if we use the pydantic model signature (self.__pydantic_model.__signature__),
tool calls like loaded_tool("some value") == "ok" will fail with errors: TypeError: too many positional arguments.
but tool calls like loaded_tool(param1="some value") == "ok" will pass.

I tried using kw_only=False (ref) as a constraint. But it still shows up as a keyword argument in the generated signature.

@twishabansal twishabansal requested a review from kurtisvg April 3, 2025 10:19
Base automatically changed from twisha-core-docstring to main April 3, 2025 15:50
@kurtisvg kurtisvg changed the title feat: Validate tool param types before tool calling feat: add type validation to params when tool is invoked Apr 3, 2025
@twishabansal twishabansal requested a review from anubhav756 April 3, 2025 17:48
@twishabansal twishabansal merged commit 5d62138 into main Apr 3, 2025
14 checks passed
@twishabansal twishabansal deleted the twisha-core-pydantic branch April 3, 2025 18:09
This was referenced Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants