AI / Core OpenAI Codex Application Fundamentals Interview Questions
What is function calling (tool use) in the OpenAI API and how does it work?
Function calling (referred to as tool use in the Responses API) allows you to describe external functions to the model in a structured JSON schema format. The model then decides when and how to call those functions, returning structured arguments you can use to invoke your actual code.
# Chat Completions - function calling from openai import OpenAI import json client = OpenAI() # 1. Define functions as tools tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a city", "parameters": { "type": "object", "properties": { "city": {"type": "string", "description": "City name"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["city"] } } }] # 2. First call - model decides to call a function response = client.chat.completions.create( model="gpt-5.5", messages=[{"role": "user", "content": "What's the weather in Tokyo?"}], tools=tools, tool_choice="auto", # "auto" | "required" | {"type":"function","function":{"name":"..."}} ) # 3. Extract the function call message = response.choices[0].message tool_call = message.tool_calls[0] function_args = json.loads(tool_call.function.arguments) # 4. Execute your function weather_result = get_weather(**function_args) # your actual implementation # 5. Second call - send function result back response2 = client.chat.completions.create( model="gpt-5.5", messages=[ {"role": "user", "content": "What's the weather in Tokyo?"}, message, # assistant turn with tool_calls {"role": "tool", "content": json.dumps(weather_result), "tool_call_id": tool_call.id} ], tools=tools, )
In the Responses API, the same concept is called tool use and requires less manual state management - you can use previous_response_id to chain turns instead of manually reconstructing the message array.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
