Define tools
Addtools to session. Each tool uses the OpenAI GA shape.
Handle a call
The loop has four steps:1
Collect the arguments
The model streams arguments as
response.function_call_arguments.delta. Wait for response.function_call_arguments.done, which has the full JSON string and call_id.2
Run your function
Parse
arguments and run your code. Keep this on your server or in a trusted backend.3
Return the output
Send the result as a
function_call_output item that uses the same call_id:4
Ask for the reply
Send
response.create. The model speaks the answer using the output.Example event flow
Notes
outputmust be a string. Return JSON as a string.- Return an error message in
outputwhen your function fails, so the model can apologize. - Keep tool results short. Long results make the reply slower.
Related
- Client events for
conversation.item.createandresponse.create - Server events for function call events