匠人学院 JR Academy
Skip to article
Learning guide
OpenAI API Guide

Build with the OpenAI API using Responses API, streaming, tools, embeddings, and multimodal inputs.

3 chapters·9 guides·Access varies by chapter
Function Calling
On this page

OpenAI Function Calling

A model requests a structured action; the application validates, authorizes and executes it, then returns a result associated with that call. The model does not receive unrestricted control of the system.

#Appropriate uses

Read an order or inventory record, query another service, or extract arguments for an auditable workflow. A single text-generation task without external data does not necessarily need a tool loop.

#A bounded Responses example

The order data below is a synthetic read-only fixture. The SDK requests still use a real configured model if this program is run.

python
import json import os from openai import OpenAI client = OpenAI(timeout=20.0) tools = [{ "type": "function", "name": "lookup_order", "description": "Read a synthetic teaching order; no real customer data.", "strict": True, "parameters": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"], "additionalProperties": False, }, }] def execute(name, raw): if name != "lookup_order": return {"error": "unknown_tool"} try: args = json.loads(raw) except (TypeError, json.JSONDecodeError): return {"error": "invalid_json"} if not isinstance(args, dict) or set(args) != {"order_id"}: return {"error": "invalid_arguments"} if not isinstance(args["order_id"], str): return {"error": "invalid_arguments"} if args["order_id"] != "DEMO-1001": return {"error": "order_not_found"} return {"status": "shipped", "mode": "synthetic_fixture"} response = client.responses.create( model=os.environ["OPENAI_MODEL"], input="What is the status of teaching order DEMO-1001?", tools=tools, ) for step in range(4): calls = [item for item in response.output if item.type == "function_call"] if not calls: print(response.output_text) break outputs = [{ "type": "function_call_output", "call_id": call.call_id, "output": json.dumps(execute(call.name, call.arguments)), } for call in calls] response = client.responses.create( model=os.environ["OPENAI_MODEL"], previous_response_id=response.id, input=outputs, tools=tools, ) else: raise RuntimeError("step_limit_reached")

#Multiple and parallel requests

A response may contain several calls. Return an output for each call ID. Execute independently only when the tools and business semantics permit it; parallel model requests do not authorize concurrent writes.

Tool-choice settings can constrain whether a supported tool is available or required, but do not replace the application's allowlist and argument checks.

#Descriptions and schemas

Describe observable tool behavior, required fields, formats and meaningful enums. Reject unknown fields and invalid values even when schema-constrained generation is enabled.

#Production boundaries

Never pass a model-generated expression to eval or a shell. Re-check authorization using the authenticated user, not an argument supplied by the model. Use idempotency for side effects, bounded retries and explicit failure results. Preserve enough safe event evidence to connect a tool request, execution and response.

This chapter's fixture shows execution structure. It does not establish live-model accuracy or production reliability.

#References and next steps

Published by
JR Academy
Learning resources

Individual authors and reviewers are credited where recorded.

System Design

Core system design concepts and practical case studies

Learn the trade-offs and patterns that matter in technical interviews.

Open System Design →

Related Roadmaps

Done reading? Next steps