#!/usr/bin/env bash # Function calling via curl: forced strict function -> function_call -> function_call_output round trip. # STATUS: LIVE_VERIFIED 2026-09-18 (gpt-5.4-nano). Run: bash examples/openai/tools/function-calling/function_calling.sh set -eo pipefail # no -u: oai() runs in $(...) subshells, so OAI_STATUS may be unset in the parent source "$(dirname "$0")/../../../../scripts/lib.sh" TOOL='{"type":"function","name":"get_weather","description":"Current weather for a city.","strict":true,"parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"],"additionalProperties":false}}' R1=$(oai POST /v1/responses -d "{\"model\":\"gpt-5.4-nano\",\"input\":\"Weather in Paris?\",\"tools\":[$TOOL],\"tool_choice\":{\"type\":\"function\",\"name\":\"get_weather\"},\"max_output_tokens\":64,\"reasoning\":{\"effort\":\"low\"}}") log_request openai POST /v1/responses "${OAI_STATUS:-200}" 0.0001 "function_calling.sh forced" RID=$(echo "$R1" | jq -r .id); CALL_ID=$(echo "$R1" | jq -r '.output[] | select(.type=="function_call") | .call_id') echo "$R1" | jq -c '.output[] | select(.type=="function_call") | {name, arguments, call_id, status}' R2=$(oai POST /v1/responses -d "{\"model\":\"gpt-5.4-nano\",\"previous_response_id\":\"$RID\",\"tools\":[$TOOL],\"max_output_tokens\":32,\"reasoning\":{\"effort\":\"low\"},\"input\":[{\"type\":\"function_call_output\",\"call_id\":\"$CALL_ID\",\"output\":\"{\\\"temp_c\\\":18,\\\"sky\\\":\\\"clear\\\"}\"}]}") log_request openai POST /v1/responses "${OAI_STATUS:-200}" 0.0001 "function_calling.sh round trip" echo "$R2" | jq -r '.output[] | select(.type=="message") | .content[0].text'