Appendix C. Implementing The PydanticFunctionTool
In Chapter 2, we introduced the PydanticFunctionTool and its usage pattern with our framework, as an alternative to our from-scratch SimpleFunctionTool. The main benefits of the PydanticFunctionTool are more robust JSON Schema generation for the associated function’s parameters, as well as more powerful validation through the pydantic library. In this appendix, we provide a walkthrough on the full implementation of the PydanticFunctionTool as well as its asynchronous version, AsyncPydanticFunctionTool.
C.1 Implementing PydanticFunctionTool
We now will build the PydanticFunctionTool wrapper class that will enable the usage pattern that we just showed. As we saw then, we wrap a function which takes in a single parameter params that is pydantic.BaseModel. For convenience and to conform to our typing practices that we’ve begun to establish in our framework, let’s create a designated type for these kinds of functions.
We’ll call such function types PydanticFunction since they package their parameters in a pydantic.BaseModel type as we saw in HailstoneParams. The next listing provides its definition.