Function calling
Connect language models to application-defined tools
Function calling lets a language model request an operation defined by your application. NaviGator AI Toolkit returns the requested function name and arguments; it does not execute your code.
How it works
- Send the model a list of tools described with JSON Schema, a format that defines the permitted argument structure.
- Inspect every returned
tool_call. - Validate and authorize the arguments.
- Execute the matching application function.
- Add the tool result to the conversation.
- Ask the model to produce the final response.
Tool support varies by model. This example uses gpt-oss-120b, which supports function calling.
Request a tool call
Before running an example, complete Getting Started, configure the documented environment variables and confirm that your key can use gpt-oss-120b.
curl "$NAVIGATOR_API_BASE_URL/chat/completions" \
--header "Authorization: Bearer $NAVIGATOR_TOOLKIT_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "gpt-oss-120b",
"messages": [{"role": "user", "content": "What is the weather in Gainesville?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
"additionalProperties": false
}
}
}],
"tool_choice": {"type": "function", "function": {"name": "get_weather"}}
}'The requested call appears under choices[0].message.tool_calls, with the function name and its arguments as a JSON string.
Complete the round trip
Your application runs the function, sends the result back as a tool message and asks for the final answer. The Python and JavaScript examples need the openai package from OpenAI-compatible SDKs.
import json
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["NAVIGATOR_TOOLKIT_API_KEY"],
base_url=os.environ.get("NAVIGATOR_API_BASE_URL", "https://api.navigator.ai.ufl.edu/v1"),
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
"additionalProperties": False,
},
},
}
]
messages = [{"role": "user", "content": "What is the weather in Gainesville?"}]
response = client.chat.completions.create(
model="gpt-oss-120b",
messages=messages,
tools=tools,
tool_choice={"type": "function", "function": {"name": "get_weather"}},
)
assistant = response.choices[0].message
messages.append(assistant.model_dump(exclude_none=True))
for call in assistant.tool_calls or []:
if call.function.name != "get_weather":
raise ValueError("Unsupported tool requested")
arguments = json.loads(call.function.arguments)
# Example data. A real application would call a weather service here.
result = {"city": arguments["city"], "temperature_f": 78, "conditions": "Sunny"}
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
final = client.chat.completions.create(model="gpt-oss-120b", messages=messages)
print(final.choices[0].message.content)A successful round trip prints an answer based on the sample Gainesville weather data. The wording varies.
Let the model decide
The examples force a call to get_weather with tool_choice, so the output is predictable. In an application, leave tool_choice out or set it to "auto": the model then calls a tool only when it decides one is needed. Test this with the exact model you use, because models differ in how readily they call tools.
Security
- Allowlist function names rather than executing arbitrary model output.
- Validate arguments against the schema and your application's business rules.
- Authorize every operation for the current user.
- Apply timeouts and output-size limits to external calls.
- Require confirmation before consequential or irreversible actions.
- Treat tool results as untrusted input before returning them to the model.