Function calling allows models to invoke defined functions during conversations, enabling them to perform specific actions or retrieve external information.
from openai import OpenAIimport jsonclient = OpenAI( api_key="your_truefoundry_api_key", base_url="{GATEWAY_BASE_URL}")# Define a functiontools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } }}]# Make the requestresponse = client.chat.completions.create( model="openai-main/gpt-4o-mini", messages=[{"role": "user", "content": "What's the weather in New York?"}], tools=tools)# Check if the model wants to call a functionif response.choices[0].message.tool_calls: tool_call = response.choices[0].message.tool_calls[0] function_name = tool_call.function.name function_args = json.loads(tool_call.function.arguments) print(f"Function called: {function_name}") print(f"Arguments: {function_args}")
Process function calls and continue the conversation:
# Initial requestmessages = [{"role": "user", "content": "What's the weather in Tokyo?"}]response = client.chat.completions.create( model="openai-main/gpt-4o-mini", messages=messages, tools=tools)# Handle function callif response.choices[0].message.tool_calls: messages.append(response.choices[0].message) for tool_call in response.choices[0].message.tool_calls: function_name = tool_call.function.name function_args = json.loads(tool_call.function.arguments) # Execute your function (simulated here) if function_name == "get_weather": result = f"The weather in {function_args['location']} is 22°C and sunny" # Add the function result to the conversation messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": result }) # Continue the conversation final_response = client.chat.completions.create( model="openai-main/gpt-4o-mini", messages=messages ) print(final_response.choices[0].message.content)
Controlling When and How Functions Are Called
Control when and how functions are called:
# Force a specific function callresponse = client.chat.completions.create( model="openai-main/gpt-4o-mini", messages=[{"role": "user", "content": "What's the weather?"}], tools=tools, tool_choice={"type": "function", "function": {"name": "get_weather"}})# Allow automatic function calling (default)response = client.chat.completions.create( model="openai-main/gpt-4o-mini", messages=[{"role": "user", "content": "What's the weather?"}], tools=tools, tool_choice="auto")# Prevent function callingresponse = client.chat.completions.create( model="openai-main/gpt-4o-mini", messages=[{"role": "user", "content": "What's the weather?"}], tools=tools, tool_choice="none")# Force any function callresponse = client.chat.completions.create( model="openai-main/gpt-4o-mini", messages=[{"role": "user", "content": "What's the weather?"}], tools=tools, tool_choice="required")
Thought signatures are encrypted representations of a model’s internal reasoning process that help maintain context and coherence across multi-turn interactions, particularly during function calling. When using certain Gemini 3 preview models, the API includes a thought_signature field in tool call responses.
from openai import OpenAIimport jsonclient = OpenAI( api_key="your_truefoundry_api_key", base_url="{GATEWAY_BASE_URL}")tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get weather for a location", "parameters": { "type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"] } }}]# First call - model requests toolresponse = client.chat.completions.create( model="vertex-main/gemini-3-pro-preview", messages=[{"role": "user", "content": "What's the weather in San Francisco?"}], tools=tools)message = response.choices[0].messageif message.tool_calls: tool_call = message.tool_calls[0] args = json.loads(tool_call.function.arguments) result = f"The weather in {args['location']} is 18°C and cloudy." # Convert message to dict (preserves thought_signature) assistant_message = message.model_dump(exclude_none=True) # Second call - send tool result back final_response = client.chat.completions.create( model="vertex-main/gemini-3-pro-preview", messages=[ {"role": "user", "content": "What's the weather in San Francisco?"}, assistant_message, # Includes thought_signature { "role": "tool", "content": json.dumps(result), "tool_call_id": tool_call.id } ] ) print(final_response.choices[0].message.content)