Skip to content
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

Update Tool dataclass to accept a custom function_schema argument #881

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

jonchun
Copy link

@jonchun jonchun commented Feb 10, 2025

Unsure if the tests should be importing _pydantic like that, but the idea behind the test is to add a function schema for a different function and checking that the generated json schema matches the function schema passed rather than the one that would have been generated without it.

Resolves #878.

@github-actions github-actions bot temporarily deployed to deploy-preview February 10, 2025 02:50 Inactive
@github-actions github-actions bot temporarily deployed to deploy-preview February 11, 2025 20:21 Inactive
Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, thanks for the contribution! I commented with one test change request :)

Comment on lines 360 to 362
# make sure the function schema for y_tool is used instead of the default of x_tool.
assert 'x' not in agent._function_tools['x_tool']._parameters_json_schema['properties']
assert 'y' in agent._function_tools['x_tool']._parameters_json_schema['properties']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could just do assert agent._function_tools['x_tool']._parameters_json_schema == snapshot(...)?

Copy link
Author

@jonchun jonchun Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmontagu
Copy link
Contributor

I like this, but if we expose a way to override the function_schema (which I see could be a useful convenience in some scenarios where a function is defined using *args and **kwargs), I think we should move pydantic_ai._pydantic to be a public module (or at least the things in it related to FunctionSchema), and rename it.

@github-actions github-actions bot temporarily deployed to deploy-preview February 12, 2025 01:31 Inactive
@jonchun
Copy link
Author

jonchun commented Feb 12, 2025

@dmontagu - Any suggestions? There's quite a lot/almost all of _pydantic is related to FunctionSchema or building it. I had the same thought as you but figured it was internal intentionally because of this comment

This module has to use numerous internal Pydantic APIs and is therefore brittle to changes in Pydantic.

I think maybe we can pull out only the FunctionSchema TypedDict so that it is public?

@github-actions github-actions bot temporarily deployed to deploy-preview February 12, 2025 03:10 Inactive
@jonchun jonchun force-pushed the tool-function-schema branch from a0b36dd to 17df4fc Compare February 12, 2025 03:13
@github-actions github-actions bot temporarily deployed to deploy-preview February 12, 2025 03:17 Inactive
@sydney-runkle
Copy link
Member

I don't think we should make pydantic_ai._pydantic public - I think we should preserve its privacy so that we have the ability to make changes quickly as needed for now.

That being said, let's expose FunctionSchema. I think in tools.py would be appropriate for now.

@jonchun
Copy link
Author

jonchun commented Feb 12, 2025

@sydney-runkle - how does the latest commit look? I had to change a few things around due to the circular dependency.

@github-actions github-actions bot temporarily deployed to deploy-preview February 12, 2025 22:35 Inactive
@jonchun jonchun force-pushed the tool-function-schema branch from c30b05f to 0a3a97d Compare February 13, 2025 02:39
@github-actions github-actions bot temporarily deployed to deploy-preview February 13, 2025 02:43 Inactive
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.

Allow custom function_schema definition for Tool dataclass
3 participants