💻Code execution
Enable code execution on the model
You can enable code execution on the model, as shown here:
The output might look something like this:
Enable code execution on the request
Alternatively, you can enable code execution on the call to generate_content
:
Use code execution in chat
You can also use code execution as part of a chat.
Code execution versus function calling
Code execution and function calling are similar features:
Code execution lets the model run code in the API backend in a fixed, isolated environment.
Function calling lets you run the functions that the model requests, in whatever environment you want.
In general you should prefer to use code execution if it can handle your use case. Code execution is simpler to use (you just enable it) and resolves in a single GenerateContent
request (thus incurring a single charge). Function calling takes an additional GenerateContent
request to send back the output from each function call (thus incurring multiple charges).
For most cases, you should use function calling if you have your own functions that you want to run locally, and you should use code execution if you'd like the API to write and run Python code for you and return the result.
Last updated