How to properly type hint function calls: Function vs FunctionDefinition

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:

  1. the tools argument in openai.beta.assistants.create accepts Iterable[AssistantToolParam]
  2. A FunctionToolParam is a valid AssistantToolParam
  3. We can create a FunctionToolParam by combining a FunctionDefinition 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.
  4. We can create a FunctionDefinition in the usual way, i.e. a python dict with three keys: name, description, and parameters – see above for an example.

Miraculously it is also accepted by the chat completions API, even though the documentation does not show that!!!

  1. The functions arguments of client.chat.completions.create accepts an object of type Iterable[completion_create_params.Function].
  2. 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 :stuck_out_tongue: :

Any simplifications on this front would be appreciated!

1 Like