Python 88.3%
TypeScript 7.6%
Shell 4.1%
1#!/usr/bin/env python32"""Function calling: force a strict function, send the result back, stream the arguments.3STATUS: LIVE_VERIFIED 2026-09-18 (gpt-5.4-nano; forced tool_choice -> function_call -> function_call_output -> message; streaming4emits response.function_call_arguments.delta/done). Run: .venv/bin/python examples/openai/tools/function-calling/function_calling.py5"""6import json, sys7from pathlib import Path8ROOT = Path(__file__).resolve().parents[4]; sys.path.insert(0, str(ROOT))9from scripts.live import load_env, log_request; load_env()10from openai import OpenAI11client = OpenAI(); MODEL = "gpt-5.4-nano"1213WEATHER = {"type": "function", "name": "get_weather", "description": "Current weather for a city.", "strict": True,14 "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], "additionalProperties": False}}1516r1 = client.responses.create(model=MODEL, input="Weather in Paris?", tools=[WEATHER], max_output_tokens=64,17 tool_choice={"type": "function", "name": "get_weather"}, reasoning={"effort": "low"})18log_request("openai", "POST", "/v1/responses", 200, 0.0001, "function_calling.py forced call")19call = next(i for i in r1.output if i.type == "function_call")20print("function_call:", call.name, call.arguments, call.call_id)2122r2 = client.responses.create(model=MODEL, previous_response_id=r1.id, tools=[WEATHER], max_output_tokens=32, reasoning={"effort": "low"},23 input=[{"type": "function_call_output", "call_id": call.call_id, "output": json.dumps({"temp_c": 18, "sky": "clear"})}])24log_request("openai", "POST", "/v1/responses", 200, 0.0001, "function_calling.py round trip")25print("final:", r2.output_text)2627with client.responses.stream(model=MODEL, input="Weather in Rome?", tools=[WEATHER], tool_choice="required", max_output_tokens=64, reasoning={"effort": "low"}) as s:28 seen = []29 for ev in s:30 seen.append(ev.type)31log_request("openai", "POST", "/v1/responses", 200, 0.0001, "function_calling.py stream")32print("stream events:", sorted(set(seen)))33