OPENAI WORKFLOW · 08 / 10

Return a function result to the Responses API

Complete the tool-call loop with a local demonstration function.

Documentation checked · Loomwell editorial guide · Examples have not been live API tested.

Steps

  1. Declare a function tool, inspect the returned calls, execute supported functions in your application, and return each result with its call_id.
  2. This example deliberately exposes only a harmless local lookup. Validate arguments and enforce permissions before connecting real tools.

Example

Python · Python API examples require OPENAI_API_KEY and OPENAI_MODEL in your server environment. API calls may incur OpenAI charges.

import json
import os
from openai import OpenAI

client = OpenAI()
model = os.environ["OPENAI_MODEL"]
tools = [{"type": "function", "name": "get_demo_status",
          "description": "Return the local demo status.", "strict": True,
          "parameters": {"type": "object", "properties": {},
                         "required": [], "additionalProperties": False}}]
first = client.responses.create(
    model=model, tools=tools, input="Get the demo status.",
    tool_choice={"type": "function", "name": "get_demo_status"},
    parallel_tool_calls=False,
)
results = []
for call in first.output:
    if call.type != "function_call":
        continue
    if call.name != "get_demo_status" or json.loads(call.arguments) != {}:
        raise ValueError("Unsupported function or arguments")
    results.append({"type": "function_call_output", "call_id": call.call_id,
                    "output": json.dumps({"status": "demo-ready"})})
if not results:
    raise RuntimeError("No function call returned")
final = client.responses.create(
    model=model, previous_response_id=first.id, input=results,
)
print(final.output_text)

Check the result

Expect the final answer to describe demo-ready. This is a bounded, one-tool illustration; a production agent needs limits, error handling, and support for further tool turns.

Official source

Function calling

Check current provider documentation and model support before deployment. This guide is separate from the approved evidence records listed in the catalog.

Use Loomwell with your coding agent

Inspect the published record coverage before starting a trial or connecting MCP.