I found the solution: apparently there are even more related type hints, one of which is also called FunctionDefinition. Note that even though this thing is called literally the same thing, it is actually a different class than the one I used in my example above. (One is imported from openai.types.shared, the other from openai.types.shared_params.)
This works:
from openai.types.shared_params.function_definition import FunctionDefinition
function_definition_extract_number: FunctionDefinition = {
...
}
This thing is accepted by the assistant API:
- the
tools
argument inopenai.beta.assistants.create
acceptsIterable[AssistantToolParam]
- A
FunctionToolParam
is a validAssistantToolParam
- We can create a
FunctionToolParam
by combining aFunctionDefinition
with the literal string"function"
in a python dict. Note: this only works with FunctionDefinition imported from openai.types.shared_params, not the one imported from openai.shared.params. - We can create a
FunctionDefinition
in the usual way, i.e. a python dict with three keys:name
,description
, andparameters
– see above for an example.
Miraculously it is also accepted by the chat completions API, even though the documentation does not show that!!!
- The
functions
arguments ofclient.chat.completions.create
accepts an object of typeIterable[completion_create_params.Function]
. - Somehow, miraculously, it also accepts
Iterable[FunctionDefinition]
. (Or at least Pyright doesn’t complain about it.)
So yeah… It’s a mess, which leads to pretty interesting type checking errors :
Any simplifications on this front would be appreciated!