OpenAI

Function Calling in the OpenAI API

What is function calling and how does it work in the OpenAI API?

Updated: 19 hours ago

As of March 11, 2025, we’ve released the building blocks of our new Agents platform. For details, see our API docs for our Responses API, Tools including Web Search, File Search, and Computer Use, and our Agents SDK with Tracing.

Function calling allows you to connect OpenAI models to external tools and systems. This is useful for many things such as empowering AI assistants with capabilities, or building deep integrations between your applications and LLMs.

Learn more in our function calling developer guide.

In June 2024, we launched Structured Outputs. When you turn it on by setting strict: true, in your function definition, Structured Outputs guarantees that the arguments generated by the model for a function call exactly match the JSON Schema you provided in the function definition.

In October 2024, we launched the 'Generate Anything' feature, which allows developers to describe a function, paste it in directly or paste your code and generate a valid function schema. Learn more about 'Generate Anything' in help center article here

How can I use function calling?

Function calling is useful for a large number of use cases, such as:

  • Enabling assistants to fetch data:

  • Enabling assistants to take actions:

  • Enabling assistants to perform computation:

  • Building rich workflows:

  • a data extraction pipeline that fetches raw text, then converts it to structured data and saves it in a database.

Function calling is supported in the Responses API, which unifies capabilities that were previously split across the Chat Completions API and the Assistants API.

How can I use JSON mode?

When JSON mode is turned on, the model's output is ensured to be valid JSON, except for in some edge cases that you should detect and handle appropriately.

To request JSON mode with the Responses API or the Chat Completions API, you can set the response_format to { "type": "json_object" } on supported models, but this works only when model/message/tool preconditions are satisfied (for example, the model supports json_object, the conversation includes an instruction to produce JSON, and any tool constraints are compatible). For JSON mode with response_format: {"type": "json_object"}, at least one request message must contain json in some form, such as JSON, json, or Json; otherwise the API returns an error. When function calling is used on compatible models/paths, JSON constraints are applied automatically to function-call arguments; incompatible models or tool/response-format combinations may be rejected or may not use JSON constrained sampling.

Important notes:

  • When using JSON mode, you must always instruct the model to produce JSON via some message in the conversation, for example via your system message. If you don't include an explicit instruction to generate JSON, the model may generate an unending stream of whitespace and the request may run continually until it reaches the token limit. To help ensure you don't forget, JSON mode rejects requests unless the relevant input messages or instructions contain the word json in some form, case-insensitively.

  • JSON mode will not guarantee the output matches any specific schema, only that it is valid and parses without errors. You should use Structured Outputs to ensure it matches your schema, or if that is not possible, you should use a validation library and potentially retries to ensure that the output matches your desired schema.

  • Your application must detect and handle the edge cases that can result in the model output not being a complete JSON object (see below)

Was this article helpful?