As it uses the function signature and the arguments of the first invocation to generate the function, it can deal with unexpected arguments. Here is an example recursively merging dictionaries:
When importing your function it doesn't actually produce the function yet, it just provides a (GPT) wrapper. When invoking the function for the first time it uses the following information to produce a function:
The prompt includes these values together with a request to generate a Python function within a Markdown python block. The code is extracted from the response, checked for valid syntax, loaded into the namespace and executed against the inputs.
While the generated functions are usually correct, the produced syntax might not be correct or the function might fail during execution. When this happens, chatgpt will regenerate the function with the previous implementations and associated error messages included in the prompt. The number of retries is capped at 3.
The function is cached after successful generation. The cache is keyed on the function signature. This means that the next time you invoke the function it will check the cache if there was a previous invocation with the same argument structure, and if so it will reuse that one. This means that you can "safely" run the following code without paying a month of rent to OpenAI: