OpenAI Function Calling Complete Guide: Complete Developer Guide 2026

Master OpenAI Function Calling Complete Guide with practical examples and production patterns

返回教程列表
进阶11 分钟

OpenAI Function Calling Complete Guide: Complete Developer Guide 2026

Master OpenAI Function Calling Complete Guide with practical examples and production patterns

OpenAI Function/Tool Calling 完全指南(2026):用 JSON Schema 定义工具→模型返回结构化调用→你执行并回填结果的完整循环,含真实代码、生产模式(校验/tool_choice/并行调用/strict)、与结构化输出的区别,以及它如何支撑 Agent。

OpenAI Function Calling: Complete Developer Guide (2026)

Function calling (now usually called tool calling) lets a model decide to invoke functions you define and return structured arguments for them. You describe your tools as JSON Schemas; the model, when appropriate, replies with a tool call (name + JSON arguments) instead of free text; your code runs the function and feeds the result back. This is the foundation of agents, RAG retrieval, and any app where the LLM must take actions reliably.

The core loop

python

pip install openai

from openai import OpenAI client = OpenAI()

tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, }, }]

messages = [{"role": "user", "content": "What's the weather in Tokyo?"}] resp = client.chat.completions.create(model="gpt-4o", messages=messages, tools=tools)

call = resp.choices[0].message.tool_calls[0] import json args = json.loads(call.function.arguments) # {"city": "Tokyo"} result = get_weather(**args) # your real function

feed the result back so the model can answer in natural language

messages.append(resp.choices[0].message) messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)}) final = client.chat.completions.create(model="gpt-4o", messages=messages, tools=tools) print(final.choices[0].message.content)

Production patterns that matter

  • Always validate arguments. The model returns JSON, but treat it as untrusted input — parse into a schema (Pydantic) and handle bad values. See Pydantic AI vs Instructor.
  • Use tool_choice to force, forbid, or auto-select tools. Force a specific tool when you need guaranteed structured output.
  • Handle parallel tool calls. Modern models can request several tools at once; iterate over tool_calls and append one tool message per call id.
  • Keep tools small and orthogonal. Many overlapping tools confuse the model; a handful of clear ones work better.
  • Strict mode / structured outputs can guarantee the arguments match your schema exactly — use it when downstream code is rigid.
  • Function calling vs structured outputs

    If you only want a typed object back (not to *run* anything), use structured outputs / response schemas rather than a tool round-trip. If the model must choose actions and you execute them, that's tool calling. Anthropic's Claude offers the same capability through its tool-use API — the concepts transfer directly.

    How it powers agents

    Every agent framework is, underneath, this loop wrapped in control flow. To see it scaled up, compare OpenAI Assistants vs LangGraph and CrewAI vs AutoGen.

    FAQ

    Does the model actually run my function? No — it only returns the name and arguments. Your code executes it and returns the result. Can it call multiple tools? Yes, in parallel; handle each tool_call_id separately. How do I guarantee valid JSON? Use strict/structured-output mode and validate with Pydantic before executing. Is this OpenAI-only? No — Claude and others expose equivalent tool-use APIs with the same loop.

    Summary

    Function calling turns an LLM from a text generator into a controller that can take actions. Define tools as JSON Schemas, run the call loop, validate every argument, and feed results back. Master this and agents, RAG, and tool-augmented apps all become variations on one pattern.


    *Last updated: June 2026. Verify the current API shape in the OpenAI docs.*

    相关工具

    LangChainOpenAIPython
    所属主题:OpenAI 开发实战